Dada una array de enteros arr[] de N elementos, la tarea es generar otra array que tenga (Bitwise) AND de elementos anteriores y siguientes con las siguientes excepciones.
- El primer elemento es el AND bit a bit del primer y segundo elemento.
- El último elemento es el AND bit a bit del último y penúltimo elemento.
Ejemplos:
Entrada: arr[] = {1, 2, 3, 4, 5, 6}
Salida: 0 1 0 1 4 4
La nueva array será {1 y 2, 1 y 3, 2 y 4, 3 y 5, 4 y 6, 5 y 6}Entrada: arr[] = {9, 8, 7}
Salida: 8 1 0
Enfoque: el primer y el segundo elemento de la nueva array se pueden calcular como arr[0] & arr[1] y arr[N – 1] & arr[N – 2] respectivamente. El resto de los elementos se pueden calcular como arr[i – 1] & arr[i + 1] .
A continuación se muestra la implementación del enfoque anterior:
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>
0 1 0 1 4 4
Complejidad de tiempo: O(N), donde N es el tamaño de la array dada.
Espacio auxiliar: O(N), donde N es el tamaño de la array dada.
Publicación traducida automáticamente
Artículo escrito por NikhilRathor y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA