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.
- 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 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[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 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. |
0 1 0 1 4 4
Time Complexity: O(N)
Recommended Posts:
- Replace every array element by Bitwise Xor of previous and next element
- Range Query on array whose each element is XOR of index value and previous element
- Replace every element of the array by its previous element
- Replace every array element by sum of previous and next
- Elements greater than the previous and next element in an Array
- Replace every array element by multiplication of previous and next
- Rearrange an array such that every odd indexed element is greater than it previous
- Maximum element in an array such that its previous and next element product is maximum
- Replace every element of the array with BitWise XOR of all other
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Previous greater element
- Rearrange array elements such that Bitwise AND of first N - 1 elements is equal to last element
- Maximum previous and next element product
- Array sum after dividing numbers from previous
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.



