# Print values of ‘a’ in equation (a+b) <= n and a+b is divisible by x

• Last Updated : 25 Aug, 2022

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 ``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 ` `// 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

 `= 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

 ``

Output:

`2 8 14 20 26`

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up