In the previous article, we have seen Java Program to Find the Common Strings in Two String Arrays
In this article we will see how to find a missing number in an array using Java Language.
Java Program to Find a Missing Number in an Array
Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to find a missing number in an array.
Method-1: Java Program to Find a Missing Number in an Array By Using summation formula (Static Input)
Approach:
- Static array taken.
- Calculate the sum of first n natural numbers as
arraySum= n*(n+1)/2 - Traverse the array from start to end.
- Update the value of sum as
arraySum -= array[i] - Return the variable
arraySum.
Program:
public class MissingNum
{
//Driver method
public static void main(String[] args)
{
//An array declared and initialized
int[] arr = {1, 2, 4, 5, 6, 7};
//findMissing() method called
System.out.println("The missing number is : "+ findMissing(arr));
}
//findMissing() user defined method
//it willfind the missing number
public static int findMissing(int arr[])
{
// calculating the sum upto n integers
int arraySum = (arr[arr.length-1] * (arr[arr.length-1] + 1)) / 2;
for (int i : arr)
{
arraySum -= i;
}
return arraySum;
}
}
Output: The missing number is : 3
Method-2: Java Program to Find a Missing Number in an Array By Using summation formula (Dynamic Input)
Approach:
- Dynamic array taken.
- Calculate the sum of first n natural numbers as
arraySum= n*(n+1)/2 - Traverse the array from start to end.
- Update the value of sum as
arraySum -= array[i] - Return the variable
arraySum.
Program:
import java.util.*;
public class MissingNum
{
//Driver method
public static void main(String[] args)
{
//Scanner class object created
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements in the array : ");
int n = sc.nextInt();
int[] arr = new int[n];
// initializing the array elements
System.out.println("Enter Array elements : ");
for (int i = 0; i < n; i++)
{
arr[i] = sc.nextInt();
}
//calling the findMissing() user defined method
System.out.println("Missing number is : "+findMissing(arr));
}
//findMissing() user defined method
//it willfind the missing number
public static int findMissing(int arr[])
{
// calculating the sum upto n integers
int arraySum = (arr[arr.length-1] * (arr[arr.length-1] + 1)) / 2;
for (int i : arr)
{
arraySum -= i;
}
return arraySum;
}
}
Output: Enter number of elements in the array : 5 Enter Array elements : 1 2 4 5 6 Missing number is : 3
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Related Java Programs:
- Java Program to Find All Pairs of Elements in an Array Whose Sum is Equal to a Specified Number
- Java Program to Find All Pairs of Elements in an Array Whose Product is Equal to a Specified Number
- Java Program to Check if Two Arrays are Equal or Not
- Java Program to Check if One Array is Subset of Another Array or Not