Dados dos enteros positivos X e Y , la tarea es contar pares con una suma par posible eligiendo cualquier entero del rango 1 a X y otro entero del rango 1 a Y .
Ejemplos:
Entrada: X = 2, Y = 3
Salida: 3
Explicación: Todos estos pares posibles son {1, 1}, {1, 3}, {2, 2}.
Enfoque: siga los pasos a continuación para resolver el problema:
- Inicialice una variable, digamos cntXEvenNums , para almacenar el recuento de números pares en el rango [1, X] obtenido al dividir X entre 2 .
- Inicialice una variable, digamos cntXOddNums , para almacenar el recuento de números impares en el rango [1, X] obtenido al dividir (X + 1) entre 2 .
- Inicialice una variable, digamos cntYEvenNums , para almacenar el recuento de números pares entre 1 y Y dividiendo Y por 2 .
- Inicialice una variable, digamos cntYOddNums , para almacenar el recuento de números impares entre 1 y Y dividiendo (Y + 1) por 2 .
- Inicialice una variable, digamos cntPairs , para almacenar el recuento de pares de sumas pares multiplicando cntXEvenNums con cntYEvenNums y multiplicando cntXOddNums con cntYOddNums y encuentre la suma de ambos.
- Finalmente, imprime el valor de cntPairs obtenido.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to count even // sum pairs in the given range long long cntEvenSumPairs(long long X, long long Y) { // Stores the count of even // numbers between 1 to X long long cntXEvenNums = X / 2; // Stores the count of odd // numbers between 1 to X long long cntXOddNums = (X + 1) / 2; // Stores the count of even // numbers between 1 to Y long long cntYEvenNums = Y / 2; // Stores the count of odd // numbers between 1 to Y long long cntYOddNums = (Y + 1) / 2; // Stores the count of // pairs having even sum long long cntPairs = (cntXEvenNums * 1LL * cntYEvenNums) + (cntXOddNums * 1LL * cntYOddNums); // Returns the count of pairs // having even sum return cntPairs; } // Driver Code int main() { long long X = 2; long long Y = 3; cout << cntEvenSumPairs(X, Y); return 0; }
Java
// Java program to implement // the above approach import java.io.*; class GFG { // Function to count maximum even // sum pairs in the given range static long cntEvenSumPairs(long X, long Y) { // Stores the count of even // numbers between 1 to X long cntXEvenNums = X / 2; // Stores the count of odd // numbers between 1 to X long cntXOddNums = (X + 1) / 2; // Stores the count of even // numbers between 1 to Y long cntYEvenNums = Y / 2; // Stores the count of odd // numbers between 1 to Y long cntYOddNums = (Y + 1) / 2; // Stores the count of // pairs having even sum long cntPairs = (cntXEvenNums * cntYEvenNums) + (cntXOddNums * cntYOddNums); // Returns the count of pairs // having even sum return cntPairs; } // Driver Code public static void main(String[] args) { long X = 2; long Y = 3; System.out.println(cntEvenSumPairs(X, Y)); } }
Python
# Python program to implement # the above approach # Function to count even # sum pairs in the given range def cntEvenSumPairs(X, Y): # Stores the count of even # numbers between 1 to X cntXEvenNums = X / 2 # Stores the count of odd # numbers between 1 to X cntXOddNums = (X + 1) / 2 # Stores the count of even # numbers between 1 to Y cntYEvenNums = Y / 2 # Stores the count of odd # numbers between 1 to Y cntYOddNums = (Y + 1) / 2 # Stores the count of # pairs having even sum cntPairs = ((cntXEvenNums * cntYEvenNums) + (cntXOddNums * cntYOddNums)) # Returns the count of pairs # having even sum return cntPairs # Driver code X = 2 Y = 3 print(cntEvenSumPairs(X, Y)) # This code is contributed by hemanth gadarla
C#
// C# program to implement // the above approach using System; class GFG{ // Function to count maximum even // sum pairs in the given range static long cntEvenSumPairs(long X, long Y) { // Stores the count of even // numbers between 1 to X long cntXEvenNums = X / 2; // Stores the count of odd // numbers between 1 to X long cntXOddNums = (X + 1) / 2; // Stores the count of even // numbers between 1 to Y long cntYEvenNums = Y / 2; // Stores the count of odd // numbers between 1 to Y long cntYOddNums = (Y + 1) / 2; // Stores the count of // pairs having even sum long cntPairs = (cntXEvenNums * cntYEvenNums) + (cntXOddNums * cntYOddNums); // Returns the count of pairs // having even sum return cntPairs; } // Driver Code public static void Main(string[] args) { long X = 2; long Y = 3; Console.WriteLine(cntEvenSumPairs(X, Y)); } } // This code is contributed by chitranayal
Javascript
<script> // Javascript program to implement // the above approach // Function to count maximum even // sum pairs in the given range function cntEvenSumPairs(X , Y) { // Stores the count of even // numbers between 1 to X var cntXEvenNums = parseInt(X / 2); // Stores the count of odd // numbers between 1 to X var cntXOddNums = parseInt((X + 1) / 2); // Stores the count of even // numbers between 1 to Y var cntYEvenNums = parseInt(Y / 2); // Stores the count of odd // numbers between 1 to Y var cntYOddNums =parseInt( (Y + 1) / 2); // Stores the count of // pairs having even sum var cntPairs = (cntXEvenNums * cntYEvenNums) + (cntXOddNums * cntYOddNums); // Returns the count of pairs // having even sum return cntPairs; } // Driver Code var X = 2; var Y = 3; document.write(cntEvenSumPairs(X, Y)); // This code contributed by Rajput-Ji </script>
Producción:
3
Complejidad de Tiempo : O(1)
Espacio Auxiliar : O(1)
Publicación traducida automáticamente
Artículo escrito por dharanendralv23 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA