Count even sum pairs possible by selecting two integers from two given ranges respectively
Last Updated :
16 Feb, 2022
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++
#include <bits/stdc++.h>
using namespace std;
long long cntEvenSumPairs( long long X, long long Y)
{
long long cntXEvenNums = X / 2;
long long cntXOddNums = (X + 1) / 2;
long long cntYEvenNums = Y / 2;
long long cntYOddNums = (Y + 1) / 2;
long long cntPairs
= (cntXEvenNums * 1LL * cntYEvenNums)
+ (cntXOddNums * 1LL * cntYOddNums);
return cntPairs;
}
int main()
{
long long X = 2;
long long Y = 3;
cout << cntEvenSumPairs(X, Y);
return 0;
}
|
Java
import java.io.*;
class GFG {
static long cntEvenSumPairs( long X, long Y)
{
long cntXEvenNums = X / 2 ;
long cntXOddNums = (X + 1 ) / 2 ;
long cntYEvenNums = Y / 2 ;
long cntYOddNums = (Y + 1 ) / 2 ;
long cntPairs = (cntXEvenNums * cntYEvenNums)
+ (cntXOddNums * cntYOddNums);
return cntPairs;
}
public static void main(String[] args)
{
long X = 2 ;
long Y = 3 ;
System.out.println(cntEvenSumPairs(X, Y));
}
}
|
Python
def cntEvenSumPairs(X, Y):
cntXEvenNums = X / 2
cntXOddNums = (X + 1 ) / 2
cntYEvenNums = Y / 2
cntYOddNums = (Y + 1 ) / 2
cntPairs = ((cntXEvenNums * cntYEvenNums) +
(cntXOddNums * cntYOddNums))
return cntPairs
X = 2
Y = 3
print (cntEvenSumPairs(X, Y))
|
C#
using System;
class GFG{
static long cntEvenSumPairs( long X, long Y)
{
long cntXEvenNums = X / 2;
long cntXOddNums = (X + 1) / 2;
long cntYEvenNums = Y / 2;
long cntYOddNums = (Y + 1) / 2;
long cntPairs = (cntXEvenNums * cntYEvenNums) +
(cntXOddNums * cntYOddNums);
return cntPairs;
}
public static void Main( string [] args)
{
long X = 2;
long Y = 3;
Console.WriteLine(cntEvenSumPairs(X, Y));
}
}
|
Javascript
<script>
function cntEvenSumPairs(X , Y) {
var cntXEvenNums = parseInt(X / 2);
var cntXOddNums = parseInt((X + 1) / 2);
var cntYEvenNums = parseInt(Y / 2);
var cntYOddNums =parseInt( (Y + 1) / 2);
var cntPairs = (cntXEvenNums * cntYEvenNums) +
(cntXOddNums * cntYOddNums);
return cntPairs;
}
var X = 2;
var Y = 3;
document.write(cntEvenSumPairs(X, Y));
</script>
|
Time Complexity : O(1)
Auxiliary Space : O(1)
Share your thoughts in the comments
Please Login to comment...