Find the only one occurring odd and find the missing terms. Important GFG
package com.company;
public class CWR_oddNumberArrayContainer {
// my method Time complexity -n*n
public static void count(int[] arr) {
for (int i = 0; i < arr.length; i++) {
int count = 0;
for (int j = 0; j < arr.length; j++) {
if (arr[i] == arr[j]) {
count++;
}
}
if (count % 2 != 0) {
System.out.println(arr[i]);
}
}
}
// Method 2
public static int findOdd(int[] arr, int n) {
int res = 0;
for (int i = 0; i < n; i++) {
res = res ^ arr[i];
}
return res;
}
// Find the missing number
public static int findMissingNo(int[] arr, int n) {
int res = 0;
for (int i = 0; i < n; i++) {
res = res ^ arr[i];
}
for(int j = 1; j <= n+1; j++){
res = res ^ j;
}
return res;
}
public static void main(String[] args) {
int[] a = {1, 2, 3, 5, 6, 7, 8, 9, 10};
count(a);
// System.out.println(findOdd(a, 5));
// System.out.println(findMissingNo(a, 9));
}
}
Comments
Post a Comment