Friday, 1 February 2013

Java Interview : Insert 1-100 numbers in an array of 99 cell and find out the missing number


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package arrayof99cells;

import java.io.IOException;

/**
 *
 * @author Sunshine
 */
public class Arrayof99cells {
    int FindMinssing(int[] array){
        int sum=0;
        int sum_of_n_natural_nums=((array.length+2)*(array.length+1))/2;
        System.out.println(array.length);
        for(int i=0;i<array.length;i++){
            sum_of_n_natural_nums-=array[i];
        }
        return (sum_of_n_natural_nums);
       
    }
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        int[] arrayof99cells=new int[]{1,2,3,5};
        Arrayof99cells a=new Arrayof99cells();
        int missing_number=a.FindMinssing(arrayof99cells);
        System.out.println(missing_number);
       
       
    }
}

No comments:

Post a Comment