Count pairs from a given range having even sum
Last Updated :
06 Jul, 2021
Given two positive integers L and R, the task is to find the count of ordered pairs in the range [L, R] such that the sum of elements of each pair is even.
Examples:
Input: L = 1, R =3
Output: 5
Explanation:
Pairs whose sum of elements are even and lies in the range [1, 3] are { (1, 3), (1, 1), (2, 2), (3, 3), (3, 1) }
Input: L = 1, R = 1
Output: 1
Approach: The problem can be solved using the fact that the sum of two numbers is even only when both the numbers are even or both the numbers are odd. Follow the steps below to solve this problem:
- Count all the even numbers, say cnt_even, in the range [L, R]. Follow the steps below in order to calculate:
- The count of pairs whose sum of elements is even and both the elements of the pairs are also even is count_even * count_even.
- Similarly, count all the odd numbers, say count_odd, in the range [L, R]. Follow the steps below in order to calculate:
- If L is even, then count_odd = ((R + 1) / 2) – ((L + 1) / 2).
- If L is odd, then count_odd = ( (R+1) / 2) – ((L + 1) / 2) + 1.
- The count of pairs whose sum of elements is even and both the elements of the pairs are also odd is count_odd * count_odd
- Finally, print the value of (count_odd * count_odd + count_even * count_even)
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int countPairs( int L, int R)
{
int count_even;
if (L % 2 == 0) {
count_even = (R / 2) - (L / 2) + 1;
}
else {
count_even = (R / 2) - (L / 2);
}
int count_odd;
if (L % 2 == 0) {
count_odd = ((R + 1) / 2) - ((L + 1) / 2);
}
else {
count_odd = ((R + 1) / 2) - ((L + 1) / 2) + 1;
}
count_even *= count_even;
count_odd *= count_odd;
cout << count_even + count_odd;
}
int main()
{
int L = 1, R = 3;
countPairs(L, R);
return 0;
}
|
Java
class GFG
{
static void countPairs( int L, int R)
{
int count_even;
if (L % 2 == 0 )
{
count_even = (R / 2 ) - (L / 2 ) + 1 ;
}
else {
count_even = (R / 2 ) - (L / 2 );
}
int count_odd;
if (L % 2 == 0 )
{
count_odd = ((R + 1 ) / 2 ) - ((L + 1 ) / 2 );
}
else
{
count_odd = ((R + 1 ) / 2 ) - ((L + 1 ) / 2 ) + 1 ;
}
count_even *= count_even;
count_odd *= count_odd;
System.out.println(count_even + count_odd);
}
public static void main (String[] args)
{
int L = 1 , R = 3 ;
countPairs(L, R);
}
}
|
Python3
def countPairs(L, R):
count_even = 0
if (L % 2 = = 0 ):
count_even = ((R / / 2 ) -
(L / / 2 ) + 1 )
else :
count_even = ((R / / 2 ) -
(L / / 2 ))
count_odd = 0
if (L % 2 = = 0 ):
count_odd = (((R + 1 ) / / 2 ) -
((L + 1 ) / / 2 ))
else :
count_odd = (((R + 1 ) / / 2 ) -
((L + 1 ) / / 2 ) + 1 )
count_even * = count_even
count_odd * = count_odd
print (count_even + count_odd)
if __name__ = = '__main__' :
L, R = 1 , 3
countPairs(L, R)
|
C#
using System;
class GFG
{
static void countPairs( int L, int R)
{
int count_even;
if (L % 2 == 0)
{
count_even = (R / 2) - (L / 2) + 1;
}
else
{
count_even = (R / 2) - (L / 2);
}
int count_odd;
if (L % 2 == 0)
{
count_odd = ((R + 1) / 2) - ((L + 1) / 2);
}
else
{
count_odd = ((R + 1) / 2) - ((L + 1) / 2) + 1;
}
count_even *= count_even;
count_odd *= count_odd;
Console.WriteLine(count_even + count_odd);
}
public static void Main(String[] args)
{
int L = 1, R = 3;
countPairs(L, R);
}
}
|
Javascript
<script>
function countPairs(L, R)
{
let count_even;
if (L % 2 == 0)
{
count_even = (R / 2) - (L / 2) + 1;
}
else
{
count_even = (R / 2) - (L / 2);
}
let count_odd;
if (L % 2 == 0)
{
count_odd = ((R + 1) / 2) -
((L + 1) / 2);
}
else
{
count_odd = ((R + 1) / 2) -
((L + 1) / 2) + 1;
}
count_even *= count_even;
count_odd *= count_odd;
document.write(count_even + count_odd);
}
let L = 1, R = 3;
countPairs(L, R);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...