Skip to content

Instantly share code, notes, and snippets.

@ghostrecog
Created August 11, 2017 07:48
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 ghostrecog/ff35caa12ba5b73234ce46e7fdcea1c2 to your computer and use it in GitHub Desktop.
Save ghostrecog/ff35caa12ba5b73234ce46e7fdcea1c2 to your computer and use it in GitHub Desktop.
You will be given an array and you need to find those three elements whose sum are equal to 0.If found print True to the output else print False.Note: The length of the array should not be less than 3.
/**
* You will be given an array and you need to find those three elements whose sum are equal to 0.
* If found print True to the output else print False.Note: The length of the array should not be less than 3.
*/
import java.util.*;
public class EqualToZero {
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
int size=0;
size=Integer.parseInt(sc.nextLine().trim());
if(size>2){
boolean output=false;
String arrayElements=sc.nextLine();
String [] array=arrayElements.split(" ");
int sum=0,x=0,y=0,z=0;
for(int i=0;i<array.length;i++){
x=Integer.parseInt(array[i]);
for(int j=i+1;j<array.length;j++){
y=Integer.parseInt(array[j]);
for(int k=j+1;k<array.length;k++){
z=Integer.parseInt(array[k]);
sum=x+y+z;
if(sum==0){
output=true;
break;
}
}
}
}
System.out.println(output?"True":"False");
}
sc.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment