# Count of distinct remainders when N is divided by all the numbers from the range [1, N]

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)

## Recommended Posts:

- Count of numbers having only 1 set bit in the range [0, n]
- Count Odd and Even numbers in a range from L to R
- Count the numbers divisible by 'M' in a given range
- Count of numbers from range [L, R] whose sum of digits is Y
- Count factorial numbers in a given range
- Count numbers in range 1 to N which are divisible by X but not by Y
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count numbers with unit digit k in given range
- Count all the numbers in a range with smallest factor as K
- Sum of numbers in a range [L, R] whose count of divisors is prime
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Count of common multiples of two numbers in a range
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count numbers from range whose prime factors are only 2 and 3

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.