
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Write a program in Java to find the missing positive number in a given array of unsorted integers
Let’s suppose we have given an array of unsorted integers. The task is to find the positive missing number which is not present in the given array in the range [0 to n]. For example,
Input-1 −
N = 9 arr = [0,2,5,9,1,7,4,3,6]
Output −
8
Explanation − In the given unsorted array, ‘8’ is the only positive integer that is missing, thus the output is ‘8’.
Input-2 −
N = 1 arr = [0]
Output −
1
Explanation − In the given array, ‘1’ is the only one positive integer which is missing, thus the output is ‘1’.
Approach to solve this problem
There are several approaches to solve this particular problem. However, we can solve this problem in linear time O(n) and constant space O(1).
Since we know that our array is of size n and it contains exactly elements in the range of [0 to n]. So, if we do XOR operation of each of the elements and its index with ‘n’, then we can find the resultant number as a unique number that is missing from the array.
Take Input of N size of the array with elements in the range [0 to n].
An integer function findMissingNumber(int arr[], int size) takes an array and its size as input and returns the missing number.
Let’s take n as a missing number to perform XOR operation.
Iterate over all the array elements and perform XOR operation with each of the array elements and its indexes with respect to missing number, i.e., n
Now return the missing number.
Example
public class Solution { public static int findMissingNumber(int arr[], int size){ int missing_no= size; for(int i=0;i<size;i++){ missing_no^= i^arr[i]; } return missing_no; } public static void main(String[] args){ int arr[] = {0,4,2,1,6,3}; int n = arr.length; int a=findMissingNumber(arr, n); System.out.println(a); } }
Output
Running the above code will generate the output as,
5
In the given array {0,4,2,1,6,3}, ‘5’ is missing, thus we will return 5.
- Related Articles
- Write a program in C++ to find the missing positive number in a given array of unsorted integers
- Find the Smallest Positive Number Missing From an Unsorted Array
- Write a program in C++ to find the maximum and second maximum in a given unsorted array of integers
- Java Program to sort integers in unsorted array
- Program to find kth missing positive number in an array in Python
- Write a program in C++ to find the most frequent element in a given array of integers
- How to find the missing number in a given Array from number 1 to n in Java?
- Write a program in Python to find which column has the minimum number of missing values in a given dataframe
- Finding the largest and smallest number in an unsorted array of integers in JavaScript
- Java program to find if the given number is positive or negative
- Write a program in C++ to find the top K frequent element in an array of integers
- PHP program to find the numbers within a given array that are missing
- Write a Golang program to find duplicate elements in a given array
- Java program to create a sorted merged array of two unsorted arrays
- Write a program to find the first non-repeating number in an integer array using Java?
