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; } |
C
// C program to Find values of a, in equation // (a+b)<=n and a+b is divisible by x. #include <stdio.h> // 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) { printf ( "%d " ,leastdivisible - b); // increase value by x leastdivisible += x; // answer is possible flag = 0; } else break ; } if (flag) printf ( "%d" ,-1); } // Driver code int main() { int b = 10, x = 6, n = 40; // function call PossibleValues(b, x, n); return 0; } // This code is contributed by kothavvsaakash. |
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 ?> |
Javascript
<script> // Javascript 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 let leastdivisible = (Math.floor(b / x) + 1) * x; let flag = 1; // run a loop to get required answer while (leastdivisible <= n) { if (leastdivisible - b >= 1) { document.write( leastdivisible - b + " " ); // increase value by x leastdivisible += x; // answer is possible flag = 0; } else break ; } if (flag>0) document.write(-1+ "<br>" ); } // Driver code let b = 10, x = 6, n = 40; // function call PossibleValues(b, x, n); // This code is contributed by rag2127 </script> |
2 8 14 20 26
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!