Created
September 22, 2016 06:56
-
-
Save nichtemna/4cd43f6baf551a9bf3491f272c1db3a7 to your computer and use it in GitHub Desktop.
Identify whether there exists a pair of numbers in an array such that their sum is equal to N
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
/** | |
* Identify whether there exists a pair of numbers in an array such that their sum is equal to N | |
*/ | |
public class SumNumbers1 { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
int n = scanner.nextInt(); | |
int m = scanner.nextInt(); | |
int[] array = new int[n]; | |
Set<Integer> set = new HashSet<>(n); | |
for (int i = 0; i < array.length; i++) { | |
int v = scanner.nextInt(); | |
array[i] = v; | |
set.add(v); | |
} | |
List<String> res = new ArrayList<>(); | |
for (int i = 0; i < array.length; i++) { | |
int v1 = m - array[i]; | |
if (set.contains(v1) && v1 != array[i]) { | |
res.add(v1 + " " + array[i]); | |
} | |
} | |
if (res.size() > 0) { | |
for (String re : res) { | |
System.out.println(re); | |
} | |
} else { | |
System.out.print("No"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment