Last active
August 16, 2020 11:15
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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