Find missing elements of a range | A sorting based Java program to find missing elements from an array ; Print all elements of range [ low , high ] that are not present in arr [ 0. . n - 1 ] ; Do binary search for ' low ' in sorted array and find index of first element which either equal to or greater than low . ; Start from the found index and linearly search every range element x after this index in arr [ ] ; If x doesn 't math with current element  print it ; If x matches , move to next element in arr [ ] ; Move to next element in range [ low , high ] ; Print range elements thar are greater than the last element of sorted array . ; Utility function to find ceil index of given element ; Driver program to test above function
import java . util . Arrays ; public class PrintMissing { static void printMissing ( int ar [ ] , int low , int high ) { Arrays . sort ( ar ) ; int index = ceilindex ( ar , low , 0 , ar . length - 1 ) ; int x = low ; while ( index < ar . length && x <= high ) { if ( ar [ index ] != x ) { System . out . print ( x + " ▁ " ) ; } else index ++ ; x ++ ; } while ( x <= high ) { System . out . print ( x + " ▁ " ) ; x ++ ; } } static int ceilindex ( int ar [ ] , int val , int low , int high ) { if ( val < ar [ 0 ] ) return 0 ; if ( val > ar [ ar . length - 1 ] ) return ar . length ; int mid = ( low + high ) / 2 ; if ( ar [ mid ] == val ) return mid ; if ( ar [ mid ] < val ) { if ( mid + 1 < high && ar [ mid + 1 ] >= val ) return mid + 1 ; return ceilindex ( ar , val , mid + 1 , high ) ; } else { if ( mid - 1 >= low && ar [ mid - 1 ] < val ) return mid ; return ceilindex ( ar , val , low , mid - 1 ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 , 4 } ; int low = 1 , high = 10 ; printMissing ( arr , low , high ) ; } }