Dado un número , la tarea es contar pares (x, y) de modo que su suma (x+y) sea divisible por su valor xor (x^y) y la condición 1 ≤ x < y < N se cumpla.
Ejemplos :
Input: N = 3 Output: 3 Explanation: (1, 2), (1, 3), (2, 3) are the valid pairs Input: N = 6 Output: 11
Acercarse:
- Después de tomar la array como entrada, primero debemos encontrar todos los pares posibles en esa array .
- Entonces, averigüe los pares de la array.
- Luego, para cada par, verifica si la suma del par es divisible por el valor xor del par. Si es así, aumente el conteo requerido en uno.
- Cuando se hayan verificado todos los pares, devuelva o imprima el conteo de dicho par.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to count pairs from 1 to N // such that their Sum is divisible by their XOR #include <bits/stdc++.h> using namespace std; // Function to count pairs int countPairs(int n) { // variable to store count int count = 0; // Generate all possible pairs such that // 1 <= x < y < n for (int x = 1; x < n; x++) { for (int y = x + 1; y <= n; y++) { if ((y + x) % (y ^ x) == 0) count++; } } return count; } // Driver code int main() { int n = 6; cout << countPairs(n); return 0; }
Java
// Java program to count pairs from 1 to N // such that their Sum is divisible by their XOR class GFG { // Function to count pairs static int countPairs(int n) { // variable to store count int count = 0; // Generate all possible pairs such that // 1 <= x < y < n for (int x = 1; x < n; x++) { for (int y = x + 1; y <= n; y++) { if ((y + x) % (y ^ x) == 0) count++; } } return count; } // Driver code public static void main (String[] args) { int n = 6; System.out.println(countPairs(n)); } } // This code is contributed by AnkitRai01
Python3
# Python3 program to count pairs from 1 to N # such that their Sum is divisible by their XOR # Function to count pairs def countPairs(n) : # variable to store count count = 0; # Generate all possible pairs such that # 1 <= x < y < n for x in range(1, n) : for y in range(x + 1, n + 1) : if ((y + x) % (y ^ x) == 0) : count += 1; return count; # Driver code if __name__ == "__main__" : n = 6; print(countPairs(n)); # This code is contributed by AnkitRai01
C#
// C# program to count pairs from 1 to N // such that their Sum is divisible by their XOR using System; public class GFG { // Function to count pairs static int countPairs(int n) { // variable to store count int count = 0; // Generate all possible pairs such that // 1 <= x < y < n for (int x = 1; x < n; x++) { for (int y = x + 1; y <= n; y++) { if ((y + x) % (y ^ x) == 0) count++; } } return count; } // Driver code public static void Main() { int n = 6; Console.WriteLine(countPairs(n)); } } // This code is contributed by AnkitRai01
Javascript
<script> // JavaScript program to count pairs from 1 to N // such that their Sum is divisible by their XOR // Function to count pairs function countPairs(n) { // variable to store count let count = 0; // Generate all possible pairs such that // 1 <= x < y < n for (let x = 1; x < n; x++) { for (let y = x + 1; y <= n; y++) { if ((y + x) % (y ^ x) == 0) count++; } } return count; } // Driver code let n = 6; document.write(countPairs(n)); // This code is contributed by Surbhi Tyagi. </script>
Producción:
11
Complejidad de tiempo: O(N 2 ), ya que estamos usando bucles anidados para atravesar N*N veces.
Espacio auxiliar: O(1), ya que no estamos utilizando ningún espacio adicional.