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++
#include <bits/stdc++.h>
using namespace std;
void PossibleValues( int b, int x, int n)
{
int leastdivisible = (b / x + 1) * x;
int flag = 1;
while (leastdivisible <= n) {
if (leastdivisible - b >= 1) {
cout << leastdivisible - b << " " ;
leastdivisible += x;
flag = 0;
}
else
break ;
}
if (flag)
cout << -1;
}
int main()
{
int b = 10, x = 6, n = 40;
PossibleValues(b, x, n);
return 0;
}
|
C
#include <stdio.h>
void PossibleValues( int b, int x, int n)
{
int leastdivisible = (b / x + 1) * x;
int flag = 1;
while (leastdivisible <= n) {
if (leastdivisible - b >= 1) {
printf ( "%d " ,leastdivisible - b);
leastdivisible += x;
flag = 0;
}
else
break ;
}
if (flag)
printf ( "%d" ,-1);
}
int main()
{
int b = 10, x = 6, n = 40;
PossibleValues(b, x, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void PossibleValues( int b, int x, int n)
{
int leastdivisible = (b / x + 1 ) * x;
int flag = 1 ;
while (leastdivisible <= n) {
if (leastdivisible - b >= 1 ) {
System.out.print( leastdivisible - b + " " );
leastdivisible += x;
flag = 0 ;
}
else
break ;
}
if (flag> 0 )
System.out.println(- 1 );
}
public static void main (String[] args) {
int b = 10 , x = 6 , n = 40 ;
PossibleValues(b, x, n);
}
}
|
Python3
def PossibleValues(b, x, n) :
leastdivisible = int (b / x + 1 ) * x
flag = 1
while (leastdivisible < = n) :
if (leastdivisible - b > = 1 ) :
print (leastdivisible - b ,end = " " )
leastdivisible + = x
flag = 0
else :
break
if (flag ! = 0 ) :
print ( - 1 )
if __name__ = = '__main__' :
b = 10
x = 6
n = 40
PossibleValues(b, x, n)
|
C#
using System;
class GFG {
static void PossibleValues( int b, int x, int n)
{
int leastdivisible = (b / x + 1) * x;
int flag = 1;
while (leastdivisible <= n) {
if (leastdivisible - b >= 1) {
Console.Write( leastdivisible - b + " " );
leastdivisible += x;
flag = 0;
}
else
break ;
}
if (flag > 0)
Console.WriteLine(-1);
}
public static void Main ()
{
int b = 10, x = 6, n = 40;
PossibleValues(b, x, n);
}
}
|
PHP
<?php
function PossibleValues( $b , $x , $n )
{
$leastdivisible = ( intval ( $b / $x ) + 1) * $x ;
$flag = 1;
while ( $leastdivisible <= $n )
{
if ( $leastdivisible - $b >= 1)
{
echo $leastdivisible - $b . " " ;
$leastdivisible += $x ;
$flag = 0;
}
else
break ;
}
if ( $flag )
echo "-1" ;
}
$b = 10;
$x = 6;
$n = 40;
PossibleValues( $b , $x , $n );
?>
|
Javascript
<script>
function PossibleValues(b,x,n)
{
let leastdivisible = (Math.floor(b / x) + 1) * x;
let flag = 1;
while (leastdivisible <= n) {
if (leastdivisible - b >= 1) {
document.write( leastdivisible - b + " " );
leastdivisible += x;
flag = 0;
}
else
break ;
}
if (flag>0)
document.write(-1+ "<br>" );
}
let b = 10, x = 6, n = 40;
PossibleValues(b, x, n);
</script>
|
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...