Given an **integer N**, the task is to count of decreasing pairs from numbers 1 to N.

A pair (x, y) is said to be decreasing if x > y

**Examples:**

Input:N = 8

Output:3

Explanation:

Decreasing pairs are: (7, 1), (6, 2), (5, 3).

Input:N = 9

Output:4

Explanation:

Decreasing pairs are: (8, 1), (7, 2), (6, 3), (5, 4).

**Approach:** Consider the below cases:

If N = 1 => Count = 0

If N = 2 => Count = 1 {(2, 1)}

If N = 3 => Count = 1 {(3, 1) or (3, 2)}

If N = 4 => Count = 2 {(4, 3), (2, 1)}

If N = 5 => Count = 2 {(5, 4), (3, 2)}

If N = 6 => Count = 3 {(6, 5), (4, 3), (2, 1)}

.

.

and so on

It can be clearly observed that

Below is the implementation of the above approach:

## C++

`// C++ program to count decreasing ` `// pairs formed from numbers 1 to N ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count the ` `// possible number of pairs ` `int` `divParts(` `int` `N) ` `{ ` ` ` `if` `(N % 2 == 0) ` ` ` ` ` `// if the number is even ` ` ` `// then the answer in (N/2)-1 ` ` ` `cout << (N / 2) - 1 << endl; ` ` ` ` ` `else` ` ` `// if the number is odd ` ` ` `// then the answer in N/2 ` ` ` `cout << N / 2 << endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 8; ` ` ` ` ` `divParts(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count decreasing ` `// pairs formed from numbers 1 to N ` `import` `java.util.*; ` `class` `GFG{ ` ` ` `// Function to count the ` `// possible number of pairs ` `static` `void` `divParts(` `int` `N) ` `{ ` ` ` `if` `(N % ` `2` `== ` `0` `) ` ` ` ` ` `// if the number is even ` ` ` `// then the answer in (N/2)-1 ` ` ` `System.out.println((N / ` `2` `) - ` `1` `); ` ` ` ` ` `else` ` ` `// if the number is odd ` ` ` `// then the answer in N/2 ` ` ` `System.out.println((N / ` `2` `)); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `8` `; ` ` ` ` ` `divParts(N); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count decreasing ` `# pairs formed from numbers 1 to N ` ` ` `# Function to count the ` `# possible number of pairs ` `def` `divParts(N): ` ` ` ` ` `if` `(N ` `%` `2` `=` `=` `0` `): ` ` ` ` ` `# if the number is even ` ` ` `# then the answer in (N/2)-1 ` ` ` `print` `((N ` `/` `2` `) ` `-` `1` `); ` ` ` ` ` `else` `: ` ` ` ` ` `# if the number is odd ` ` ` `# then the answer in N/2 ` ` ` `print` `(N ` `/` `2` `); ` ` ` `# Driver code ` `N ` `=` `8` `; ` `divParts(N); ` ` ` `# This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count decreasing ` `// pairs formed from numbers 1 to N ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to count the ` `// possible number of pairs ` `static` `void` `divParts(` `int` `N) ` `{ ` ` ` `if` `(N % 2 == 0) ` ` ` ` ` `// if the number is even ` ` ` `// then the answer in (N/2)-1 ` ` ` `Console.WriteLine((N / 2) - 1); ` ` ` ` ` `else` ` ` `// if the number is odd ` ` ` `// then the answer in N/2 ` ` ` `Console.WriteLine((N / 2)); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 8; ` ` ` ` ` `divParts(N); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

3

## Recommended Posts:

- Count of non decreasing arrays of length N formed with values in range L to R
- Count non decreasing subarrays of size N from N Natural numbers
- Count numbers formed by given two digit with sum having given digits
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Count of pairs with sum N from first N natural numbers
- Count pairs of numbers from 1 to N with Product divisible by their Sum
- Count ordered pairs of positive numbers such that their sum is S and XOR is K
- Count possible combinations of pairs with adjacent elements from first N numbers
- Count pairs of natural numbers with GCD equal to given number
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Count permutation such that sequence is non decreasing
- Count permutations that are first decreasing then increasing.
- Total number of non-decreasing numbers with n digits
- Find the count of Strictly decreasing Subarrays
- Maximum factors formed by two numbers
- GCD of two numbers formed by n repeating x and y times
- Sum of sum of all subsets of a set formed by first N natural numbers
- Sum of all subsets of a set formed by first n natural numbers
- Count of substrings formed using a given set of characters only
- N digit numbers divisible by 5 formed from the M digits

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.