Largest number made up of X and Y with count of X divisible by Y and of Y by X
Last Updated :
11 May, 2021
Given three integers X, Y and N, the task is to find the largest number possible of length N consisting only of X and Y as its digits, such that, the count of X‘s in it is divisible by Y and vice-versa. If no such number can be formed, print -1.
Examples:
Input: N = 3, X = 5, Y = 3
Output: 555
Explanation:
Count of 5’s = 3, which is divisible by 3
Count of 3’s = 0
Input: N = 4, X = 7, Y = 5
Output: -1
Approach:
Follow the steps below to solve the problem:
- Consider the larger of X and Y as X and smaller as Y.
- Since, the number needs to of length N, perform the following two steps until N ? 0:
- If N is divisible by Y, append X, N times to the answer and reduce N to zero.
- Otherwise, reduce N by X and append Y, X times to the answer.
- After completion of the above step, if N <0, then a number of required type is not possible. Print -1.
- Otherwise, print the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void largestNumber( int n, int X, int Y)
{
int maxm = max(X, Y);
Y = X + Y - maxm;
X = maxm;
int Xs = 0;
int Ys = 0;
while (n > 0) {
if (n % Y == 0) {
Xs += n;
n = 0;
}
else {
n -= X;
Ys += X;
}
}
if (n == 0) {
while (Xs-- > 0)
cout << X;
while (Ys-- > 0)
cout << Y;
}
else
cout << "-1" ;
}
int main()
{
int n = 19, X = 7, Y = 5;
largestNumber(n, X, Y);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static void largestNumber( int n, int X,
int Y)
{
int maxm = Math.max(X, Y);
Y = X + Y - maxm;
X = maxm;
int Xs = 0 ;
int Ys = 0 ;
while (n > 0 )
{
if (n % Y == 0 )
{
Xs += n;
n = 0 ;
}
else
{
n -= X;
Ys += X;
}
}
if (n == 0 )
{
while (Xs-- > 0 )
System.out.print(X);
while (Ys-- > 0 )
System.out.print(Y);
}
else
System.out.print( "-1" );
}
public static void main (String[] args)
{
int n = 19 , X = 7 , Y = 5 ;
largestNumber(n, X, Y);
}
}
|
Python3
def largestNumber(n, X, Y):
maxm = max (X, Y)
Y = X + Y - maxm
X = maxm
Xs = 0
Ys = 0
while (n > 0 ):
if (n % Y = = 0 ):
Xs + = n
n = 0
else :
n - = X
Ys + = X
if (n = = 0 ):
while (Xs > 0 ):
Xs - = 1
print (X, end = '')
while (Ys > 0 ):
Ys - = 1
print (Y, end = '')
else :
print ( "-1" )
n = 19
X = 7
Y = 5
largestNumber(n, X, Y)
|
C#
using System;
class GFG{
public static void largestNumber( int n, int X,
int Y)
{
int maxm = Math.Max(X, Y);
Y = X + Y - maxm;
X = maxm;
int Xs = 0;
int Ys = 0;
while (n > 0)
{
if (n % Y == 0)
{
Xs += n;
n = 0;
}
else
{
n -= X;
Ys += X;
}
}
if (n == 0)
{
while (Xs-- > 0)
Console.Write(X);
while (Ys-- > 0)
Console.Write(Y);
}
else
Console.Write( "-1" );
}
public static void Main (String[] args)
{
int n = 19, X = 7, Y = 5;
largestNumber(n, X, Y);
}
}
|
Javascript
<script>
function largestNumber(n, X, Y)
{
let maxm = Math.max(X, Y);
Y = X + Y - maxm;
X = maxm;
let Xs = 0;
let Ys = 0;
while (n > 0)
{
if (n % Y == 0)
{
Xs += n;
n = 0;
}
else
{
n -= X;
Ys += X;
}
}
if (n == 0)
{
while (Xs-- > 0)
document.write(X);
while (Ys-- > 0)
document.write(Y);
}
else
document.write( "-1" );
}
let n = 19, X = 7, Y = 5;
largestNumber(n, X, Y);
</script>
|
Output:
7777755555555555555
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...