-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitwise-XOR-Equalization.js
More file actions
83 lines (69 loc) · 3.45 KB
/
Copy pathBitwise-XOR-Equalization.js
File metadata and controls
83 lines (69 loc) · 3.45 KB
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
let testCases = prompt("Enter the number of test cases: ");
// Validate testCases input, ensuring it's within the allowed range.
while (testCases < 1 || testCases > Math.pow(10, 4)) {
testCases = prompt("Invalid value, try again: ");
}
let sumOfN = 0; // Track the sum of all integers across test cases.
for (let j = 1; j <= testCases; j++) {
let numberOfIntegers = prompt("Enter the number of integers: ");
// Validate numberOfIntegers input.
while (numberOfIntegers < 1 || numberOfIntegers > Math.pow(10, 5)) {
numberOfIntegers = prompt("Invalid value, try again: ");
}
sumOfN += numberOfIntegers; // Accumulate the total count of numbers processed.
// Ensure the total number of integers does not exceed the maximum allowed limit.
if (sumOfN > 5 * Math.pow(10, 5)) {
console.log("The sum of Number of integers over all test cases exceeded the maximum limit.")
break;
} else {
let integerX = prompt("Enter an integer that denotes the bitwise xor operation: ");
// Validate integerX input.
while (integerX < 0 || integerX > Math.pow(10, 9)) {
integerX = prompt("Invalid value, try again: ");
}
let array = []; // Array to store input values.
// Read and validate each array element.
for (let i = 0; i < numberOfIntegers; i++) {
array[i] = prompt("Enter array values: ");
while (array[i] < 1 || array[i] > Math.pow(10, 9)) {
array[i] = prompt("Invalid value, try again: ");
}
}
let finalEqualCounter = 1; // Tracks maximum equal elements count.
let finalOperationCounter = 0; // Tracks minimum operations needed.
// Iterate through each element to find the maximum count of equal values.
for (let i = 0; i < numberOfIntegers; i++) {
let currentEqualCounter = 1; // Count of elements equal to array[i].
let currentOperationCounter = 0; // Number of operations needed.
// Compare array[i] with every other element.
for (let k = 0; k < numberOfIntegers; k++) {
if (k !== i) {
if (array[i] == array[k]) {
currentEqualCounter++; // Direct match found.
} else {
let xorValue = array[i] ^ integerX; // Compute XOR transformation.
if (array[k] == xorValue) {
currentEqualCounter++; // Match found after XOR.
currentOperationCounter++; // Operation counted.
}
}
}
}
// Update the final results based on comparisons.
if (i == 0) {
finalOperationCounter = currentOperationCounter;
finalEqualCounter = currentEqualCounter;
} else {
if (currentEqualCounter > finalEqualCounter || currentEqualCounter == finalEqualCounter) {
finalEqualCounter = currentEqualCounter;
if (currentOperationCounter < finalOperationCounter) {
finalOperationCounter = currentOperationCounter;
}
}
}
}
// Print the results for the current test case.
console.log(finalEqualCounter);
console.log(finalOperationCounter);
}
}