Count pair of integers having even sum

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Output:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


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.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

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.