Skip to content

Instantly share code, notes, and snippets.

@0001vrn
Created March 11, 2017 07:20
Show Gist options
  • Select an option

  • Save 0001vrn/a312cf4f2115ade7ae64e9898f12c680 to your computer and use it in GitHub Desktop.

Select an option

Save 0001vrn/a312cf4f2115ade7ae64e9898f12c680 to your computer and use it in GitHub Desktop.
package com.company;
import java.util.Arrays;
import java.util.Scanner;
import static java.util.Arrays.binarySearch;
public class EXTRAN {
public static void main(String[] args) {
// write your code here
// your code goes here
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
System.out.println(getFaultNum(arr,n));
}
}
private static int getFaultNum(int[] arr, int n) {
Arrays.sort(arr);
//check for duplicates
for(int i=1;i<n;i++)
if(arr[i-1]==arr[i])
return arr[i];
//binarySearch based solution
int key = arr[0];
for(int i=1;i<n;i++){
if(binarySearch(arr,++key)>0)
{
key=arr[i];
continue;
}
else if(i==n-1)
return arr[n-1];
else
return arr[i-1];
}
return -1;
}
}
@PoLiAu
Copy link

PoLiAu commented Mar 11, 2017

Hmmm... I can't understand: for what this code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment