forked from Algo-Phantoms/Algo-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Repeating_And_Missing_Number.java
87 lines (70 loc) · 2.4 KB
/
Repeating_And_Missing_Number.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*Repeating And Missing Numbers :-
Given an array of size n.
Array elements are in the range from 1 to n. One number from set {1, 2, …n} is missing and one number occurs twice in the array.
we are going to find these two numbers.
Algorithm
This method involves creating a Hashset with the help of Sets. In this, the elements are assigned to their natural index. In this process,
if an element is assigned twice, then it is the repeating element. And if an element’s assigning is not there, then it is the missing element.
*/
package reapeating_and_mising_number;
import java.io.*;
import java.util.*;
public class Repeating_And_Missing_Number {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Number of Intergers : ");
//Taking the input of n
int n = Integer.parseInt(br.readLine());
int[] a;
a = new int[n];
for(int i = 0; i < n; i++) {
a[i] = Integer.parseInt(br.readLine());
}
int[] ans = findTwoElement(a, n);
System.out.println("Repeating Number"+" "+ans[0] + " " +"Missing Number"+" "+ ans[1]);
}
public static int[] findTwoElement(int arr[], int n) {
Set<Integer> numSet = new HashSet<Integer>();
int[] a = {0,0};
for(int i = 0; i < n; i++) {
//Checking if numset contains that element
if(!numSet.contains(arr[i]))
{
//if condition gets false then adding the element to numset
numSet.add(arr[i]);
} else {
//assigning value to a[0](repeating number)
a[0] = arr[i];
}
}
for(int num = 1; num <= n; num++) {
//checking for missing number
if(!numSet.contains(num))
{
a[1] = num;
}
}
return a;
}
}
/*
Test Cases
Input : Enter the Number of Intergers:
4
1
2
2
4
Output : Repeating Number 2 Missing Number 3
Input : Enter the Number of Intergers:
5
1
2
2
4
5
Output : Repeating Number 2 Missing Number 3
TIME COMPLEXITY AND SPACE COMPLEXITY
Time Complexity :- O(n), Where n is the size of array.
Space Complexity :- O(1)
*/