November 20, 2012

Find Missing number in sequence

Find the Missing sequence number from Array

Description : Find the missing number in sequence while user try to insert n number randomly into n -1 array. The complexity will be minimum







import java.util.Scanner;

/**
 * @author +Jeevanantham R
 *
 */
public class MissingNumber {
  
    int inputArray[] = null;
    int limit = 0;
    private Scanner scanner = null;
  
    private void getInput() {
        scanner = new Scanner(System.in);
        System.out.println(" Enter total number of elements ");
        limit = scanner.nextInt();
        int arraySize = limit-1;
        System.out.println(" Array  size  :"+arraySize );
        inputArray = new int[arraySize];
        for(int i=0; i<arraySize; i++){
            inputArray[i]= scanner.nextInt();
        }
    }
  
    private void findingMissedOne() {
        long startTime = System.currentTimeMillis();
        int actualValue = (limit * (limit + 1))/2;
        int arrayValue = 0;
        for(int i : inputArray){
            arrayValue = arrayValue + i;
        }
        System.out.println(" Missing value (from the randomly inserted array) : "+(actualValue - arrayValue));
        System.out.println(" Execution time :"+(System.currentTimeMillis()- startTime)+" ms");
    }
  
    private void showArray() {
        System.out.print(" Array members  : ");
        for (int i: inputArray) {
            System.out.print(i+"\t");
        }
    }
    public static void main(String[] args) {
      
        MissingNumber missingNumber = new MissingNumber();
        missingNumber.getInput();
        missingNumber.findingMissedOne();
        missingNumber.showArray();
    }
}



Finding the missing number in a sequence
How to find missing number in sequence
My friend had suggested another way to find the missing number in sequence which is much better than the above code. try it and please provide +Akshay Agarwal  a feedback



Find missing number in an unordered sequential array

Akshay Agrawal

import java.io.*;
class MissingNumber
{
public static void main(String args[])throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the array size:");
int n=Integer.parseInt(b.readLine());

int a[]=new int[n];
System.out.println("Enter "+(n-1)+" array elements:");

int sum=0;
for(int i=0;i
{
a[i]=Integer.parseInt(b.readLine());
sum=sum+a[i];
}

int s=n*(n+1)/2;

int missing_number=s-sum;

long startTime = System.currentTimeMillis();
System.out.println("missing number in the from randomly inserted sequence="+missing_number);
System.out.println(" Execution time :"+(System.currentTimeMillis()- startTime)+" ms");

}
}



Thank you akshay agarwal for the above java code

Your Valuable FeedBacks Please

http://belazy.blog.com/

Facebook comments