Minimum number possible such that it has X digits and it grows Y times by rotation
Last Updated :
25 Feb, 2022
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++
// 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
// 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
# 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#
// 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.
JavaScript
<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>
Time Complexity: O(X*Y)
Auxiliary Space: O(X)
Similar Reads
Maximum value possible by rotating digits of a given number Given a positive integer N, the task is to find the maximum value among all the rotations of the digits of the integer N. Examples: Input: N = 657Output: 765Explanation: All rotations of 657 are {657, 576, 765}. The maximum value among all these rotations is 765. Input: N = 7092Output: 9270Explanati
9 min read
Javascript Program to Find Maximum value possible by rotating digits of a given number Given a positive integer N, the task is to find the maximum value among all the rotations of the digits of the integer N. Examples: Input: N = 657Output: 765Explanation: All rotations of 657 are {657, 576, 765}. The maximum value among all these rotations is 765. Input: N = 7092Output: 9270Explanati
2 min read
Find the minimum possible sum that can be made by the digits of given String Given a string S of length N along with K, the task is to output the minimum sum of suffix after making the longest prefix of zeros that can be obtained by applying the given operation at most K times. Then you can apply the given operation on S: Choose an index let's say i (1 <= i <= N)Do thi
15+ min read
Minimum number to be added to all digits of X to make X > Y Given two numbers X and Y of the same length, the task is to find the minimum number d that needs to be added to all the digits of X such that it becomes larger than Y.Examples: Input: X = 123, Y = 222 Output: 1 Explanation: Add 1 to all digits of X Then X becomes {2, 3, 4} which is lexicographicall
7 min read
Generating numbers that are divisor of their right-rotations Given a number m, find all numbers which have m digits and are a divisor of their right rotation. Right-rotation of a number N is the result of rotating the digits of N one place to the right and wrapping the least significant digit around so that it becomes the most significant digit. For example,
10 min read