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++
#include <iostream>
using namespace std;
void generateArr( int arr[], int n)
{
if (n == 1) {
cout << arr[0];
return ;
}
int barr[n];
barr[0] = arr[0] & arr[1];
barr[n - 1] = arr[n - 1] & arr[n - 2];
for ( int i = 1; i < n - 1; i++)
barr[i] = arr[i - 1] & arr[i + 1];
for ( int i = 0; i < n; i++)
cout << barr[i] << " " ;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
generateArr(arr, n);
return 0;
}
|
Java
import java .io.*;
class GFG
{
static void generateArr( int [] arr, int n)
{
if (n <= 1 )
return ;
int prev = arr[ 0 ];
arr[ 0 ] = arr[ 0 ] & arr[ 1 ];
for ( int i = 1 ; i < n - 1 ; i++)
{
int curr = arr[i];
arr[i] = prev & arr[i + 1 ];
prev = curr;
}
arr[n - 1 ] = prev & arr[n - 1 ];
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 , 6 };
int n = arr.length;
generateArr(arr, n);
for ( int i = 0 ; i < n; i++)
System.out.print(arr[i] + " " );
}
}
|
Python3
def generateArr(arr, n):
if (n = = 1 ):
print (arr[ 0 ]);
return ;
barr = [ 0 ] * n;
barr[ 0 ] = arr[ 0 ] & arr[ 1 ];
barr[n - 1 ] = arr[n - 1 ] & arr[n - 2 ];
for i in range ( 1 , n - 1 ):
barr[i] = arr[i - 1 ] & arr[i + 1 ];
for i in range (n):
print (barr[i], end = " " );
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ];
n = len (arr);
generateArr(arr, n);
|
C#
using System;
class GFG
{
static void generateArr( int [] arr, int n)
{
if (n <= 1)
return ;
int prev = arr[0];
arr[0] = arr[0] & arr[1];
for ( int i = 1; i < n - 1; i++)
{
int curr = arr[i];
arr[i] = prev & arr[i + 1];
prev = curr;
}
arr[n - 1] = prev & arr[n - 1];
}
static public void Main ()
{
int [] arr = { 1, 2, 3, 4, 5, 6 };
int n = arr.Length;
generateArr(arr, n);
for ( int i = 0; i < n; i++)
Console.Write(arr[i] + " " );
}
}
|
Javascript
<script>
function generateArr(arr, n)
{
if (n == 1) {
document.write(arr[0]);
return ;
}
let barr = new Array(n);
barr[0] = arr[0] & arr[1];
barr[n - 1] = arr[n - 1] & arr[n - 2];
for (let i = 1; i < n - 1; i++)
barr[i] = arr[i - 1] & arr[i + 1];
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...