Skip to content

Commit

Permalink
potd 18 dec updated (cpp)
Browse files Browse the repository at this point in the history
  • Loading branch information
SHRbharat committed Dec 17, 2023
1 parent bd90480 commit 602874b
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 43 deletions.
97 changes: 97 additions & 0 deletions .markdowns/2023/december/18-Game of XOR.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
## GFG Problem Of The Day

### Date - 18 December, 2023
### Ques - [Game of XOR](https://www.geeksforgeeks.org/problems/game-of-xor1541/1)
![](https://badgen.net/badge/Level/Medium/yellow)

Given an array A of size N. The value of an array is denoted by the bit-wise XOR of all elements it contains. Find the bit-wise XOR of the values of all subarrays of A.

### Approach Used
The key observation is that if the frequency of an element in all possible subarrays is even, then XORing it with 0 (even number of times) results in 0. If the frequency is odd, XORing it with the element itself (odd number of times) preserves the element's value.

1. **Initialize Result:**
- Initialize a variable `result` to store the final bitwise XOR of all subarrays. Start it with 0.

2. **Iterate Over Array:**
- Use a loop to iterate through each element of the array `A` from index 0 to `N-1`.

3. **Calculate Frequency:**
- For each element at index `i`, calculate its frequency in all possible subarrays. The frequency is determined by `(i + 1) * (N - i)`, representing the number of subarrays in which the element at index `i` appears.

4. **XOR Operation:**
- If the frequency is even, XOR with 0. This is because XORing an even number of times with the same value results in 0.
- If the frequency is odd, XOR with the element at index `i`.

5. **Repeat for All Elements:**
- Repeat steps 3-4 for all elements in the array.

6. **Return Result:**
- After processing all elements, the `result` variable contains the bitwise XOR of all subarrays. Return this result.

### Example : N = 3 ,A = [1, 2, 3]

1. **Initialize Result:**
- Initialize a variable `result` to 0.

2. **Iterate Over Array:**
- For index 0:
- Calculate frequency = (0 + 1) * (3 - 0) = 3 (odd)
- XOR result ^= 1 (element at index 0)

- For index 1:
- Calculate frequency = (1 + 1) * (3 - 1) = 4 (even)
- XOR result ^= 0 (XOR with 0)

- For index 2:
- Calculate frequency = (2 + 1) * (3 - 2) = 3 (odd)
- XOR result ^= 3 (element at index 2)

3. **Final Result:**
- `result` = 1 ^ 0 ^ 3 = 2

## Explanation:

- For index 0, the element 1 appears in 3 subarrays. XOR with 1 (odd frequency) results in 1.
- For index 1, the element 2 appears in 4 subarrays. XOR with 0 (even frequency) results in 2.
- For index 2, the element 3 appears in 3 subarrays. XOR with 3 (odd frequency) results in 0.

Thus, the final bitwise XOR of all subarrays is 2.

### Time and Auxiliary Space Complexity

- **Time Complexity :** `O(N)` , where N in the size of array
- **Auxiliary Space Complexity :** `O(1)`

### Code (C++)
```cpp
class Solution {
public:
int gameOfXor(int N , int A[]) {
int result = 0;

for (int i = 0; i < N; ++i) {
int freq = (i + 1) * (N - i);
// If frequency is even, XOR is 0.
if (freq % 2 == 0) {
result ^= 0;
}
// If frequency is odd, XOR with the element.
else {
result ^= A[i];
}
}

return result;
}
};
```
### Contribution and Support

If you have a better solution or any queries / discussions , please visit our [discussion section](https://github.com/SHRbharat/gfg_potd/discussions).

If you find this solution helpful, consider supporting us by giving a `⭐ star` to the [SHRbharat/gfg_potd](https://github.com/SHRbharat/gfg_potd) repository.

![Total number of repository visitors](https://komarev.com/ghpvc/?username=SHRbharat&color=blueviolet&&label=Visitors)

[![Total number of repository stars](https://img.shields.io/github/stars/SHRbharat/gfg_potd.svg)](https://github.com/SHRbharat/gfg_potd/stargazers)
110 changes: 67 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,65 +1,89 @@
## GFG Problem Of The Day

### Date - 17 December, 2023
### Ques - [Max Sum without Adjacents](https://www.geeksforgeeks.org/problems/max-sum-without-adjacents2430/1)
![](https://badgen.net/badge/Level/Easy/green)
### Date - 18 December, 2023
### Ques - [Game of XOR](https://www.geeksforgeeks.org/problems/game-of-xor1541/1)
![](https://badgen.net/badge/Level/Medium/yellow)

Given an array Arr of size N containing positive integers. Find the maximum sum of a any possible subsequence such that no two numbers in the subsequence should be adjacent in Arr.
Given an array A of size N. The value of an array is denoted by the bit-wise XOR of all elements it contains. Find the bit-wise XOR of the values of all subarrays of A.

### Approach Used
We have used dynamic programming to compute and store the maximum sum of a subsequence, ensuring that no two numbers in the subsequence are adjacent. The dynamic programming array `dp` is filled iteratively, considering the choice of including or excluding each element in the calculation of the maximum sum.
The key observation is that if the frequency of an element in all possible subarrays is even, then XORing it with 0 (even number of times) results in 0. If the frequency is odd, XORing it with the element itself (odd number of times) preserves the element's value.

### Example: Let's take ` n = 6 ` and `arr = {5,5,10,100,10,5}`
- Initialization:
- `dp[0] = 5` (maximum sum ending at index 0)
- `dp[1] = max(5, 5) = 5` (maximum sum ending at index 1)
1. **Initialize Result:**
- Initialize a variable `result` to store the final bitwise XOR of all subarrays. Start it with 0.

- Iteration (i = 2):
- `dp[2] = max(dp[1], dp[0] + Arr[2]) = max(5, 5 + 10) = 15`
2. **Iterate Over Array:**
- Use a loop to iterate through each element of the array `A` from index 0 to `N-1`.

- Iteration (i = 3):
- `dp[3] = max(dp[2], dp[1] + Arr[3]) = max(15, 5 + 100) = 105`
3. **Calculate Frequency:**
- For each element at index `i`, calculate its frequency in all possible subarrays. The frequency is determined by `(i + 1) * (N - i)`, representing the number of subarrays in which the element at index `i` appears.

- Iteration (i = 4):
- `dp[4] = max(dp[3], dp[2] + Arr[4]) = max(105, 15 + 10) = 105`
4. **XOR Operation:**
- If the frequency is even, XOR with 0. This is because XORing an even number of times with the same value results in 0.
- If the frequency is odd, XOR with the element at index `i`.

- Iteration (i = 5):
- `dp[5] = max(dp[4], dp[3] + Arr[5]) = max(105, 105 + 5) = 110`
5. **Repeat for All Elements:**
- Repeat steps 3-4 for all elements in the array.

6. **Return Result:**
- After processing all elements, the `result` variable contains the bitwise XOR of all subarrays. Return this result.

### Example : N = 3 ,A = [1, 2, 3]

1. **Initialize Result:**
- Initialize a variable `result` to 0.

2. **Iterate Over Array:**
- For index 0:
- Calculate frequency = (0 + 1) * (3 - 0) = 3 (odd)
- XOR result ^= 1 (element at index 0)

- For index 1:
- Calculate frequency = (1 + 1) * (3 - 1) = 4 (even)
- XOR result ^= 0 (XOR with 0)

- For index 2:
- Calculate frequency = (2 + 1) * (3 - 2) = 3 (odd)
- XOR result ^= 3 (element at index 2)

3. **Final Result:**
- `result` = 1 ^ 0 ^ 3 = 2

## Explanation:

- For index 0, the element 1 appears in 3 subarrays. XOR with 1 (odd frequency) results in 1.
- For index 1, the element 2 appears in 4 subarrays. XOR with 0 (even frequency) results in 2.
- For index 2, the element 3 appears in 3 subarrays. XOR with 3 (odd frequency) results in 0.

Thus, the final bitwise XOR of all subarrays is 2.

### Time and Auxiliary Space Complexity

- **Time Complexity :** `O(N)` , where N in the size of array
- **Auxiliary Space Complexity :** `O(n)` , where n is size of dp array
- **Auxiliary Space Complexity :** `O(1)`

### Code (C++)
```cpp
class Solution{
public:
// calculate the maximum sum with out adjacent
int findMaxSum(int *arr, int n) {
// code here
if (n == 0) {
return 0;
}
if (n == 1) {
return arr[0];
}

// Initialize an array to store the maximum sum up to each index
int dp[n];

// Base cases
dp[0] = arr[0];
dp[1] = max(arr[0], arr[1]);

// Fill the dp array using the recurrence relation
for (int i = 2; i < n; i++) {
dp[i] = max(dp[i - 1], dp[i - 2] + arr[i]);
class Solution {
public:
int gameOfXor(int N , int A[]) {
int result = 0;

for (int i = 0; i < N; ++i) {
int freq = (i + 1) * (N - i);
// If frequency is even, XOR is 0.
if (freq % 2 == 0) {
result ^= 0;
}
// If frequency is odd, XOR with the element.
else {
result ^= A[i];
}
}

// Return the maximum sum
return dp[n - 1];
}
return result;
}
};
```
### Contribution and Support
Expand Down
47 changes: 47 additions & 0 deletions cpp_codes/2023/december/18-Game of XOR.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;

// } Driver Code Ends
class Solution {
public:
int gameOfXor(int N , int A[]) {

int result = 0;

for (int i = 0; i < N; ++i) {
int freq = (i + 1) * (N - i);

// If frequency is even, XOR is 0.
if (freq % 2 == 0) {
result ^= 0;
}
// If frequency is odd, XOR with the element.
else {
result ^= A[i];
}
}

return result;
}
};


//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int N;

cin>>N;
int A[N];
for(int i=0 ; i<N ; i++)
cin>>A[i];

Solution ob;
cout << ob.gameOfXor(N,A) << endl;
}
return 0;
}
// } Driver Code Ends

0 comments on commit 602874b

Please sign in to comment.