Java

Find Two Missing Numbers in Java1 min read

How do you find two missing numbers in a sequence? How do you find the missing number in a sequence? How do you find the missing number in an array?

Write an algorithm to find two Missing Numbers in a Sequence of Consecutive Numbers

Input:  Array, arrA[] with two missing numbers and Range




Output : Two missing numbers

Approach:

    • Approach is very simple, Add all the given numbers say S
    • Calculate sum of N numbers by formula n(n+1)/2 , say N
    • Find sum of two missing numbers a+b = N-S
  • Now take the product of all given numbers say P
  • Now take the product of N numbers , say Np;
  • Find the product of two missing numbers ab = Np-P
  • Now we have a+b and ab , we can easily calculate a and b

Java Code Example:

Output:

 

Source: algorithms.tutorialhorizon.com/algorithms-find-two-missing-numbers-in-a-sequence-of-consecutive-numbers/

Leave a Comment