# Generate an array having Bitwise AND of the previous and the next element

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.

1. The first element is the bitwise AND of the first and the second element.
2. 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The first and the second element of the new array can be calculated as arr & arr 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 ` `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; ` `        ``return``; ` `    ``} ` ` `  `    ``// To store the generated array ` `    ``int` `barr[n]; ` ` `  `    ``// First element ` `    ``barr = arr & arr; ` ` `  `    ``// 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); ` ` `  `    ``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  ` `    ``// 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 interation  ` `        ``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``]; ` ` `  `    ``# Prthe 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  ` `    ``// and update it  ` `    ``int` `prev = arr;  ` `    ``arr = arr & arr;  ` ` `  `    ``// Update rest of the array elements  ` `    ``for` `(``int` `i = 1; i < n - 1; i++)  ` `    ``{  ` `        ``// Store current value of  ` `        ``// next interation  ` `        ``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. `

Output:

```0 1 0 1 4 4
```

Time Complexity: O(N)

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.