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++ program for the above approach #include <iostream> using namespace std;
// Function to count pairs with even sum int countEvenPairs( int N, int M)
{ // Stores the count of pairs
// with even sum
int count = 0;
// Traverse the range 1 to N
for ( int i = 1; i <= N; i++)
{
// Traverse the range 1 to M
for ( int j = 1; j <= M; j++)
{
// Check if the sum of the
// pair (i, j) is even or not
if ((i + j) % 2 == 0)
{
// Update count
count++;
}
}
}
// Return the count
return count;
} // Driver Code int main()
{ int N = 4;
int M = 6;
cout << countEvenPairs(N, M) << endl;
return 0;
} // This code is contributed by akhilsaini |
// Java program for the above approach import java.io.*;
class GFG {
// Function to count pairs with even sum
public static int countEvenPairs(
int N, int M)
{
// Stores the count of pairs
// with even sum
int count = 0 ;
// Traverse the range 1 to N
for ( int i = 1 ; i <= N; i++) {
// Traverse the range 1 to M
for ( int j = 1 ; j <= M; j++) {
// Check if the sum of the
// pair (i, j) is even or not
if ((i + j) % 2 == 0 ) {
// Update count
count++;
}
}
}
// Return the count
return count;
}
// Driver Code
public static void main(String[] args)
{
int N = 4 ;
int M = 6 ;
System.out.print(
countEvenPairs(N, M));
}
} |
# Python3 program for the above approach # Function to count pairs with even sum def countEvenPairs(N, M):
# Stores the count of pairs
# with even sum
count = 0
# Traverse the range 1 to N
for i in range ( 1 , N + 1 ):
# Traverse the range 1 to M
for j in range ( 1 , M + 1 ):
# Check if the sum of the
# pair (i, j) is even or not
if ((i + j) % 2 = = 0 ):
# Update count
count + = 1
# Return the count
return count
# Driver Code if __name__ = = '__main__' :
N = 4
M = 6
print (countEvenPairs(N, M))
# This code is contributed by akhilsaini |
// C# program for the above approach using System;
class GFG{
// Function to count pairs with even sum public static int countEvenPairs( int N, int M)
{ // Stores the count of pairs
// with even sum
int count = 0;
// Traverse the range 1 to N
for ( int i = 1; i <= N; i++)
{
// Traverse the range 1 to M
for ( int j = 1; j <= M; j++)
{
// Check if the sum of the
// pair (i, j) is even or not
if ((i + j) % 2 == 0)
{
// Update count
count++;
}
}
}
// Return the count
return count;
} // Driver Code public static void Main()
{ int N = 4;
int M = 6;
Console.WriteLine(
countEvenPairs(N, M));
} } // This code is contributed by akhilsaini |
<script> // Javascript program for the above approach // Function to count pairs with even sum function countEvenPairs(N, M)
{ // Stores the count of pairs
// with even sum
var count = 0;
// Traverse the range 1 to N
for ( var i = 1; i <= N; i++)
{
// Traverse the range 1 to M
for ( var j = 1; j <= M; j++)
{
// Check if the sum of the
// pair (i, j) is even or not
if ((i + j) % 2 == 0)
{
// Update count
count++;
}
}
}
// Return the count
return count;
} // Driver code var N = 4;
var M = 6;
document.write(countEvenPairs(N, M)); // This code is contributed by Kirti </script> |
12
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++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to count even pairs int countEvenPairs( int N, int M)
{ // Stores count of pairs having even sum
int count = 0;
// Stores count of even numbers up to N
int nEven = floor (N / 2);
// Stores count of odd numbers up to N
int nOdd = ceil (N / 2);
// Stores count of even numbers up to M
int mEven = floor (M / 2);
// Stores count of odd numbers up to M
int mOdd = ceil (M / 2);
count = nEven * mEven + nOdd * mOdd;
// Return the count
return count;
} // Driver Code int main()
{ int N = 4;
int M = 6;
cout << countEvenPairs(N, M);
return 0;
} // This code is contributed by Dharanendra L V |
// Java program for the above approach import java.io.*;
class GFG {
// Function to count even pairs
public static int countEvenPairs(
int N, int M)
{
// Stores count of pairs having even sum
int count = 0 ;
// Stores count of even numbers up to N
int nEven = ( int )Math.floor(( double )N / 2 );
// Stores count of odd numbers up to N
int nOdd = ( int )Math.ceil(( double )N / 2 );
// Stores count of even numbers up to M
int mEven = ( int )Math.floor(( double )M / 2 );
// Stores count of odd numbers up to M
int mOdd = ( int )Math.ceil(( double )M / 2 );
count = nEven * mEven + nOdd * mOdd;
// Return the count
return count;
}
// Driver Code
public static void main(String[] args)
{
int N = 4 ;
int M = 6 ;
System.out.print(countEvenPairs(N, M));
}
} |
# Python3 program for the above approach import math
# Function to count even pairs def countEvenPairs(N, M):
# Stores count of pairs having even sum
count = 0 ;
# Stores count of even numbers up to N
nEven = int (math.floor(N / 2 ));
# Stores count of odd numbers up to N
nOdd = int (math.ceil(N / 2 ));
# Stores count of even numbers up to M
mEven = int (math.floor(M / 2 ));
# Stores count of odd numbers up to M
mOdd = int (math.ceil(M / 2 ));
count = nEven * mEven + nOdd * mOdd;
# Return the count
return count;
# Driver Code if __name__ = = '__main__' :
N = 4 ;
M = 6 ;
print (countEvenPairs(N, M));
# This code is contributed by 29AjayKumar |
// C# program for the above approach using System;
class GFG
{ // Function to count even pairs
public static int countEvenPairs( int N, int M)
{
// Stores count of pairs having even sum
int count = 0;
// Stores count of even numbers up to N
int nEven = ( int )Math.Floor(( double )N / 2);
// Stores count of odd numbers up to N
int nOdd = ( int )Math.Ceiling(( double )N / 2);
// Stores count of even numbers up to M
int mEven = ( int )Math.Floor(( double )M / 2);
// Stores count of odd numbers up to M
int mOdd = ( int )Math.Ceiling(( double )M / 2);
count = nEven * mEven + nOdd * mOdd;
// Return the count
return count;
}
// Driver Code
public static void Main(String[] args)
{
int N = 4;
int M = 6;
Console.Write(countEvenPairs(N, M));
}
} // This code is contributed by shikhasingrajput |
<script> // Javascript program for the above approach // Function to count even pairs function countEvenPairs(N, M)
{ // Stores count of pairs having even sum
let count = 0;
// Stores count of even numbers up to N
nEven = parseInt(Math.floor(N / 2));
// Stores count of odd numbers up to N
nOdd = parseInt(Math.ceil(N / 2));
// Stores count of even numbers up to M
mEven = parseInt(Math.floor(M / 2));
// Stores count of odd numbers up to M
mOdd = parseInt(Math.ceil(M / 2));
count = nEven * mEven + nOdd * mOdd;
// Return the count
return count;
} // Driver Code let N = 4; let M = 6; document.write(countEvenPairs(N, M)); // This code is contributed by mohan1240760 </script> |
12
Time Complexity: O(1)
Auxiliary Space: O(1)