Created
August 28, 2017 01:26
-
-
Save thmain/b0fd054260271a95d8aedb828ecca7c7 to your computer and use it in GitHub Desktop.
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.Arrays; | |
public class ThreeNumbersSumKSorting { | |
public static void find(int [] a, int k){ | |
Arrays.sort(a); | |
for (int i = 0; i <a.length ; i++) { | |
int x = a[i]; | |
//now problem is reduced to find two numbers in an array whose sum = M | |
//here M = k - x and array from index = 1 to end | |
int M = k -x; | |
int start = i + 1; | |
int end = a.length-1; | |
while(start<end){ | |
int sum = a[start] + a[end]; | |
if(sum==M){ | |
System.out.println("Found 3 elements whose sum is = " +k); | |
System.out.println("Elements are " + a[i] + " " + a[start]+ " " + a[end]); | |
return; | |
}else if(sum<M) | |
start++; | |
else //sum>M | |
end--; | |
} | |
} | |
} | |
public static void main(String[] args) { | |
int a [] = { 3,1,7,4,5,9,10}; | |
int k = 21; | |
find(a,k); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment