Find a pair (a, b) such that Aa + Bb = N
Last Updated :
03 Jun, 2021
Given three integers N, A, and B, the task is to find a pair of positive integers (a, b) such that Aa + Bb = N.If no such pair exists, print -1.
Examples:
Input: N = 106, A = 3, B = 5
Output: 4 2
Explanation: Pair (4, 2) satisfies the answer i.e., 34+52 is equal to 106
Input: N = 60467200, A = 6, B = 4
Output: 10 5
Explanation: Pair (10, 5) satisfies the answer i.e., 610+45 is equal to 60467200
Approach: The idea is to calculate logAN and logBN and check for every pair (i, j) (0 ≤ i ≤ logAN and 0 ≤ j ≤ logBN ), whether Ai + Bj is equal to N or not. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int power( long long int A, long long int N)
{
int count = 0;
if (A == 1)
return 0;
while (N) {
count++;
N /= A;
}
return count;
}
void Pairs( long long int N, long long int A,
long long int B)
{
int powerA, powerB;
powerA = power(A, N);
powerB = power(B, N);
long long int intialB = B, intialA = A;
A = 1;
for ( int i = 0; i <= powerA; i++) {
B = 1;
for ( int j = 0; j <= powerB; j++) {
if (B == N - A) {
cout << i << " " << j << endl;
return ;
}
B *= intialB;
}
A *= intialA;
}
cout << -1 << endl;
return ;
}
int main()
{
long long int N = 106, A = 3, B = 5;
Pairs(N, A, B);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int power( int A, int N)
{
int count = 0 ;
if (A == 1 )
return 0 ;
while (N > 0 )
{
count++;
N /= A;
}
return count;
}
static void Pairs( int N, int A, int B)
{
int powerA, powerB;
powerA = power(A, N);
powerB = power(B, N);
int intialB = B, intialA = A;
A = 1 ;
for ( int i = 0 ; i <= powerA; i++)
{
B = 1 ;
for ( int j = 0 ; j <= powerB; j++)
{
if (B == N - A)
{
System.out.println(i + " " + j);
return ;
}
B *= intialB;
}
A *= intialA;
}
System.out.println( "-1" );
return ;
}
public static void main(String args[])
{
int N = 106 , A = 3 , B = 5 ;
Pairs(N, A, B);
}
}
|
Python3
def power(A, N):
count = 0 ;
if (A = = 1 ):
return 0 ;
while (N > 0 ):
count + = 1 ;
N / / = A;
return int (count);
def Pairs(N, A, B):
powerA, powerB = 0 , 0 ;
powerA = power(A, N);
powerB = power(B, N);
intialB = B;
intialA = A;
A = 1 ;
for i in range (powerA + 1 ):
B = 1 ;
for j in range (powerB + 1 ):
if (B = = N - A):
print (i , " " , j);
return ;
B * = intialB;
A * = intialA;
print ( "-1" );
return ;
if __name__ = = '__main__' :
N = 106 ;
A = 3 ;
B = 5 ;
Pairs(N, A, B);
|
C#
using System;
class GFG
{
static int power( int A, int N)
{
int count = 0;
if (A == 1)
return 0;
while (N > 0)
{
count++;
N /= A;
}
return count;
}
static void Pairs( int N, int A, int B)
{
int powerA, powerB;
powerA = power(A, N);
powerB = power(B, N);
int intialB = B, intialA = A;
A = 1;
for ( int i = 0; i <= powerA; i++)
{
B = 1;
for ( int j = 0; j <= powerB; j++)
{
if (B == N - A)
{
Console.WriteLine(i + " " + j);
return ;
}
B *= intialB;
}
A *= intialA;
}
Console.WriteLine( "-1" );
return ;
}
public static void Main(String []args)
{
int N = 106, A = 3, B = 5;
Pairs(N, A, B);
}
}
|
Javascript
<script>
function power(A, N)
{
let count = 0;
if (A == 1)
return 0;
while (N > 0)
{
count++;
N /= A;
}
return count;
}
function Pairs(N, A, B)
{
let powerA, powerB;
powerA = power(A, N);
powerB = power(B, N);
let letialB = B, letialA = A;
A = 1;
for (let i = 0; i <= powerA; i++)
{
B = 1;
for (let j = 0; j <= powerB; j++)
{
if (B == N - A)
{
document.write(i + " " + j);
return ;
}
B *= letialB;
}
A *= letialA;
}
document.write( "-1" );
return ;
}
let N = 106, A = 3, B = 5;
Pairs(N, A, B);
</script>
|
Time Complexity: O((logAN)*(logBN))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...