Sunday, September 22, 2024
Google search engine
HomeData Modelling & AICount of distinct remainders when N is divided by all the numbers...

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:
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;
}


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


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))


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


Javascript




<script>
 
// Javascript 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]
function distinctRemainders(n)
{
 
    // If n is even
    if (n % 2 == 0)
        return parseInt(n / 2);
 
    // If n is odd
    return (1 + parseInt(n / 2));
}
 
// Driver code
    let n = 5;
 
    document.write(distinctRemainders(n));
 
</script>


Output: 

3

 

Time Complexity: O(1)

Auxiliary Space: O(1)
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments