# Count even sum pairs possible by selecting two integers from two given ranges respectively

Given two positive integers **X** and **Y**, the task is to count pairs with even sum possible by choosing any integer from the range **1** to **X** and another integer from the range **1** to **Y**.

**Examples:**

Input :X = 2, Y = 3Output :3Explanation :All such possible pairs are {1, 1}, {1, 3}, {2, 2}.

**Approach:** Follow the steps below to solve the problem:

- Initialize a variable, say
**cntXEvenNums**, to store the count of even numbers in the range**[1, X]**obtained by dividing**X**by**2**. - Initialize a variable, say
**cntXOddNums**, to store the count of odd numbers in the range**[1, X]**obtained by dividing**(X + 1)**by**2**. - Initialize a variable, say
**cntYEvenNums**, to store the count of even numbers between**1**to**Y**by dividing**Y**by**2**. - Initialize a variable, say
**cntYOddNums**, to store the count of odd numbers between**1**to**Y**by dividing**(Y + 1)**by**2**. - Initialize a variable, say
**cntPairs**, to store the count of even sum pairs by multiplying**cntXEvenNums**with**cntYEvenNums**and multiplying**cntXOddNums**with**cntYOddNums**and find the sum of both. - Finally, print the value of
**cntPairs**obtained.

Below is the implementation of the above approach:

## C++

`// C++ program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to count even` `// sum pairs in the given range` `long` `long` `cntEvenSumPairs(` `long` `long` `X, ` `long` `long` `Y)` `{` ` ` `// Stores the count of even` ` ` `// numbers between 1 to X` ` ` `long` `long` `cntXEvenNums = X / 2;` ` ` `// Stores the count of odd` ` ` `// numbers between 1 to X` ` ` `long` `long` `cntXOddNums = (X + 1) / 2;` ` ` `// Stores the count of even` ` ` `// numbers between 1 to Y` ` ` `long` `long` `cntYEvenNums = Y / 2;` ` ` `// Stores the count of odd` ` ` `// numbers between 1 to Y` ` ` `long` `long` `cntYOddNums = (Y + 1) / 2;` ` ` `// Stores the count of` ` ` `// pairs having even sum` ` ` `long` `long` `cntPairs` ` ` `= (cntXEvenNums * 1LL * cntYEvenNums)` ` ` `+ (cntXOddNums * 1LL * cntYOddNums);` ` ` `// Retuens the count of pairs` ` ` `// having even sum` ` ` `return` `cntPairs;` `}` `// Driver Code` `int` `main()` `{` ` ` `long` `long` `X = 2;` ` ` `long` `long` `Y = 3;` ` ` `cout << cntEvenSumPairs(X, Y);` ` ` `return` `0;` `}` |

## Java

`// Java program to implement` `// the above approach` `import` `java.io.*;` `class` `GFG {` ` ` `// Function to count maximum even` ` ` `// sum pairs in the given range` ` ` `static` `long` `cntEvenSumPairs(` `long` `X, ` `long` `Y)` ` ` `{` ` ` `// Stores the count of even` ` ` `// numbers between 1 to X` ` ` `long` `cntXEvenNums = X / ` `2` `;` ` ` `// Stores the count of odd` ` ` `// numbers between 1 to X` ` ` `long` `cntXOddNums = (X + ` `1` `) / ` `2` `;` ` ` `// Stores the count of even` ` ` `// numbers between 1 to Y` ` ` `long` `cntYEvenNums = Y / ` `2` `;` ` ` `// Stores the count of odd` ` ` `// numbers between 1 to Y` ` ` `long` `cntYOddNums = (Y + ` `1` `) / ` `2` `;` ` ` `// Stores the count of` ` ` `// pairs having even sum` ` ` `long` `cntPairs = (cntXEvenNums * cntYEvenNums)` ` ` `+ (cntXOddNums * cntYOddNums);` ` ` `// Retuens the count of pairs` ` ` `// having even sum` ` ` `return` `cntPairs;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `long` `X = ` `2` `;` ` ` `long` `Y = ` `3` `;` ` ` `System.out.println(cntEvenSumPairs(X, Y));` ` ` `}` `}` |

## Python

`# Python program to implement` `# the above approach` `# Function to count even` `# sum pairs in the given range` `def` `cntEvenSumPairs(X, Y):` ` ` ` ` `# Stores the count of even` ` ` `# numbers between 1 to X` ` ` `cntXEvenNums ` `=` `X ` `/` `2` ` ` `# Stores the count of odd` ` ` `# numbers between 1 to X` ` ` `cntXOddNums ` `=` `(X ` `+` `1` `) ` `/` `2` ` ` `# Stores the count of even` ` ` `# numbers between 1 to Y` ` ` `cntYEvenNums ` `=` `Y ` `/` `2` ` ` `# Stores the count of odd` ` ` `# numbers between 1 to Y` ` ` `cntYOddNums ` `=` `(Y ` `+` `1` `) ` `/` `2` ` ` `# Stores the count of` ` ` `# pairs having even sum` ` ` `cntPairs ` `=` `((cntXEvenNums ` `*` `cntYEvenNums) ` `+` ` ` `(cntXOddNums ` `*` `cntYOddNums))` ` ` ` ` `# Returns the count of pairs` ` ` `# having even sum` ` ` `return` `cntPairs` `# Driver code` `X ` `=` `2` `Y ` `=` `3` `print` `(cntEvenSumPairs(X, Y))` `# This code is contributed by hemanth gadarla` |

## C#

`// C# program to implement` `// the above approach` `using` `System;` `class` `GFG{` `// Function to count maximum even` `// sum pairs in the given range` `static` `long` `cntEvenSumPairs(` `long` `X, ` `long` `Y)` `{` ` ` ` ` `// Stores the count of even` ` ` `// numbers between 1 to X` ` ` `long` `cntXEvenNums = X / 2;` ` ` `// Stores the count of odd` ` ` `// numbers between 1 to X` ` ` `long` `cntXOddNums = (X + 1) / 2;` ` ` `// Stores the count of even` ` ` `// numbers between 1 to Y` ` ` `long` `cntYEvenNums = Y / 2;` ` ` `// Stores the count of odd` ` ` `// numbers between 1 to Y` ` ` `long` `cntYOddNums = (Y + 1) / 2;` ` ` `// Stores the count of` ` ` `// pairs having even sum` ` ` `long` `cntPairs = (cntXEvenNums * cntYEvenNums) +` ` ` `(cntXOddNums * cntYOddNums);` ` ` `// Retuens the count of pairs` ` ` `// having even sum` ` ` `return` `cntPairs;` `}` `// Driver Code` `public` `static` `void` `Main(` `string` `[] args)` `{` ` ` `long` `X = 2;` ` ` `long` `Y = 3;` ` ` `Console.WriteLine(cntEvenSumPairs(X, Y));` `}` `}` `// This code is contributed by chitranayal` |

## Javascript

`<script>` `// Javascript program to implement` `// the above approach ` ` ` `// Function to count maximum even` ` ` `// sum pairs in the given range` ` ` `function` `cntEvenSumPairs(X , Y) {` ` ` `// Stores the count of even` ` ` `// numbers between 1 to X` ` ` `var` `cntXEvenNums = parseInt(X / 2);` ` ` `// Stores the count of odd` ` ` `// numbers between 1 to X` ` ` `var` `cntXOddNums = parseInt((X + 1) / 2);` ` ` `// Stores the count of even` ` ` `// numbers between 1 to Y` ` ` `var` `cntYEvenNums = parseInt(Y / 2);` ` ` `// Stores the count of odd` ` ` `// numbers between 1 to Y` ` ` `var` `cntYOddNums =parseInt( (Y + 1) / 2);` ` ` `// Stores the count of` ` ` `// pairs having even sum` ` ` `var` `cntPairs = (cntXEvenNums * cntYEvenNums) +` ` ` `(cntXOddNums * cntYOddNums);` ` ` `// Retuens the count of pairs` ` ` `// having even sum` ` ` `return` `cntPairs;` ` ` `}` ` ` `// Driver Code` ` ` ` ` `var` `X = 2;` ` ` `var` `Y = 3;` ` ` `document.write(cntEvenSumPairs(X, Y));` `// This code contributed by Rajput-Ji` `</script>` |

**Output:**

3

**Time Complexity** : O(1)**Auxiliary Space** : O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**