We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I have written exact same code in java but test cases 3 and 4 are not passing. Please help me.
importjava.io.*;importjava.util.*;importjava.text.*;importjava.math.*;importjava.util.regex.*;publicclassSolution{publicstaticvoidmain(String[]args){/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */Scannersc=newScanner(System.in);intn=sc.nextInt();inta[]=newint[n];for(inti=0;i<n;i++){a[i]=sc.nextInt();}Arrays.sort(a);intd=a[1]-a[0],id=0;for(inti=1;i<n;i++){if(d>Math.abs(a[i]-a[i-1])){d=Math.abs(a[i]-a[i-1]);id=i-1;}}for(inti=id;i<n-1;i++){if(d==Math.abs(a[i]-a[i+1]))System.out.print(a[i]+" "+a[i+1]);}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Closest Numbers
You are viewing a single comment's thread. Return to all comments →
I have written exact same code in java but test cases 3 and 4 are not passing. Please help me.