Find missing number between 1 to 100 in unsortedArray - in one iteration in java


In this core java programming tutorial we will write a program to Find missing number between 1 to 100 in unSortedArray in one iteration (consumes more  memory) in java.  



Logic>
We will maintain one extra array of size 100 (i.e. tempAr[100]), after initialization all the values in tempAr will be 0.

Start iterating on ar,
In first iteration i=0 and let’s say we obtain 3 at ar[0]
i.e.   i[0]=33, than make tempAr[33]=1;
In first iteration i=1 and we obtain 25 at ar[1]  
i.e. i[1]=27, than make tempAr[27]=1;

Once we have completed iteration on ar,
we will iterate from 1 to 100  (keep it in variable i),
if tempAr[i]==0, number is missing in array.
if tempAr[i]==1, number is there in array.



Hence, because of tempAr[100] we will end up consuming extra memory, but process for finding missing numbers will be quick in java.

Full Program/SourceCode/ Example to Find missing number between 1 to 100 in unsorted Array - consumes more memory (but quick) in java >
/** Copyright (c), AnkitMittal  JavaMadeSoEasy.com */
public class FindMissingNumberInUnSortedArray2{
   static int ar[]={33,27,5,8,44,24}; //given array
  
   public static void main(String[] args) {
          System.out.print("given array : ");
          for (int j = 0; j < ar.length; j++)
                 System.out.print(ar[j] +" "); // display it
         
          displayMissing();      
   }
  
   /*
   * Method for displaying missing number in given range.
   */
   static public void displayMissing(){
         
          int tempAr[]=new int[100];
          for (int j = 0; j < ar.length; j++){ /*make indexes of tempAr corresponding to value found in ar equal to 1.
                                                                              i.e if i[0]=22, than make tempAr[22]=1;
                                                                              if i[1]=25, than make tempAr[25]=1; */
                 tempAr[ar[j]]=1;
          }
         
          System.out.print("\nNumbers missing between 1 to 100 in array :  ");
         
          for(int i=1;i<tempAr.length;i++){
                 if(tempAr[i]==0)
                       System.out.print(i+" ");
          }
          System.out.println(tempAr.length);
   }
  
  
}
/*OUTPUT
given array : 33 27 5 8 44 24
Numbers missing between 1 to 100 in array :  1 2 3 4 6 7 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 25 26 28 29 30 31 32 34 35 36 37 38 39 40 41 42 43 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
*/


Previous program                                                                  Next program




Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.




RELATED LINKS>









eEdit
Must read for you :