Java Program to Find the Missing Element in a Given Permutation26 Mar 2025 | 3 min read The task is to determine the missing number from the given sequence. The array will contain all numbers in this range except for one. Approaches to Find the Missing NumberNaive Approach: Using HashingThis approach involves creating an auxiliary array (hashArray) to keep track of the frequency of each element in the input array. The element with a frequency of 0 in this auxiliary array is the missing number. Algorithm
Let's implement the above algorithm in a Java program. ExampleOutput: 4 ExplanationIn the above program, the frequency of each number in the input array is stored in an auxiliary array (hash) of size N+1. We increment the value at the matching index in the hash array as we iterate through the input array. We first fill in the frequency array and then search the hash array for the index that doesn't change to zero. Since it implies that the matching number in the range [1, N] did not appear in the input array, this index denotes the missing number. Efficient Approach: Using Sum FormulaThis approach leverages the formula for the sum of the first N natural numbers: By subtracting the sum of the array elements from this total, we will get the missing number. ExampleOutput: 4 ExplanationFirst, the formula is used to get the total sum for the range [1, N]. Next, the input array's total element count is calculated. The value that should have been in the array is not represented by the difference between these two sums that provide the missing number. Efficient Approach: Using XOR OperationWhen a number is XORed with itself, the result is 0, and when a number is XORed with 0, it gives the number itself. ExampleOutput: 4 ExplanationTo obtain xorTotal, first XOR every number from 1 to N. Get xorArray by XORing every element in the input array. You may find the missing number by XORing xorTotal and xorArray. All other digits cancel out, leaving only the missing number. ConclusionThe naive approach involves using an auxiliary array, while more efficient methods use the sum formula or XOR operation. The latter approaches are preferable for their O(1) space complexity and straightforward implementation, making them suitable for larger datasets. Next TopicJava Xmx |
Cryptosystem is responsible for encrypting the user's data and provide a secure mechanism to store it in a virtual drive. The virtual drive will be created by the system for the particular user for the very first while using the system. The system provides limited storage...
6 min read
in Thread is a part of multithreading programming. In order to write a code for printing even and odd using 2 threads, it is required that we should have knowledge of multithreading. Now, we need to print the even and odd numbers in natural order up to...
11 min read
A Y-shaped linked list is a kind of linked list in which two distinct linked lists meet at a shared intersection point. In this Java program, we illustrate how to determine the intersection point at which two linked lists converge. The approach consists of going through...
14 min read
Printing numbers from N down to 1 without loop is a good exercise for understanding recursion in Java, just replace loop counter i with a parameter of the recursive function. On average, to accomplish repetitive jobs like counting down, you have to use loops. However, recursion does...
4 min read
Spring and Struts are both popular Java frameworks used for developing web applications. Spring is a lightweight and flexible framework that provides a comprehensive solution for building enterprise-level applications. It offers dependency injection, aspect-oriented programming, and integration with Hibernate and JPA. Spring promotes a modular and...
2 min read
The 3N+1 problem is an abstract mathematical problem that is a conjecture (not yet proven). It is also known as Collatz problem. In this section, we will discuss the 3N+1 problem along with its Java program. The task is to write a Java program that will read...
3 min read
String compression is a fundamental problem in computer science and programming, where the objective is to compress a string by counting consecutive repeated characters. The problem's essence is to represent strings more efficiently, especially when dealing with large datasets. This technique is beneficial in various...
7 min read
? Linked lists are fundamental data structures that store and manipulate data efficiently in computer programming. They consist of nodes, each containing data and a reference to the node in the list. In this article, we will explore various operations on a linked list in Java,...
6 min read
In Java, package plays an important role in enting naming conflicts, controlling access, and making searching and usage of classes, enumeration, interfaces, and annotation easier. In order to group classes, interfaces, and sub-packages that are related to each other, we use packages. By using packages: It is very...
3 min read
Java programming is used by many users worldwide. It provides numerous packages to solve different problems. To use the Java packages in our programs, import keyword is used. In this section, we will discuss about static import in Java. Java import Keyword Most of the Java programs starts...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India