Advertisement
ffpaladin

Missing Int O(n)

Oct 16th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.45 KB | None | 0 0
  1.  
  2. public class MissingInt {
  3.    
  4.    
  5.     public static void main (String args[]) {
  6.  
  7.         int[] array = {3,2,1,13,5,6,7,8,9,10,11,12,14};
  8.      
  9.         // sort the list O(nlogn)
  10.    
  11.         // or
  12.    
  13.         // sum the numbers O(n)
  14.         // then subtract the numbers O(n)
  15.    
  16.         int fullSum = array.length + 1;
  17.         int listSum = 0;
  18.  
  19.    
  20.         for (int i=0; i<array.length; i++)
  21.         {
  22.             listSum += array[i];
  23.             fullSum += (i+1);
  24.  
  25.         }
  26.        
  27.         System.out.println (fullSum-listSum);
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement