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:
Java
// 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)); } } |
12
Time Complexity: O(N2)
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 uing the formula:
count = nEven * mEven + nOdd * mOdd
Below is the implementation of the above approach:
Java
// 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)); } } |
Time Complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Count ways to express even number ‘n’ as sum of even integers
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Lexicographically smallest permutation of size A having B integers exceeding all preceeding integers
- Pair of integers having difference of their fifth power as X
- Pair of integers with difference K having an element as the K-th multiple of the other
- Count numbers from given range having odd digits at odd places and even digits at even places
- Queries to check if any pair exists in an array having values at most equal to the given pair
- Count of elements to be multiplied with integers to make each pair of Array a perfect square
- Check if a number has an odd count of odd divisors and even count of even divisors
- Print any pair of integers with sum of GCD and LCM equals to N
- Maximum possible GCD for a pair of integers with sum N
- Count subarrays having sum of elements at even and odd positions equal
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Pair formation such that maximum pair sum is minimized
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Count sequences of positive integers having product X
- Generate minimum sum sequence of integers with even elements greater
- Ways to form an array having integers in given range such that total sum is divisible by 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

