Generate an array having Bitwise AND of the previous and the next element
Last Updated :
17 Sep, 2022
Given an array of integers arr[] of N elements, the task is to generate another array having (Bitwise) AND of previous and next elements with the following exceptions.
- The first element is the bitwise AND of the first and the second element.
- The last element is the bitwise AND of the last and the second last element.
Examples:
Input: arr[] = {1, 2, 3, 4, 5, 6}
Output: 0 1 0 1 4 4
The new array will be {1 & 2, 1 & 3, 2 & 4, 3 & 5, 4 & 6, 5 & 6}
Input: arr[] = {9, 8, 7}
Output: 8 1 0
Approach: The first and the second element of the new array can be calculated as arr[0] & arr[1] and arr[N - 1] & arr[N - 2] respectively. The rest of the elements can be calculated as arr[i - 1] & arr[i + 1].
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <iostream>
using namespace std;
// Function to generate the array that
// satisfies the given condition
void generateArr(int arr[], int n)
{
// If there is only a single element
// in the array
if (n == 1) {
cout << arr[0];
return;
}
// To store the generated array
int barr[n];
// First element
barr[0] = arr[0] & arr[1];
// Last element
barr[n - 1] = arr[n - 1] & arr[n - 2];
// Rest of the elements
for (int i = 1; i < n - 1; i++)
barr[i] = arr[i - 1] & arr[i + 1];
// Print the generated array
for (int i = 0; i < n; i++)
cout << barr[i] << " ";
}
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
generateArr(arr, n);
return 0;
}
Java
// Java implementation of the approach
import java .io.*;
class GFG
{
static void generateArr(int[] arr, int n)
{
// Nothing to do when array size is 1
if (n <= 1)
return;
// store current value of arr[0]
// and update it
int prev = arr[0];
arr[0] = arr[0] & arr[1];
// Update rest of the array elements
for (int i = 1; i < n - 1; i++)
{
// Store current value of
// next interaction
int curr = arr[i];
// Update current value using
// previous value
arr[i] = prev & arr[i + 1];
// Update previous value
prev = curr;
}
// Update last array element separately
arr[n - 1] = prev & arr[n - 1];
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 4, 5, 6 };
int n = arr.length;
generateArr(arr, n);
// Print the modified array
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
}
// This code is contributed by Nikhil
Python3
# Python3 implementation of the approach
# Function to generate the array that
# satisfies the given condition
def generateArr(arr, n):
# If there is only a single element
# in the array
if (n == 1):
print(arr[0]);
return;
# To store the generated array
barr = [0] * n;
# First element
barr[0] = arr[0] & arr[1];
# Last element
barr[n - 1] = arr[n - 1] & arr[n - 2];
# Rest of the elements
for i in range(1, n - 1):
barr[i] = arr[i - 1] & arr[i + 1];
# Print the generated array
for i in range(n):
print(barr[i], end = " ");
# Driver Code
if __name__ == '__main__':
arr = [1, 2, 3, 4, 5, 6];
n = len(arr);
generateArr(arr, n);
# This code is contributed by 29AjayKumar
C#
// C# implementation of the approach
using System;
class GFG
{
static void generateArr(int[] arr, int n)
{
// Nothing to do when array size is 1
if (n <= 1)
return;
// store current value of arr[0]
// and update it
int prev = arr[0];
arr[0] = arr[0] & arr[1];
// Update rest of the array elements
for (int i = 1; i < n - 1; i++)
{
// Store current value of
// next interaction
int curr = arr[i];
// Update current value using
// previous value
arr[i] = prev & arr[i + 1];
// Update previous value
prev = curr;
}
// Update last array element separately
arr[n - 1] = prev & arr[n - 1];
}
// Driver Code
static public void Main ()
{
int[] arr = { 1, 2, 3, 4, 5, 6 };
int n = arr.Length;
generateArr(arr, n);
// Print the modified array
for (int i = 0; i < n; i++)
Console.Write(arr[i] + " ");
}
}
// This code is contributed by ajit.
JavaScript
<script>
// Javascript implementation of the approach
// Function to generate the array that
// satisfies the given condition
function generateArr(arr, n)
{
// If there is only a single element
// in the array
if (n == 1) {
document.write(arr[0]);
return;
}
// To store the generated array
let barr = new Array(n);
// First element
barr[0] = arr[0] & arr[1];
// Last element
barr[n - 1] = arr[n - 1] & arr[n - 2];
// Rest of the elements
for (let i = 1; i < n - 1; i++)
barr[i] = arr[i - 1] & arr[i + 1];
// Print the generated array
for (let i = 0; i < n; i++)
document.write(barr[i] + " ");
}
let arr = [ 1, 2, 3, 4, 5, 6 ];
let n = arr.length;
generateArr(arr, n);
</script>
Time Complexity: O(N), where N is the size of the given array.
Auxiliary Space: O(N), where N is the size of the given array.
Similar Reads
Generate original Array from the bitwise AND and Bitwise OR of adjacent elements Given an integer N denoting the size of an array and two arrays containing Bitwise AND and Bitwise OR of adjacent elements of the array and the first element of the array X, the task is to build the original array. Examples: Input: N = 2, X(First element) = 2Bitwise OR = {3}, Bitwise AND = {2}Output
4 min read
Generate an Array with XOR of even length prefix as 0 or 1 Given an integer N, the task is to construct an array of N distinct elements (arr[i] ⤠N+1) such that the bitwise XOR of every prefix having an even length is either 0 or 1. Examples: Input: N = 5Output = 2 3 4 5 6Explanation: XOR from arr[1] to arr[2] = XOR(2, 3) = 1XOR from arr[1] to arr[4] = XOR(
5 min read
Generate permutation of [1, N] having bitwise XOR of adjacent differences as 0 Given an integer N, the task is to generate a permutation from 1 to N such that the bitwise XOR of differences between adjacent elements is 0 i.e., | A[1]â A[2] | ^ | A[2]â A[3] | ^ . . . ^ | A[N â1] â A[N] | = 0, where |X - Y| represents absolute difference between X and Y. Examples: Input: N = 4Ou
5 min read
Find last element in Array formed from bitwise AND of array elements Given an array A[] of size N, the task is to find the last remaining element in a new array B containing all pairwise bitwise AND of elements from A i.e., B consists of N?(N ? 1) / 2 elements, each of the form Ai & Aj for some 1 ? i < j ? N. And we can perform the following operation any numb
6 min read
Construct the Array using given bitwise AND, OR and XOR Given Bitwise AND, OR, and XOR of N elements of an array denoted by a, b, c. The task is to find the elements of the array. If there exists no such array then print "-1".Examples: Input: N = 3, a = 4, b = 6, c = 6. Output: {4, 4, 6} Explanation: Bitwise AND of array = 4 & 4 & 6 = 4 Bitwise O
9 min read
Generate an array B[] from the given array A[] which satisfies the given conditions Given an array A[] of N integers such that A[0] + A[1] + A[2] + ... A[N - 1] = 0. The task is to generate an array B[] such that B[i] is either ?A[i] / 2? or ?A[i] / 2? for all valid i and B[0] + B[1] + B[2] + ... + B[N - 1] = 0. Examples: Input: A[] = {1, 2, -5, 3, -1} Output: 0 1 -2 1 0Input: A[]
6 min read