Skip to content

Instantly share code, notes, and snippets.

@thmain
Created August 28, 2017 01:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/28d5c970ebf41ee426c36167458591be to your computer and use it in GitHub Desktop.
Save thmain/28d5c970ebf41ee426c36167458591be to your computer and use it in GitHub Desktop.
public class ThreeNumbersSumKBruteForce {
public static void find(int [] a, int k){
for (int i = 0; i <a.length ; i++) {
for (int j = i+1; j <a.length ; j++) {
for (int l = j+1; l <a.length ; l++) {
if(a[i]+a[j]+a[l]==k){
System.out.println("Found 3 elements whose sum is = " +k);
System.out.println("Elements are " + a[i] + " " + a[j]+ " " + a[l]);
return;
}
}
}
}
System.out.println("Did not find 3 elements whose sum is = " +k);
}
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