Given three numbers b, x, n. The task is to find the values of ‘a’ in equation (a+b) <= n such that a+b is divisible by x. If no such values are possible then print -1.
Examples:
Input: b = 10, x = 6, n = 40 Output: 2 8 14 20 26 Input: b = 10, x = 1, n = 10 Output: -1
Approach: One can find the least possible value (b/x + 1)*x – b. Then we increase answer by x until it is not greater than n. Here (b/x + 1)*x is the least possible value which is divisible by x.
Below is the implementation of above approach:
C++
// CPP program to Find values of a, in equation // (a+b)<=n and a+b is divisible by x. #include <bits/stdc++.h> using namespace std; // function to Find values of a, in equation // (a+b)<=n and a+b is divisible by x. void PossibleValues(int b, int x, int n) { // least possible which is divisible by x int leastdivisible = (b / x + 1) * x; int flag = 1; // run a loop to get required answer while (leastdivisible <= n) { if (leastdivisible - b >= 1) { cout << leastdivisible - b << " "; // increase value by x leastdivisible += x; // answer is possible flag = 0; } else break; } if (flag) cout << -1; } // Driver code int main() { int b = 10, x = 6, n = 40; // function call PossibleValues(b, x, n); return 0; } |
Java
// Java program to Find values of a, in equation // (a+b)<=n and a+b is divisible by x. import java.io.*; class GFG { // function to Find values of a, in equation // (a+b)<=n and a+b is divisible by x. static void PossibleValues(int b, int x, int n) { // least possible which is divisible by x int leastdivisible = (b / x + 1) * x; int flag = 1; // run a loop to get required answer while (leastdivisible <= n) { if (leastdivisible - b >= 1) { System.out.print( leastdivisible - b + " "); // increase value by x leastdivisible += x; // answer is possible flag = 0; } else break; } if (flag>0) System.out.println(-1); } // Driver code public static void main (String[] args) { int b = 10, x = 6, n = 40; // function call PossibleValues(b, x, n); } } // This code is contributed // by shs |
Python3
# Python3 program to Find values of a, in equation # (a+b)<=n and a+b is divisible by x. # function to Find values of a, in equation # (a+b)<=n and a+b is divisible by x. def PossibleValues(b, x, n) : # least possible which is divisible by x leastdivisible = int(b / x + 1) * x flag = 1 # run a loop to get required answer while (leastdivisible <= n) : if (leastdivisible - b >= 1) : print(leastdivisible - b ,end= " ") # increase value by x leastdivisible += x # answer is possible flag = 0 else : break if (flag != 0) : print(-1) # Driver code if __name__=='__main__': b = 10 x = 6 n = 40# function call PossibleValues(b, x, n) # This code is contributed by # Smitha Dinesh Semwal |
C#
// C# program to Find values of a, // in equation (a+b)<=n and a+b // is divisible by x. using System; class GFG { // function to Find values // of a, in equation (a+b)<=n // and a+b is divisible by x. static void PossibleValues(int b, int x, int n) { // least possible which // is divisible by x int leastdivisible = (b / x + 1) * x; int flag = 1; // run a loop to get required answer while (leastdivisible <= n) { if (leastdivisible - b >= 1) { Console.Write( leastdivisible - b + " "); // increase value by x leastdivisible += x; // answer is possible flag = 0; } else break; } if (flag > 0) Console.WriteLine(-1); } // Driver code public static void Main () { int b = 10, x = 6, n = 40; // function call PossibleValues(b, x, n); } } // This code is contributed by Shubadeep |
PHP
<?php // PHP program to Find values of a, // in equation (a+b)<=n and a+b is // divisible by x. // function to Find values of a, // in equation (a+b)<=n and a+b // is divisible by x. function PossibleValues($b, $x, $n) { // least possible which is // divisible by x $leastdivisible = (intval($b / $x) + 1) * $x; $flag = 1; // run a loop to get required answer while ($leastdivisible <= $n) { if ($leastdivisible - $b >= 1) { echo $leastdivisible - $b . " "; // increase value by x $leastdivisible += $x; // answer is possible $flag = 0; } else break; } if ($flag) echo "-1"; } // Driver code $b = 10; $x = 6; $n = 40; // function call PossibleValues($b, $x, $n); // This code is contributed // by ChitraNayal ?> |
2 8 14 20 26
Recommended Posts:
- Number of sextuplets (or six values) that satisfy an equation
- All possible values of floor(N/K) for all values of K
- Find permutation of n which is divisible by 3 but not divisible by 6
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Absolute difference between sum and product of roots of a quartic equation
- Equation of circle from centre and radius
- Equation of ellipse from its focus, directrix, and eccentricity
- Satisfy the parabola when point (A, B) and the equation is given
- Sum of minimum value of x and y satisfying the equation ax + by = c
- Find the equation of plane which passes through two points and parallel to a given axis
- Find number of solutions of a linear equation of n variables
- Runge-Kutta 4th Order Method to Solve Differential Equation
- Sort an array after applying the given equation
- Number of integral solutions of the equation x1 + x2 +.... + xN = k
- Smallest root of the equation x^2 + s(x)*x - n = 0, where s(x) is the sum of digits of root x.
- Euler Method for solving differential equation
- Number of non-negative integral solutions of sum equation
- Solve the Linear Equation of Single Variable
- Number of integral solutions for equation x = b*(sumofdigits(x)^a)+c
- Program to find number of solutions in Quadratic Equation
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.


