Given an integer **N**, the task is to find the count of total distinct remainders which can be obtained when **N** is divided by every element from the range **[1, N]**.

**Examples:**

Input:N = 5

Output:3

5 % 1 = 0

5 % 2 = 1

5 % 3 = 2

5 % 4 = 1

5 % 5 = 0

The distinct remainders are 0, 1 and 2.

Input:N = 44

Output:22

**Approach:** It can be easily observed that for even values of **N** the number of distinct remainders will be **N / 2** and for odd values of **N** it will be **1 + ⌊N / 2⌋**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count of distinct ` `// remainders that can be obtained when ` `// n is divided by every element ` `// from the range [1, n] ` `int` `distinctRemainders(` `int` `n) ` `{ ` ` ` ` ` `// If n is even ` ` ` `if` `(n % 2 == 0) ` ` ` `return` `(n / 2); ` ` ` ` ` `// If n is odd ` ` ` `return` `(1 + (n / 2)); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` ` ` `cout << distinctRemainders(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `class` `GFG ` `{ ` ` ` `// Function to return the count of distinct ` `// remainders that can be obtained when ` `// n is divided by every element ` `// from the range [1, n] ` `static` `int` `distinctRemainders(` `int` `n) ` `{ ` ` ` ` ` `// If n is even ` ` ` `if` `(n % ` `2` `== ` `0` `) ` ` ` `return` `(n / ` `2` `); ` ` ` ` ` `// If n is odd ` ` ` `return` `(` `1` `+ (n / ` `2` `)); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` `System.out.println(distinctRemainders(n)); ` `} ` `} ` ` ` `// This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count of distinct ` `# remainders that can be obtained when ` `# n is divided by every element ` `# from the range [1, n] ` `def` `distinctRemainders(n): ` ` ` ` ` `# If n is even ` ` ` `if` `n ` `%` `2` `=` `=` `0` `: ` ` ` `return` `n` `/` `/` `2` ` ` ` ` `# If n is odd ` ` ` `return` `((n` `/` `/` `2` `)` `+` `1` `) ` ` ` `# Driver code ` `if` `__name__` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `5` ` ` `print` `(distinctRemainders(n)) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the count of distinct ` ` ` `// remainders that can be obtained when ` ` ` `// n is divided by every element ` ` ` `// from the range [1, n] ` ` ` `static` `int` `distinctRemainders(` `int` `n) ` ` ` `{ ` ` ` ` ` `// If n is even ` ` ` `if` `(n % 2 == 0) ` ` ` `return` `(n / 2); ` ` ` ` ` `// If n is odd ` ` ` `return` `(1 + (n / 2)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 5; ` ` ` `Console.WriteLine(distinctRemainders(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(1)

Rated as one of the most sought after skills in the industry, own the basics of coding with our **C++ STL** Course and master the very concepts by intense problem-solving.

## Recommended Posts:

- Find all the possible remainders when N is divided by all positive integers from 1 to N+1
- Check if N leaves only distinct remainders on division by all values up to K
- Minimum operations to make counts of remainders same in an array
- Find all the possible numbers in a range that can be evenly divided by its digits
- Largest number by which given 3 numbers should be divided such that they leaves same remainder
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Count of integers obtained by replacing ? in the given string that give remainder 5 when divided by 13
- Count of Array elements to be divided by 2 to make at least K elements equal
- Count of N-digit numbers with all distinct digits
- Find two distinct numbers such that their LCM lies in given range
- All possible co-prime distinct element pairs within a range [L, R]
- Find minimum number to be divided to make a number a perfect square
- Program to find remainder when large number is divided by 11
- First N natural can be divided into two sets with given difference and co-prime sums
- Newton's Divided Difference Interpolation Formula
- Program to find remainder when large number is divided by r
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts
- Find the sums for which an array can be divided into sub-arrays of equal sum
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Check if an array of 1s and 2s can be divided into 2 parts with equal sum

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.