Count pair of integers having even sum
Last Updated :
13 Jun, 2022
Given two integers N and M, the task is to count all possible pair of integers (i, j) (1 ? i ? N, 1 ? j ? M) such that i + j is even.
Examples:
Input: N = 6, M = 4
Output: 12
Explanation: The pairs (1, 1), (1, 3), (2, 2), (2, 4), (3, 1), (3, 3), (4, 2), (4, 4), (5, 1), (5, 3), (6, 2), (6, 4) satisfy the required condition. Therefore, the count is 12.
Input: N = 2 and M = 8
Output: 8
Naive Approach: The simplest approach to solve this problem is to traverse over the range [1, M] for every number in that range, traverse the range [1, N] and generate all possible pairs. For every possible pair, check its sum is an even number or not. If found to be true, increment the count. Finally, print the count obtained.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int countEvenPairs( int N, int M)
{
int count = 0;
for ( int i = 1; i <= N; i++)
{
for ( int j = 1; j <= M; j++)
{
if ((i + j) % 2 == 0)
{
count++;
}
}
}
return count;
}
int main()
{
int N = 4;
int M = 6;
cout << countEvenPairs(N, M) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int countEvenPairs(
int N, int M)
{
int count = 0 ;
for ( int i = 1 ; i <= N; i++) {
for ( int j = 1 ; j <= M; j++) {
if ((i + j) % 2 == 0 ) {
count++;
}
}
}
return count;
}
public static void main(String[] args)
{
int N = 4 ;
int M = 6 ;
System.out.print(
countEvenPairs(N, M));
}
}
|
Python3
def countEvenPairs(N, M):
count = 0
for i in range ( 1 , N + 1 ):
for j in range ( 1 , M + 1 ):
if ((i + j) % 2 = = 0 ):
count + = 1
return count
if __name__ = = '__main__' :
N = 4
M = 6
print (countEvenPairs(N, M))
|
C#
using System;
class GFG{
public static int countEvenPairs( int N, int M)
{
int count = 0;
for ( int i = 1; i <= N; i++)
{
for ( int j = 1; j <= M; j++)
{
if ((i + j) % 2 == 0)
{
count++;
}
}
}
return count;
}
public static void Main()
{
int N = 4;
int M = 6;
Console.WriteLine(
countEvenPairs(N, M));
}
}
|
Javascript
<script>
function countEvenPairs(N, M)
{
var count = 0;
for ( var i = 1; i <= N; i++)
{
for ( var j = 1; j <= M; j++)
{
if ((i + j) % 2 == 0)
{
count++;
}
}
}
return count;
}
var N = 4;
var M = 6;
document.write(countEvenPairs(N, M));
</script>
|
Time Complexity: O(N*M)
Space Complexity: O(1)
Efficient Approach: The above approach can be optimized based on the following observations:
- Even number + Even number = Even number
- Odd number + Odd number = Even number
Follow the steps below to solve the problem:
- Initialize two variables, say nEven and nOdd, to store the count of odd and even integers up to N.
- Initialize two variables, say mEven and mOdd, to store the count of even and odd integers up to M.
- Finally, count the required number of pairs using the formula:
count = nEven * mEven + nOdd * mOdd
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countEvenPairs( int N, int M)
{
int count = 0;
int nEven = floor (N / 2);
int nOdd = ceil (N / 2);
int mEven = floor (M / 2);
int mOdd = ceil (M / 2);
count = nEven * mEven + nOdd * mOdd;
return count;
}
int main()
{
int N = 4;
int M = 6;
cout << countEvenPairs(N, M);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int countEvenPairs(
int N, int M)
{
int count = 0 ;
int nEven = ( int )Math.floor(( double )N / 2 );
int nOdd = ( int )Math.ceil(( double )N / 2 );
int mEven = ( int )Math.floor(( double )M / 2 );
int mOdd = ( int )Math.ceil(( double )M / 2 );
count = nEven * mEven + nOdd * mOdd;
return count;
}
public static void main(String[] args)
{
int N = 4 ;
int M = 6 ;
System.out.print(countEvenPairs(N, M));
}
}
|
Python3
import math
def countEvenPairs(N, M):
count = 0 ;
nEven = int (math.floor(N / 2 ));
nOdd = int (math.ceil(N / 2 ));
mEven = int (math.floor(M / 2 ));
mOdd = int (math.ceil(M / 2 ));
count = nEven * mEven + nOdd * mOdd;
return count;
if __name__ = = '__main__' :
N = 4 ;
M = 6 ;
print (countEvenPairs(N, M));
|
C#
using System;
class GFG
{
public static int countEvenPairs( int N, int M)
{
int count = 0;
int nEven = ( int )Math.Floor(( double )N / 2);
int nOdd = ( int )Math.Ceiling(( double )N / 2);
int mEven = ( int )Math.Floor(( double )M / 2);
int mOdd = ( int )Math.Ceiling(( double )M / 2);
count = nEven * mEven + nOdd * mOdd;
return count;
}
public static void Main(String[] args)
{
int N = 4;
int M = 6;
Console.Write(countEvenPairs(N, M));
}
}
|
Javascript
<script>
function countEvenPairs(N, M)
{
let count = 0;
nEven = parseInt(Math.floor(N / 2));
nOdd = parseInt(Math.ceil(N / 2));
mEven = parseInt(Math.floor(M / 2));
mOdd = parseInt(Math.ceil(M / 2));
count = nEven * mEven + nOdd * mOdd;
return count;
}
let N = 4;
let M = 6;
document.write(countEvenPairs(N, M));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...