How to find multiple missing number in integer array of 1 to 100 with output?

Category : Java | Sub Category : Java Programs | By Prasad Bonam Last updated: 2023-08-14 12:30:10 Viewed : 484


How to find multiple missing number in integer array of 1 to 100 with output?

To find multiple missing numbers in an integer array containing numbers from 1 to 100, you can modify the approach slightly. Instead of subtracting the sum of the arrays elements from the expected sum of the series, you can compare each individual element to see which numbers are missing.

Here is a Java program that demonstrates how to find multiple missing numbers:

java
import java.util.ArrayList; import java.util.List; public class MultipleMissingNumbersInArray { public static void main(String[] args) { int[] array = {1, 2, 3, 5, 7, 10, /* ... (up to 100) */ 94, 95, 97, 98}; List<Integer> missingNumbers = findMissingNumbers(array); System.out.println("The missing numbers are: " + missingNumbers); } public static List<Integer> findMissingNumbers(int[] arr) { int n = 100; // Highest number in the series boolean[] present = new boolean[n + 1]; // Index 0 is unused for (int num : arr) { present[num] = true; } List<Integer> missingNumbers = new ArrayList<>(); for (int i = 1; i <= n; i++) { if (!present[i]) { missingNumbers.add(i); } } return missingNumbers; } }

Output:

less
The missing numbers are: [4, 6, 8, 9, 11, 12, ... , 93, 96, 99, 100]

In this example, the program uses a boolean array present to mark the presence of each number in the range from 1 to 100. It iterates through the given array and marks the corresponding indices as present. Then, it scans the present array to find the indices that are still marked as false, which correspond to the missing numbers.

The output shows a list of missing numbers from the array. Please note that in a real program, you might want to consider more efficient algorithms for handling large ranges of numbers or large arrays.


Search
Related Articles

Leave a Comment: