# Count pairs having distinct sum from a given range

Given two positive integers** L** and **R**, the task is to find the number of pairs with elements from the range **[L, R]**, whose sums are distinct.

**Examples:**

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****.**

Input:L = 2, R = 3Output:3Explanation:

All possible pairs with elements from the range [2, 3] are {(2, 2), (2, 3), (3, 2), (3, 3)}. Since sum of the pairs (2, 3) and (3, 2) are equal, the count of pairs with distinct sum is 3.

Input:L = 2, R = 2Output:1

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

- Initialize a variable, say
**firstNum**, to store the least sum that can be obtained by the pairs, i.e.**2 * L**. - Initialize a variable, say
**lastNum**, to store the highest sum which can be obtained by the pairs, i.e.**2 * R**. - Initialize a variable, say
**cntPairs**, to store the count of pairs having distinct sum, i.e.**lastNum – firstNum + 1**. - Finally, print the value of
**cntPairs**.

Below is the implementation of the above approach:

## C++

`// C++ program for` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to count pairs made` `// up of elements from the range` `// [L, R] having distinct sum` `long` `countPairs(` `long` `L, ` `long` `R)` `{` ` ` `// Stores the least sum which` ` ` `// can be formed by the pairs` ` ` `long` `firstNum = 2 * L;` ` ` `// Stores the highest sum which` ` ` `// can be formed by the pairs` ` ` `long` `lastNum = 2 * R;` ` ` `// Stores the count of pairs` ` ` `// having distinct sum` ` ` `long` `Cntpairs = lastNum - firstNum + 1;` ` ` `// Print the count of pairs` ` ` `cout << Cntpairs;` `}` `// Driver Code` `int` `main()` `{` ` ` `long` `L = 2, R = 3;` ` ` `// Function call to count` ` ` `// the number of pairs` ` ` `// having distinct sum in` ` ` `// the range [L, R]` ` ` `countPairs(L, R);` ` ` `return` `0;` `}` |

## Java

`// Java program for` `// the above approach` `import` `java.io.*;` `class` `GFG {` ` ` `// Function to count pairs made` ` ` `// up of elements from the range` ` ` `// [L, R] having distinct sum` ` ` `static` `void` `countPairs(` `long` `L, ` `long` `R)` ` ` `{` ` ` `// Stores the least sum which` ` ` `// can be formed by the pairs` ` ` `long` `firstNum = ` `2` `* L;` ` ` `// Stores the highest sum which` ` ` `// can be formed by the pairs` ` ` `long` `lastNum = ` `2` `* R;` ` ` `// Stores the count of pairs` ` ` `// having distinct sum` ` ` `long` `Cntpairs = lastNum - firstNum + ` `1` `;` ` ` `// Print the count of pairs` ` ` `System.out.println(Cntpairs);` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `long` `L = ` `2` `, R = ` `3` `;` ` ` `// Function call to count` ` ` `// the number of pairs` ` ` `// having distinct sum in` ` ` `// the range [L, R]` ` ` `countPairs(L, R);` ` ` `}` `}` |

## Python3

`# Python3 program for` `# the above approach` `# Function to count pairs made` `# up of elements from the range` `# [L, R] having distinct sum` `def` `countPairs(L, R):` ` ` `# Stores the least sum which` ` ` `# can be formed by the pairs` ` ` `firstNum ` `=` `2` `*` `L` ` ` `# Stores the highest sum which` ` ` `# can be formed by the pairs` ` ` `lastNum ` `=` `2` `*` `R` ` ` `# Stores the count of pairs` ` ` `# having distinct sum` ` ` `Cntpairs ` `=` `lastNum ` `-` `firstNum ` `+` `1` ` ` `# Print the count of pairs` ` ` `print` `(Cntpairs)` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `L,R ` `=` `2` `,` `3` ` ` `# Function call to count` ` ` `# the number of pairs` ` ` `# having distinct sum in` ` ` `# the range [L, R]` ` ` `countPairs(L, R)` ` ` `# This code is contributed by mohit kumar 29.` |

## C#

`// C# program for` `// the above approach` `using` `System;` `public` `class` `GFG {` ` ` `// Function to count pairs made` ` ` `// up of elements from the range` ` ` `// [L, R] having distinct sum` ` ` `static` `void` `countPairs(` `long` `L, ` `long` `R)` ` ` `{` ` ` `// Stores the least sum which` ` ` `// can be formed by the pairs` ` ` `long` `firstNum = 2 * L;` ` ` `// Stores the highest sum which` ` ` `// can be formed by the pairs` ` ` `long` `lastNum = 2 * R;` ` ` `// Stores the count of pairs` ` ` `// having distinct sum` ` ` `long` `Cntpairs = lastNum - firstNum + 1;` ` ` `Console.WriteLine(Cntpairs);` ` ` `}` ` ` `// Driver Code` ` ` `static` `public` `void` `Main()` ` ` `{` ` ` `long` `L = 2, R = 3;` ` ` `// Function call to count` ` ` `// the number of pairs` ` ` `// having distinct sum in` ` ` `// the range [L, R]` ` ` `countPairs(L, R);` ` ` `}` `}` |

## Javascript

`<script>` `// JavaScript program for` `// the above approach` `// Function to count pairs made` `// up of elements from the range` `// [L, R] having distinct sum` `function` `countPairs(L,R)` `{` ` ` `// Stores the least sum which` ` ` `// can be formed by the pairs` ` ` `let firstNum = 2 * L;` ` ` ` ` `// Stores the highest sum which` ` ` `// can be formed by the pairs` ` ` `let lastNum = 2 * R;` ` ` ` ` `// Stores the count of pairs` ` ` `// having distinct sum` ` ` `let Cntpairs = lastNum - firstNum + 1;` ` ` ` ` `// Print the count of pairs` ` ` `document.write(Cntpairs+` `"<br>"` `);` `}` `// Driver Code` `let L = 2, R = 3;` ` ` `// Function call to count` `// the number of pairs` `// having distinct sum in` `// the range [L, R]` `countPairs(L, R);` `// This code is contributed by unknown2108` `</script>` |

**Output:**

3

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