Count numbers less than N whose modulo with A is equal to B
Given three non-negative integers A, B, and N where A is non-zero, the task is to find the number of integers less than or equal to N whose modulo with A gives the value B.
Examples:
Input: A = 6, B = 3, N = 15
Output: 3
Explanation: The numbers 3, 9 and 15 are less than or equal to N (= 15) and their modulo with A (= 6) is equal to B (= 3). Therefore, the count such numbers is 3.Input: A = 4, B = 1, C = 8
Output: 2
Approach: The given problem can be solved by using an observation based on mathematics. Follow the steps below to solve the problem:
- If the value of B is at least A, then print 0, as there can’t be any such numbers whose modulo with A gives the value B.
- Otherwise, If the value of B is 0, then print the value of C / A as the count of such numbers whose modulo with A gives the value B.
- Otherwise, perform the following steps:
- Initialize a variable, say ans, with the floor value of C / A.
- If the value of (ans * A + B) is less than or equal to N, then increment the value of ans by 1.
- After completing the above steps, print the value of ans as the count of such numbers whose modulo with A gives the value B.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to count numbers less than// N, whose modulo with A gives Bvoid countValues(int A, int B, int C){ // If the value of B at least A if (B >= A) { cout << 0; return; } // If the value of B is 0 or not if (B == 0) { cout << C / A; return; } // Stores the resultant count // of numbers less than N int ans = C / A; if (ans * A + B <= C) { // Update the value of ans ans++; } // Print the value of ans cout << ans;}// Driver Codeint main(){ int A = 6, B = 3, N = 15; countValues(A, B, N); return 0;} |
Java
// Java program for the above approachpublic class MyClass{ // Function to count numbers less than// N, whose modulo with A gives Bstatic void countValues(int A, int B, int C){ // If the value of B at least A if (B >= A) { System.out.println(0); return; } // If the value of B is 0 or not if (B == 0) { System.out.println(C / A); return; } // Stores the resultant count // of numbers less than N int ans = C / A; if (ans * A + B <= C) { // Update the value of ans ans++; } // Print the value of ans System.out.println(ans);}// Driver Codepublic static void main(String args[]){ int A = 6, B = 3, N = 15; countValues(A, B, N);} }// This code in contributed by SoumikMondal |
Python3
# Python3 program for the above approach# Function to count numbers less than# N, whose modulo with A gives Bdef countValues(A, B, C): # If the value of B at least A if (B >= A): print(0) return # If the value of B is 0 or not if (B == 0): print(C // A) return # Stores the resultant count # of numbers less than N ans = C//A if (ans * A + B <= C): # Update the value of ans ans += 1 # Print the value of ans print(ans)# Driver Codeif __name__ == '__main__': A = 6 B = 3 N = 15 countValues(A, B, N)# This code is contributed by SURENDRA_GANGWAR |
C#
// C# program for the above approachusing System;using System.Collections.Generic;class GFG{ // Function to count numbers less than// N, whose modulo with A gives Bstatic void countValues(int A, int B, int C){ // If the value of B at least A if (B >= A) { Console.Write(0); return; } // If the value of B is 0 or not if (B == 0) { Console.Write(C / A); return; } // Stores the resultant count // of numbers less than N int ans = C / A; if (ans * A + B <= C) { // Update the value of ans ans++; } // Print the value of ans Console.Write(ans);}// Driver codepublic static void Main(){ int A = 6, B = 3, N = 15; countValues(A, B, N);}}// This code is contributed by sanjoy_62 |
Javascript
<script>// JavaScript program for the above approach// Function to count numbers less than// N, whose modulo with A gives Bfunction countValues(A, B, C){ // If the value of B at least A if (B >= A) { document.write(0); return; } // If the value of B is 0 or not if (B == 0) { document.write(parseInt(C / A)); return; } // Stores the resultant count // of numbers less than N let ans = parseInt(C / A); if (ans * A + B <= C) { // Update the value of ans ans++; } // Print the value of ans document.write(ans);}// Driver Code let A = 6, B = 3, N = 15; countValues(A, B, N);</script> |
Output:
3
Time Complexity: O(1)
Auxiliary Space: O(1)


