Count even sum pairs possible by selecting two integers from two given ranges respectively

Given two positive integers X and Y, the task is to count pairs with even sum possible by choosing any integer from the range 1 to X and another integer from the range 1 to Y.
Examples:Â
Input : X = 2, Y = 3Â
Output : 3Â
Explanation : All such possible pairs are {1, 1}, {1, 3}, {2, 2}.
Approach: Follow the steps below to solve the problem:Â
- Initialize a variable, say cntXEvenNums, to store the count of even numbers in the range [1, X] obtained by dividing X by 2.
- Initialize a variable, say cntXOddNums, to store the count of odd numbers in the range [1, X] obtained by dividing (X + 1) by 2.
- Initialize a variable, say cntYEvenNums, to store the count of even numbers between 1 to Y by dividing Y by 2.
- Initialize a variable, say cntYOddNums, to store the count of odd numbers between 1 to Y by dividing (Y + 1) by 2.
- Initialize a variable, say cntPairs, to store the count of even sum pairs by multiplying cntXEvenNums with cntYEvenNums and multiplying cntXOddNums with cntYOddNums and find the sum of both.
- Finally, print the value of cntPairs obtained.
Below is the implementation of the above approach:Â
C++
// C++ program to implement// the above approachÂ
#include <bits/stdc++.h>using namespace std;Â
// Function to count even// sum pairs in the given rangelong 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 Codeint 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 codeX = 2Y = 3Â
print(cntEvenSumPairs(X, Y))Â
# This code is contributed by hemanth gadarla |
C#
// C# program to implement// the above approachusing System;Â
class GFG{Â
// Function to count maximum even// sum pairs in the given rangestatic 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 Codepublic 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> |
Output:Â
3
Â
Time Complexity : O(1)
Auxiliary Space : O(1)
Â
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



