Given two positive integers X and Y. The task is to find the minimum possible number such that the number has X decimal digits & it grows Y times after moving its last digit to first
Examples:
Input: X = 6, Y = 5
Output: 142857
Explanation: Following are the operations performed to get the desired result.
Take last digit of 142857 i.e. 7 and put it forward at the beginning the number becomes 714285.
Which is equal to 142857 * Y = 142857 * 5 = 714285.
Therefore, the number if 714285 which is minimum possible.Input: X = 1, Y = 2
Output: -1
Explanation: The number that consists of a single digit cannot stay what it is when multiplied by 2 so no minimum number possible in this case.
Approach: This solution to this problem is implementation-based. Follow the steps below to solve the given problem.
- Declare a vector num to store the number formed.
- Declare and initialize a boolean variable possible = false, to keep track of whether the number can be formed or not.
- Try to form X digit number with the required condition by Iterating for all the possible numbers using.
- If the number can be formed at any point set possible = true and break from the loop.
- At last, check if possible = true:
- If Yes, print the number stored in num.
- Else No, such number found so return -1.
- Print the final result.
Below is the implementation of the above approach:
// C++ program for above approach #include <bits/stdc++.h> using namespace std;
// Function to find minimum number void findmin( int X, int Y)
{ // Additional space required to form a number
vector< int > num;
int a, b;
bool possible = 0;
// Forming a number
for ( int j = Y; j < 10; j++) {
num.push_back(j);
a = 0;
// Forming X digit number
for ( int i = 0; i < X; i++) {
b = a + num[i] * Y;
a = b / 10;
num.push_back(b - 10 * a);
}
if (num[X] == j && a == 0) {
possible = 1;
break ;
}
}
if (possible) {
// Number can be formed
for ( int i = X - 1; i >= 0; i--) {
cout << num[i];
}
}
// There is no such number possible
else
cout << "-1" ;
} // Driver code int main()
{ int X = 6;
int Y = 4;
// Function Call
findmin(X, Y);
return 0;
} |
// Java program for above approach import java.util.ArrayList;
class GFG {
// Function to find minimum number
static void findmin( int X, int Y) {
// Additional space required to form a number
ArrayList<Integer> num = new ArrayList<Integer>();
int a = 0 , b = 0 ;
boolean possible = false ;
// Forming a number
for ( int j = Y; j < 10 ; j++) {
num.add(j);
a = 0 ;
// Forming X digit number
for ( int i = 0 ; i < X; i++) {
b = a + ( int ) num.get(i) * Y;
a = b / 10 ;
num.add(b - 10 * a);
}
if (( int ) num.get(X) == j && a == 0 ) {
possible = true ;
break ;
}
}
if (possible) {
// Number can be formed
for ( int i = X - 1 ; i >= 0 ; i--) {
System.out.print(num.get(i));
}
}
// There is no such number possible
else
System.out.print( "-1" );
}
// Driver code
public static void main(String args[]) {
int X = 6 ;
int Y = 4 ;
// Function Call
findmin(X, Y);
}
} // This code is contributed by Saurabh Jaiswal |
# python3 program for above approach # Function to find minimum number def findmin(X, Y):
# Additional space required to form a number
num = []
a, b = 0 , 0
possible = 0
# Forming a number
for j in range (Y, 10 ):
num.append(j)
a = 0
# Forming X digit number
for i in range ( 0 , X):
b = a + num[i] * Y
a = b / / 10
num.append(b - 10 * a)
if (num[X] = = j and a = = 0 ):
possible = 1
break
if (possible):
# Number can be formed
for i in range (X - 1 , - 1 , - 1 ):
print (num[i], end = "")
# There is no such number possible
else :
print ( "-1" )
# Driver code if __name__ = = "__main__" :
X = 6
Y = 4
# Function Call
findmin(X, Y)
# This code is contributed by rakeshsahni |
// C# program for above approach using System;
using System.Collections;
class GFG
{ // Function to find minimum number
static void findmin( int X, int Y)
{
// Additional space required to form a number
ArrayList num = new ArrayList();
int a = 0, b = 0;
bool possible = false ;
// Forming a number
for ( int j = Y; j < 10; j++) {
num.Add(j);
a = 0;
// Forming X digit number
for ( int i = 0; i < X; i++) {
b = a + ( int )num[i] * Y;
a = b / 10;
num.Add(b - 10 * a);
}
if (( int )num[X] == j && a == 0) {
possible = true ;
break ;
}
}
if (possible) {
// Number can be formed
for ( int i = X - 1; i >= 0; i--) {
Console.Write(num[i]);
}
}
// There is no such number possible
else
Console.Write( "-1" );
}
// Driver code
public static void Main()
{
int X = 6;
int Y = 4;
// Function Call
findmin(X, Y);
}
} // This code is contributed by Samim Hossain Mondal. |
<script> // JavaScript code for the above approach
// Function to find minimum number
function findmin(X, Y) {
// Additional space required to form a number
let num = [];
let a, b;
let possible = 0;
// Forming a number
for (let j = Y; j < 10; j++) {
num.push(j);
a = 0;
// Forming X digit number
for (let i = 0; i < X; i++) {
b = a + num[i] * Y;
a = Math.floor(b / 10);
num.push(b - 10 * a);
}
if (num[X] == j && a == 0) {
possible = 1;
break ;
}
}
if (possible) {
// Number can be formed
for (let i = X - 1; i >= 0; i--) {
document.write(num[i]);
}
}
// There is no such number possible
else
document.write( "-1" )
}
// Driver code
let X = 6;
let Y = 4;
// Function Call
findmin(X, Y);
// This code is contributed by Potta Lokesh
</script>
|
102564
Time Complexity: O(X*Y)
Auxiliary Space: O(X)