Given an array arr[], modify the array in-place such that each element becomes the bitwise OR of itself and its next element. The last element remains unchanged as it has no next element.
Examples:
Input: arr[] = [10, 11, 1, 2, 3]
Output: 11 11 3 3 3
Explanation:
At index 0, arr[0] or arr[1] = 11
At index 1, arr[1] or arr[2] = 11
At index 2, arr[2] or arr[3] = 3
...
At index 4, no element is left So, it will remain as it is.
The new Array will be [11, 11, 3, 3, 3].
Input: arr[] = [5, 9, 2, 6]
Output: 13 11 6 6
Explanation:
At index 0, arr[0] or arr[1] = 13.
At index 1, arr[1] or arr[2] = 11.
At index 2, arr[2] or arr[3] = 6.
At index 3, no element is left So, it will remain as it is.
The new array will be [13, 11, 6, 6].
Table of Content
[Naive Approach] Using Extra Array - O(n) Time O(n) Space
The idea is to use a temporary array to store
arr[i]|arr[i+1]for each index, keep the last element unchanged, and then copy the result back to the original array.
#include <bits/stdc++.h>
using namespace std;
// function for gamewithnumber
vector<int> gameWithNumber(vector<int> &arr)
{
int n = arr.size();
// Temporary array to store results
vector<int> temp(n);
// Compute OR for adjacent elements
for (int i = 0; i < n - 1; i++)
{
temp[i] = arr[i] | arr[i + 1];
}
// Last element remains same
temp[n - 1] = arr[n - 1];
// Copy back to original array
for (int i = 0; i < n; i++)
{
arr[i] = temp[i];
}
return arr;
}
// Driver Code
int main()
{
int n = 5;
vector<int> arr = {10, 11, 1, 2, 3};
vector<int> result = gameWithNumber(arr);
for (int i = 0; i < n; i++)
{
cout << result[i] << " ";
}
return 0;
}
#include <stdio.h>
// function for gameWithNumber
void gameWithNumber(int arr[], int n)
{
// Temporary array to store results
int temp[n];
// Compute OR for adjacent elements
for (int i = 0; i < n - 1; i++)
{
temp[i] = arr[i] | arr[i + 1];
}
// Last element remains same
temp[n - 1] = arr[n - 1];
// Copy back to original array
for (int i = 0; i < n; i++)
{
arr[i] = temp[i];
}
}
// Driver Code
int main()
{
int n = 5;
int arr[] = {10, 11, 1, 2, 3};
gameWithNumber(arr, n);
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
import java.util.*;
class GfG {
// function for gameWithNumber
public static ArrayList<Integer> gameWithNumber(int[] arr)
{
int n = arr.length;
// Temporary array to store results
ArrayList<Integer> temp
= new ArrayList<>(Collections.nCopies(n, 0));
// Compute OR for adjacent elements
for (int i = 0; i < n - 1; i++) {
temp.set(i, arr[i] | arr[i + 1]);
}
// Last element remains same
temp.set(n - 1, arr[n - 1]);
return temp;
}
// Driver Code
public static void main(String[] args)
{
int n = 5;
int[] arr = { 10, 11, 1, 2, 3 };
ArrayList<Integer> result = gameWithNumber(arr);
for (int i = 0; i < n; i++) {
System.out.print(result.get(i) + " ");
}
}
}
# function for gameWithNumber
def gameWithNumber(arr):
n = len(arr)
# Temporary array to store results
temp = [0] * n
# Compute OR for adjacent elements
for i in range(n - 1):
temp[i] = arr[i] | arr[i + 1]
# Last element remains same
temp[n - 1] = arr[n - 1]
# Copy back to original array
for i in range(n):
arr[i] = temp[i]
return arr
# Driver Code
if __name__ == "__main__":
n = 5
arr = [10, 11, 1, 2, 3]
result = gameWithNumber(arr)
for i in range(n):
print(result[i], end=" ")
using System;
using System.Collections.Generic;
class GfG {
// function for gameWithNumber
public static List<int> gameWithNumber(int[] arr) {
int n = arr.Length;
// Temporary list to store results
List<int> temp = new List<int>(new int[n]);
// Compute OR for adjacent elements
for (int i = 0; i < n - 1; i++) {
temp[i] = arr[i] | arr[i + 1];
}
// Last element remains same
temp[n - 1] = arr[n - 1];
return temp;
}
// Driver Code
static void Main()
{
int n = 5;
int[] arr = { 10, 11, 1, 2, 3 };
List<int> result = gameWithNumber(arr);
for (int i = 0; i < n; i++) {
Console.Write(result[i] + " ");
}
}
}
// function for gameWithNumber
function gameWithNumber(arr)
{
let n = arr.length;
// Temporary array to store results
let temp = new Array(n);
// Compute OR for adjacent elements
for (let i = 0; i < n - 1; i++) {
temp[i] = arr[i] | arr[i + 1];
}
// Last element remains same
temp[n - 1] = arr[n - 1];
// Copy back to original array
for (let i = 0; i < n; i++) {
arr[i] = temp[i];
}
return arr;
}
// Driver code
let n = 5;
let arr = [ 10, 11, 1, 2, 3 ];
let result = gameWithNumber(arr);
for (let i = 0; i < n; i++) {
console.log(result[i]);
}
Output
11 11 3 3 3
Time Complexity: O(n)
Space Complexity: O(n),extra space used
[Expected Approach] In-Place - O(n) Time O(1) Space
The idea is to traverse the array from left to right and modify the array in-place. Since we traverse from left to right, we do not need the previous value of an updated item, so need of an extra array.
Working of Approach:
- Traverse from
0ton-2 - Update:
arr[i] = arr[i] | arr[i+1] - Leave last element unchanged
#include <bits/stdc++.h>
using namespace std;
// function for gamewithnumber
vector<int> gameWithNumber(vector<int> &arr)
{
int n = arr.size();
// Traverse till second last element
for (int i = 0; i < n - 1; i++)
{
// Replace current element with OR
// of itself and next element
arr[i] = arr[i] | arr[i + 1];
}
// Last element remains unchanged
return arr;
}
// Driver Code
int main()
{
int n = 5;
vector<int> arr = {10, 11, 1, 2, 3};
vector<int> result = gameWithNumber(arr);
for (int i = 0; i < n; i++)
{
cout << result[i] << " ";
}
return 0;
}
#include <stdio.h>
// function for gamewithnumber
void gameWithNumber(int arr[], int n)
{
// Traverse till second last element
for (int i = 0; i < n - 1; i++)
{
// Replace current element with OR of itself and next element
arr[i] = arr[i] | arr[i + 1];
}
}
// Driver Code
int main()
{
int n = 5;
int arr[] = {10, 11, 1, 2, 3};
gameWithNumber(arr, n);
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
import java.util.*;
class GfG {
// function for gamewithnumber
public static ArrayList<Integer>
gameWithNumber(int[] arr)
{
int n = arr.length;
// Convert array to ArrayList for in-place updates
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
list.add(arr[i]);
}
// Traverse till second last element
for (int i = 0; i < n - 1; i++) {
// Replace current element with OR of itself and
// next element
list.set(i, list.get(i) | list.get(i + 1));
}
// Last element remains unchanged
return list;
}
public static void main(String[] args)
{
int n = 5;
int[] arr = { 10, 11, 1, 2, 3 };
ArrayList<Integer> result = gameWithNumber(arr);
for (int i = 0; i < n; i++) {
System.out.print(result.get(i) + " ");
}
}
}
# Python implementation of above approach
class Solution:
# function for gamewithnumber
def gameWithNumber(self, arr):
n = len(arr)
# Traverse till second last element
for i in range(n - 1):
# Replace current element with OR of itself and next element
arr[i] = arr[i] | arr[i + 1]
# Last element remains unchanged
return arr
# Driver Code
if __name__ == "__main__":
n = 5
arr = [10, 11, 1, 2, 3]
obj = Solution()
result = obj.gameWithNumber(arr)
for i in range(n):
print(result[i], end=" ")
using System;
using System.Collections.Generic;
class GfG {
// function for gameWithNumber
public static List<int> gameWithNumber(int[] arr)
{
int n = arr.Length;
// Temporary list to store results
List<int> temp = new List<int>(new int[n]);
// Traverse till second last element
for (int i = 0; i < n - 1; i++) {
// Replace current element with OR of itself and next element
temp[i] = arr[i] | arr[i + 1];
}
// Last element remains unchanged
temp[n - 1] = arr[n - 1];
return temp;
}
static void Main()
{
int n = 5;
int[] arr = { 10, 11, 1, 2, 3 };
List<int> result = gameWithNumber(arr);
for (int i = 0; i < n; i++) {
Console.Write(result[i] + " ");
}
}
}
class Solution {
// function for gamewithnumber
gameWithNumber(arr)
{
let n = arr.length;
// Traverse till second last element
for (let i = 0; i < n - 1; i++) {
// Replace current element with OR of itself and
// next element
arr[i] = arr[i] | arr[i + 1];
}
// Last element remains unchanged
return arr;
}
}
// driver code
let n = 5;
let arr = [ 10, 11, 1, 2, 3 ];
let obj = new Solution();
let result = obj.gameWithNumber(arr);
for (let i = 0; i < n; i++) {
console.log(result[i] + " ");
}
Output
11 11 3 3 3
Time Complexity: O(n)
Auxiliary Space: O(1)