Dada una array arr[] que consta de N enteros positivos y un entero K , la tarea es contar todos los pares posibles de la array dada con Bitwise OR igual a K .
Ejemplos:
Entrada: arr[] = {2, 38, 44, 29, 62}, K = 46
Salida: 2
Explicación: Solo los siguientes dos pares están presentes en la array cuyo OR bit a bit es 46:
- 2 O 44 = 46
- 38 O 44 = 46
Entrada: arr[] = {1, 5, 20, 15, 14}, K = 20
Salida: 5
Explicación:
Solo hay 5 pares cuyo OR bit a bit es 20:
- 1 O 15 = 15
- 1 O 14 = 15
- 5 O 15 = 15
- 5 O 14 = 15
- 15 O 14 = 15
Enfoque: Para resolver el problema, la idea es generar todos los pares posibles a partir de la array dada y contar aquellos pares cuyo OR bit a bit sea igual a K . Después de verificar todos los pares, imprima el conteo almacenado.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <iostream> using namespace std; // Function that counts the pairs from // the array whose Bitwise OR is K void countPairs(int arr[], int k, int size) { // Stores the required // count of pairs int count = 0, x; // Generate all possible pairs for (int i = 0; i < size - 1; i++) { for (int j = i + 1; j < size; j++) { // Perform OR operation x = arr[i] | arr[j]; // If Bitwise OR is equal // to K, increment count if (x == k) count++; } } // Print the total count cout << count; } // Driver Code int main() { int arr[] = { 2, 38, 44, 29, 62 }; int K = 46; int N = sizeof(arr) / sizeof(arr[0]); // Function Call countPairs(arr, K, N); return 0; }
Java
// Java program for the above approach import java.io.*; import java.util.*; class GFG{ // Function that counts the pairs from // the array whose Bitwise OR is K static void countPairs(int[] arr, int k, int size) { // Stores the required // count of pairs int count = 0, x; // Generate all possible pairs for(int i = 0; i < size - 1; i++) { for(int j = i + 1; j < size; j++) { // Perform OR operation x = arr[i] | arr[j]; // If Bitwise OR is equal // to K, increment count if (x == k) count++; } } // Print the total count System.out.println(count); } // Driver Code public static void main(String[] args) { int[] arr = { 2, 38, 44, 29, 62 }; int K = 46; int N = arr.length; // Function Call countPairs(arr, K, N); } } // This code is contributed by code_hunt
Python3
# Python3 program for the above approach # Function that counts the pairs from # the array whose Bitwise OR is K def countPairs(arr, k, size): # Stores the required # count of pairs count = 0 # Generate all possible pairs for i in range(size - 1): for j in range(i + 1, size): # Perform OR operation x = arr[i] | arr[j] # If Bitwise OR is equal # to K, increment count if (x == k): count += 1 # Print the total count print(count) # Driver Code arr = [ 2, 38, 44, 29, 62 ] K = 46 N = len(arr) # Function Call countPairs(arr, K, N) # This code is contributed by sanjoy_62
C#
// C# program for the above approach using System; class GFG{ // Function that counts the pairs from // the array whose Bitwise OR is K static void countPairs(int[] arr, int k, int size) { // Stores the required // count of pairs int count = 0, x; // Generate all possible pairs for(int i = 0; i < size - 1; i++) { for(int j = i + 1; j < size; j++) { // Perform OR operation x = arr[i] | arr[j]; // If Bitwise OR is equal // to K, increment count if (x == k) count++; } } // Print the total count Console.WriteLine(count); } // Driver Code public static void Main() { int[] arr = { 2, 38, 44, 29, 62 }; int K = 46; int N = arr.Length; // Function Call countPairs(arr, K, N); } } // This code is contributed by susmitakundugoaldanga
Javascript
<script> // JavaScript program for the above approach // Function that counts the pairs from // the array whose Bitwise OR is K function countPairs(arr, k, size) { // Stores the required // count of pairs let count = 0, x; // Generate all possible pairs for(let i = 0; i < size - 1; i++) { for(let j = i + 1; j < size; j++) { // Perform OR operation x = arr[i] | arr[j]; // If Bitwise OR is equal // to K, increment count if (x == k) count++; } } // Print the total count document.write(count); } // Driver Code let arr = [ 2, 38, 44, 29, 62 ]; let K = 46; let N = arr.length; // Function Call countPairs(arr, K, N); // This code is contributed by avijitmondal998. </script>
2
Complejidad de Tiempo: O(N 2 )
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por adityaroshan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA