Game with nos | Practice | GeeksforGeeks

You are given an array arr[], you have to re-construct an array arr[].
The values in arr[] are obtained by doing Xor of consecutive elements in the array.
Example 1:
â€‹Input : arr[ ] = {10, 11, 1, 2, 3}
Output : 1 10 3 1 3
Explanation:
At index 0, arr[0] xor arr[1] = 1
At index 1, arr[1] xor arr[2] = 10
At index 2, arr[2] xor arr[3] = 3
...
At index 4, No element is left So, it will remain as
it is.
New Array will be {1, 10, 3, 1, 3}.


â€‹Example 2:
Input : arr[ ] = {5, 9, 7, 6} 
Output :  12 14 1 6 
 
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function game_with_number() that takes an array (arr), sizeOfArray (n), and return the array re-constructed array arr. The driver code takes care of the printing.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
 
Constraints:
1 ≤ N ≤ 105
1 ≤ arr[i] ≤ 107
 
+1
harshscode5 days ago
 for(int i=0;i<n-1;i++)   {       a[i]=a[i]^a[i+1];   }   return a;
+1
zerefkhan2 weeks ago
C++ solution
Total Time Taken:
0.01/1.12
int* game_with_number(int arr[], int n)
{
    for(int i=0; i<n-1; i++){
        arr[i] = arr[i] ^ arr[i+1];
    }
    return arr;
}

0
krinabhalodiya24832 weeks ago
/*JAVA code*/
class Complete{   // Function for finding maximum and value pair   public static int[] game_with_number (int arr[], int n) {       for(int i=0;i<n-1;i++){           arr[i]=arr[i]^arr[i+1];             }       return arr;   }} 
0
asifsaba5812 weeks ago
int* game_with_number(int arr[], int n)
{
    
    //dry run for example 1;
    
    for(int i=0;i<n-1;i++){
        
        
        
        arr[i]=arr[i] ^ arr[i+1];
        
 //arr[1]=arr[10] ^ arr[11] //00001010^00001011 = 00000001(1)
 
 //arr[10]=arr[11] ^ arr[1] //00001011^00000001 = 00001010(10)
 
 //arr[3]=arr[1] ^ arr[2] //00000001^00000010 = 00000011(3)
 
 //arr[1]=arr[2] ^ arr[3] //00000010^00000011 = 00000001(1)
 
 //arr[3] // last element as it is.
    }
    
    return arr; //[1,10,3,1,3];
    
}

0
arvind16yadav3 weeks ago
/*JAVA CODE*/
public static int[] game_with_number (int arr[], int n) {       // Complete the function              for(int i=0; i<n-1; i++){           arr[i] = arr[i]^arr[i+1];       }       return arr;   }
0
rahulp00rtx1 month ago
Simple JavaScript Code
class Solution {   game_with_number(arr,n){      //code here      for(let i = 0; i<n; i++){          arr[i] = arr[i] ^ arr[i+1];      }      return arr;   }}
0
hasnainraza1998hr1 month ago
int* game_with_number(int arr[], int n){   for(int i=0;i<n-1;i++){       arr[i] = arr[i] ^ arr[i+1];   }   return arr;}
0
shivanshixa312 months ago
int* game_with_number(int arr[], int n)
{
    // Complete the function
    for(int i=0;i<n-1;i++){
        arr[i] = arr[i] ^ arr[i+1];
    }
    return arr;
}
+1
sharadkm19972 months ago
for i in range(n-1):       arr[i]=arr[i]^arr[i+1]   return arr  
0
ankur02sarkar3 months ago
int* game_with_number(int arr[], int n)
{
    for(int i = 1; i<n; i++){
        arr[i-1]=arr[i-1]^arr[i];
    }
    return arr;
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.