-
Notifications
You must be signed in to change notification settings - Fork 360
/
FindMissingAndRepeating .java
41 lines (37 loc) · 1.13 KB
/
FindMissingAndRepeating .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
package com.sanket.Array;
import java.util.Scanner;
/*
Problem Link - https://practice.geeksforgeeks.org/problems/find-missing-and-repeating/0
Status - Accepted
*/
public class FindMissingAndRepeating {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while (t-- != 0) {
int n = scanner.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
int duplicate = -1;
for (int i = 0; i < n; i++) {
int current = Math.abs(arr[i]) - 1;
if (arr[current] < 0) {
duplicate = current + 1;
}
else {
arr[current] = - arr[current];
}
}
int missing = -1;
for (int i = 0; i < n; i++) {
if (arr[i] > 0) {
missing = i + 1;
break;
}
}
System.out.println(duplicate + " " + missing);
}
}
}