Count of distinct remainders when N is divided by all the numbers from the range [1, N]
Last Updated :
18 Mar, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int distinctRemainders( int n)
{
if (n % 2 == 0)
return (n / 2);
return (1 + (n / 2));
}
int main()
{
int n = 5;
cout << distinctRemainders(n);
return 0;
}
|
Java
class GFG
{
static int distinctRemainders( int n)
{
if (n % 2 == 0 )
return (n / 2 );
return ( 1 + (n / 2 ));
}
public static void main(String[] args)
{
int n = 5 ;
System.out.println(distinctRemainders(n));
}
}
|
Python3
def distinctRemainders(n):
if n % 2 = = 0 :
return n / / 2
return ((n / / 2 ) + 1 )
if __name__ = = "__main__" :
n = 5
print (distinctRemainders(n))
|
C#
using System;
class GFG
{
static int distinctRemainders( int n)
{
if (n % 2 == 0)
return (n / 2);
return (1 + (n / 2));
}
public static void Main()
{
int n = 5;
Console.WriteLine(distinctRemainders(n));
}
}
|
Javascript
<script>
function distinctRemainders(n)
{
if (n % 2 == 0)
return parseInt(n / 2);
return (1 + parseInt(n / 2));
}
let n = 5;
document.write(distinctRemainders(n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...