Skip to content

Instantly share code, notes, and snippets.

@vinodjayachandran
Last active August 16, 2020 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinodjayachandran/32d376031c4d8f34f595eada1cd7e8eb to your computer and use it in GitHub Desktop.
Save vinodjayachandran/32d376031c4d8f34f595eada1cd7e8eb to your computer and use it in GitHub Desktop.
Missing number in array - Given an array C of size N-1 and given that there are numbers from 1 to N with one element missing, the missing number is to be found.
import java.util.Scanner;
public class MissingNumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Please enter the value of N");
int N = Integer.parseInt(input.nextLine());
int expectedSum = (N * (N+1))/2;
System.out.println("Enter Input Array");
String [] numArray = input.nextLine().split(" ");
int sum = 0;
for (int i = 0; i < numArray.length; i++) {
int number = Integer.parseInt(numArray[i]);
sum = sum + number;
}
System.out.println(" Missing Number is " + (expectedSum-sum) );
input.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment