Java Program To Find Missing Number In An Array
=========================================================================
Here is the Some Criteria:
1) Array elements need not sorted but strictly should under some specific bound.
2) No Duplicate Elements are allowed in the Array.
3) You Can Read Array Size And Array Elements from the console(User) and do the operation on it.
How you can Read Elements from the Array is mentioned in the following link. You can check it out here.
public class FindMissingNumberInArray {
public static void main(String[] args) {
int a[] = { 1, 2, 3, 5 }; // Defining, Declaring and Assigning Array In One Place
// Add All the Elements From An Array And store in sum1 variable
// First for loop is used to add elements from An Array
Integer sum1 = 0;
for (int i = 0; i < a.length; i++) {
sum1 = sum1 + a[i];
}
// Second Loop is used to add all elements from an array with bound limit
Integer sum2 = 0;
for (int i = 0; i <= 5; i++) {
sum2 = sum2 + a[i];
}
//Do substraction of sum1 and sum2 variable to get the result.
Integer sum =0;
sum = sum2 -sum1;
System.out.println("The Element Which is missing an array is:" + sum);
}
}
Comments
Post a Comment