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++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to count numbers less than // N, whose modulo with A gives B void 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 Code int main()
{ int A = 6, B = 3, N = 15;
countValues(A, B, N);
return 0;
} |
// Java program for the above approach public class MyClass
{ // Function to count numbers less than // N, whose modulo with A gives B static 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 Code public static void main(String args[])
{ int A = 6 , B = 3 , N = 15 ;
countValues(A, B, N);
} } // This code in contributed by SoumikMondal |
# Python3 program for the above approach # Function to count numbers less than # N, whose modulo with A gives B def 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 Code if __name__ = = '__main__' :
A = 6
B = 3
N = 15
countValues(A, B, N)
# This code is contributed by SURENDRA_GANGWAR |
// C# program for the above approach using System;
using System.Collections.Generic;
class GFG{
// Function to count numbers less than // N, whose modulo with A gives B static 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 code public static void Main()
{ int A = 6, B = 3, N = 15;
countValues(A, B, N);
} } // This code is contributed by sanjoy_62 |
<script> // JavaScript program for the above approach // Function to count numbers less than // N, whose modulo with A gives B function 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> |
3
Time Complexity: O(1)
Auxiliary Space: O(1)