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

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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) *
  
    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 +=
  
            # 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

2 8 14 20 26


My Personal Notes arrow_drop_up

pawanasipugmailcom

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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.