Minimum number possible such that it has X digits and it grows Y times by rotation
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++
#include <bits/stdc++.h>
using namespace std;
void findmin( int X, int Y)
{
vector< int > num;
int a, b;
bool possible = 0;
for ( int j = Y; j < 10; j++) {
num.push_back(j);
a = 0;
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) {
for ( int i = X - 1; i >= 0; i--) {
cout << num[i];
}
}
else
cout << "-1" ;
}
int main()
{
int X = 6;
int Y = 4;
findmin(X, Y);
return 0;
}
|
Java
import java.util.ArrayList;
class GFG {
static void findmin( int X, int Y) {
ArrayList<Integer> num = new ArrayList<Integer>();
int a = 0 , b = 0 ;
boolean possible = false ;
for ( int j = Y; j < 10 ; j++) {
num.add(j);
a = 0 ;
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) {
for ( int i = X - 1 ; i >= 0 ; i--) {
System.out.print(num.get(i));
}
}
else
System.out.print( "-1" );
}
public static void main(String args[]) {
int X = 6 ;
int Y = 4 ;
findmin(X, Y);
}
}
|
Python3
def findmin(X, Y):
num = []
a, b = 0 , 0
possible = 0
for j in range (Y, 10 ):
num.append(j)
a = 0
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):
for i in range (X - 1 , - 1 , - 1 ):
print (num[i], end = "")
else :
print ( "-1" )
if __name__ = = "__main__" :
X = 6
Y = 4
findmin(X, Y)
|
C#
using System;
using System.Collections;
class GFG
{
static void findmin( int X, int Y)
{
ArrayList num = new ArrayList();
int a = 0, b = 0;
bool possible = false ;
for ( int j = Y; j < 10; j++) {
num.Add(j);
a = 0;
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) {
for ( int i = X - 1; i >= 0; i--) {
Console.Write(num[i]);
}
}
else
Console.Write( "-1" );
}
public static void Main()
{
int X = 6;
int Y = 4;
findmin(X, Y);
}
}
|
Javascript
<script>
function findmin(X, Y) {
let num = [];
let a, b;
let possible = 0;
for (let j = Y; j < 10; j++) {
num.push(j);
a = 0;
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) {
for (let i = X - 1; i >= 0; i--) {
document.write(num[i]);
}
}
else
document.write( "-1" )
}
let X = 6;
let Y = 4;
findmin(X, Y);
</script>
|
Time Complexity: O(X*Y)
Auxiliary Space: O(X)
Last Updated :
25 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...