Created
March 11, 2017 07:20
-
-
Save 0001vrn/a312cf4f2115ade7ae64e9898f12c680 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hmmm... I can't understand: for what this code?