package pep.Day82;
import java.util.Scanner;
public class One_Repeating_and_One_Missing {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
solution(arr);
}
private static void solution(int[] arr) {
int xor_of_array = 0, a = 0, b = 0;
// xor of array and number find krnge
// xor_of_array = a^b
for (int i : arr) {
xor_of_array = xor_of_array ^ i;
}
for (int i = 1; i <= arr.length; i++) {
xor_of_array = xor_of_array ^ i;
}
// aab humein a, b ki value find krne hain
int right_most_set_bit = xor_of_array & (-xor_of_array);
for (int i : arr) {
if ((i & right_most_set_bit) == 0)
a = a ^ i;
else
b = b ^ i;
}
for (int i = 1; i <= arr.length; i++) {
if ((i & right_most_set_bit) == 0)
a = a ^ i;
else
b = b ^ i;
}
// aab missing and repeating print krna hai
for (int i : arr) {
if (i == a) {
System.out.println("Missing Number -> " + b);
System.out.println("Repeating Number -> " + a);
} else if (i == b) {
System.out.println("Missing Number -> " + a);
System.out.println("Repeating Number -> " + b);
}
}
}
}
Monday, May 23, 2022
One Repeating And One Missing
Subscribe to:
Post Comments (Atom)
Diagonal Traversal
eg. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 1 6 11 16 2 7 12 3 8 4 Approach:...
-
https://leetcode.com/problems/reverse-integer/description/ Integer.MAX_VALUE = 2,147,483,647 = 2 31 - 1 Integer.MIN_VALUE = -2,147,483,64...
-
https://leetcode.com/problems/two-sum/description/ Given the constraints: 2 <= nums.length <= 10⁴ -10⁹ <= nums[i] <= 10⁹ -10...
-
For this particular question, we only have the In - region. So, the code looks like : package pep.Day7 ; public class TowerOfHanoi { p...


No comments:
Post a Comment