HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Puzzles / Find Missing Number From Series/Array in Java

Find Missing Number From Series/Array in Java

A commonly asked puzzle at Java interviews is – find the missing number from a series or array of numbers. This puzzle has been asked in Amazon.com.

In this Java puzzle, you have a series of numbers start (e.g. 1….N) and exactly one number in this series is missing. You have to write a java program to find missing number from series.

Solution to find mising number

Surprisingly, solution of this puzzle is very simple only if you know it already.

  1. Calculate A = n (n+1)/2 where n is largest number in series 1…N.
  2. Calculate B = Sum of all numbers in given series
  3. Missing number = A – B

Let’s a write the solution in code.

public class FindMissingNumber {
	public static void main(String[] args) {
		//10 is missing
		int[] numbers = {1,2,3,4,5,6,7,8,9, 11,12};
		
		int N = 12;
		int idealSum = (N * (N + 1)) / 2;
		int sum = calculateSum(numbers);

		int missingNumber = idealSum - sum;
		System.out.println(missingNumber);
	}

	private static int calculateSum(int[] numbers) {
		int sum = 0;
		for (int n : numbers) {
			sum += n;
		}
		return sum;
	}
}

Output:

10

Solution to find mising number – Java 8

Above code, though simple, can be reduced by many lines using new language features such as lambda in Java 8. Let’s see how?

import java.util.Arrays;

public class FindMissingNumber {
	public static void main(String[] args) {
		//10 is missing
		int[] numbers = {1,2,3,4,5,6,7,8,9, 11,12};
		
		int N = 12;
		int idealSum = (N * (N + 1)) / 2;
		int sum = Arrays.stream(numbers).sum();

		int missingNumber = idealSum - sum;
		System.out.println(missingNumber);
	}
}

Output:

10

Puzzles like these are simple to solve, but it is always useful to know the solution before it is asked in any interview. So be ready to find missing number in array, in your next interview.

Happy Learning !!

Reference: SO Thread

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. apachecn

    June 8, 2020

    it’s better to replace add with xor, never overflow

  2. Santhosh

    January 30, 2020

    how to find multiple numbers missing in a sequence using java 8 streams?

  3. Subelal

    December 26, 2019

    why he is taken N=12 only

    • Lokesh Gupta

      December 27, 2019

      It is the largest number in series.

  4. Random

    August 14, 2019

    How is it coded if there are more numbers like 1 to 200?

Comments are closed on this article!

Search Tutorials

Java Puzzles

  • Interview Puzzles List
  • Dead vs Unreachable Code
  • Palindrome Number
  • Detect infinite loop in LinkedList
  • [i += j] Vs [i = i + j]
  • HiLo Guessing Game
  • Find All Distinct Duplicate Elements
  • TreeMap Put Operation
  • String With Nth Longest Length
  • Good String Vs Bad String
  • Complete String
  • Reverse String
  • Calculate Factorial
  • FizzBuzz Solution
  • Find Missing Number From Series
  • Create Instance Without New Keyword

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)