Created
September 17, 2018 19:50
-
-
Save deyindra/c3672e5d68b670fe02eb7f7fd7260bf5 to your computer and use it in GitHub Desktop.
Find Pair
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
public static void printPairUnsorted(int[] array, int sum){ | |
Set<Integer> s = new HashSet<>(); | |
for(int v:array){ | |
int temp = sum - v; | |
if(s.contains(temp)){ | |
System.out.println(String.format("Pair found %d %d", temp,v)); | |
} | |
s.add(v); | |
} | |
} | |
public static void printPairSorted(int[] array, int sum){ | |
int i=0; | |
int j=array.length-1; | |
while (i<j){ | |
int resultSum = array[i]+array[j]; | |
if(resultSum < sum){ | |
i++; | |
}else if (resultSum > sum){ | |
j--; | |
}else{ | |
System.out.println(String.format("Pair found %d %d", array[i],array[j])); | |
i ++ ; | |
j --; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment