# Find Missing Number From Series with java

View: 360    Dowload: 0   Comment: 0   Post by: hanhga   Category: Javascript   Fields: Other

This puzzle has been asked. 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.

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 in 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.

Happy Learning !!

This puzzle has been asked. 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.

Posted on 17-08-2016

#### Comment:

To comment you must be logged in members.

## Files with category

• ### Mini Youtube Using ReactJS

Category: Javascript     Fields: none

This is one the best starter for ReactJS. MiniYoutube as the name suggests is a youtube like website developed using reactJS and youtube API. This project actually let's you search , play and list youtube videos. Do check it out and start learning...

• ### AngularJS and REST API

Category: Javascript     Fields: Other

This is a tutorial for those interested in a quick introduction to AngularJS and REST API. We will build the familiar Periodic Table of the Elements found in every chemistry textbook, and allow the user to select a Chemical Element by clicking on...

• ### Collective Intelligence, Recommending Items Based on Similar Users' Taste

Category: Javascript     Fields: Other

Using Collaborative Filtering to find people who share tastes, and for making automatic recommendations based on things that other people like.

• ### Think Like a Bird for Better Parallel Programming

Category: Javascript     Fields: Other

Coding an application to run in parallel is hard, right? I mean, it must be hard or we’d see parallel programs everywhere. All we'd see are slick parallel apps that use every available core effortlessly. Instead multi-threaded apps are the exception...

• ### Getting Started with the Bing Search APIs

Category: Javascript     Fields: Other

Bing Search API is a set of REST interfaces that find web pages, news, images, videos, entities, related searches, spelling corrections, and more in response to queries from any programming language that can generate a web request. Applications that...

• ### Brief Introduction of SocketPro High Performance and Scalable Persistent Message Queue

Category: Javascript     Fields: Other

Continuous in-line request/result batching, real-time stream sending/processing, asynchronous data transferring and parallel computation for best performance and scalability

• ### Iteration Over Java Collections with High Performance

Category: Javascript     Fields: Other

Java developers usually deal with Collections such as ArrayList, HashSet, Java 8 come with lambda and streaming API helps us to easily work with Collections. In most cases, we work with few thousands of items and performance isn't a concern. But in...

• ### SR2JLIB - A Symbolic Regression Library for Java

Category: Javascript     Fields: Other

Grammar-Guided Genetic Programming library featuring: multi-threading, just-in-time compilation of individuals, dynamic class loading, and JNI interfacing with C/C++ code

File suggestion for you
• Javascript - 386 view We all know what Serializable interface guarantees i.e. ability to serialize the classes. This interface recommends you to useserialVersioUID also....

• WordPress template - 737 view Chances are that you want to set some kind of minimum requirements on your WooCommerce store before your customers checkout.

• Flash template, Flash game - 659 view In this Quick Tip you will learn how to use BitmapData's copyPixels() method to create a very fast blurry trail effect for the bullets in your...

• Web Development - 222 view Speech to Text Recognition for Data Scraping and Collection in Data Mining

• Visual studio & .NET - 745 view This is a simple application that will teach you on how to create a screen resolution detection program using visual basic.NET framework. It...

Mega tags
• Sharepoint - 999 view According to Microsoft, "Microsoft® Content Management Server (MCMS)...

• References - 1994 view In the process of web design, which makes it the most headaches is how...

• Javascript - 309 view When I began searching for my first job as a web developer, I applied...

• HTML-CSS template - 1697 view Clipping path is the path we use to clip an element, it marks out our...

• Javascript - 919 view In the course of my website, still regularly meet the requirements for...

• Mobile applications - 151 view This application shows you how to create an E-post office where you...

• Javascript - 311 view In previous tutorial, we learned about four persistent states of...

• HTML-CSS template - 6417 view Prior to HTML5 we had no element to semantically mark up a date or...

• HTML-CSS template - 629 view There are times, however, when a lightweight carousel with minimal...

• HTML-CSS template - 1046 view Share Template HTML&CSS Artificial Casting- By Piyush