给定两个具有相同长度的数字X和Y ,任务是找到需要加到X的所有数字上的最小数字d ,以使其变得大于Y。
例子:
Input: X = 123, Y = 222
Output: 1
Explanation:
Add 1 to all digits of X
Then X becomes {2, 3, 4} which is
lexicographically larger than {2, 2, 2}.
Input: X = 4512, Y = 2998
Output: 0
Explanation:
X is already lexicographically larger than Y
so the answer will be 0.
方法:将此问题分为三种情况可以轻松解决
- 情况1:查找X在字典上是否已经大于Y。如果是,那么我们无需执行任何操作。
- 情况2:否则,将(Y [0] – X [0])添加到X的所有元素,然后检查X在字典上是否大于Y。
- 情况3:如果它仍然不更大,则答案将是(Y [0] – X [0])+1,因为X的第一个元素变得大于Y的第一个元素意味着X [0]> Y [0] 。
下面是上述方法的实现:
C++
// C++ program to find Minimum number to be added
// to all digits of X to make X > Y
#include
using namespace std;
// Function to check if X
// is lexicographically larger Y
bool IsLarger(int X[], int Y[], int n)
{
for (int i = 0; i < n; i++) {
// It is lexicographically larger
if (X[i] < Y[i]) {
return false;
}
}
return true;
}
// Utility function to check
// minimum value of d
int solve(int X[], int Y[], int n)
{
int ans = 0;
// If X is already larger
// do not need to add anything
if (IsLarger(X, Y, n)) {
ans = 0;
}
else {
// Adding d to all elements of X
int d = Y[0] - X[0];
for (int i = 0; i < n; i++) {
X[i] += d;
}
// If X is larger now
// print d
if (IsLarger(X, Y, n)) {
ans = d;
}
// else print d + 1
else {
ans = d + 1;
}
}
return ans;
}
// Driver Code
int main()
{
// Taking the numbers as sequences
int X[] = { 2, 3, 6, 9 };
int Y[] = { 3, 4, 8, 1 };
int n = sizeof(X) / sizeof(X[0]);
cout << solve(X, Y, n);
return 0;
}
Java
// Java program to find Minimum number to be added
// to all digits of X to make X > Y
import java.util.*;
class GFG
{
// Function to check if X
// is lexicographically larger Y
static boolean IsLarger(int[] X,
int[] Y, int n)
{
for (int i = 0; i < n; i++)
{
// It is lexicographically larger
if (X[i] < Y[i])
{
return false;
}
}
return true;
}
// Utility function to check
// minimum value of d
static int solve(int X[], int Y[], int n)
{
int ans = 0;
// If X is already larger
// do not need to add anything
if (IsLarger(X, Y, n))
ans = 0;
else
{
// Adding d to all elements of X
int d = Y[0] - X[0];
for (int i = 0; i < n; i++)
X[i] += d;
// If X is larger now
// print d
if (IsLarger(X, Y, n))
ans = d;
// else print d + 1
else
{
ans = d + 1;
}
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
// Taking the numbers as sequences
int X[] = { 2, 3, 6, 9 };
int Y[] = { 3, 4, 8, 1 };
int n = X.length;
System.out.println(solve(X, Y, n));
}
}
// This code is contributed by
// sanjeev2552
Python3
# Python3 program to find Minimum number to be added
# to all digits of X to make X > Y
# Function to check if X
# is lexicographically larger Y
def IsLarger(X, Y, n) :
for i in range(n) :
# It is lexicographically larger
if (X[i] < Y[i]) :
return False;
return True;
# Utility function to check
# minimum value of d
def solve(X, Y, n) :
ans = 0;
# If X is already larger
# do not need to add anything
if (IsLarger(X, Y, n)) :
ans = 0;
else :
# Adding d to all elements of X
d = Y[0] - X[0];
for i in range(n) :
X[i] += d;
# If X is larger now
# print d
if (IsLarger(X, Y, n)) :
ans = d;
# else print d + 1
else :
ans = d + 1;
return ans;
# Driver Code
if __name__ == "__main__" :
# Taking the numbers as sequences
X = [ 2, 3, 6, 9 ];
Y = [ 3, 4, 8, 1 ];
n = len(X);
print(solve(X, Y, n));
# This code is contributed by AnkitRai01
C#
// C# program to find Minimum number to be.Added
// to all digits of X to make X > Y
using System;
class GFG
{
// Function to check if X
// is lexicographically larger Y
static bool IsLarger(int[] X,
int[] Y, int n)
{
for (int i = 0; i < n; i++)
{
// It is lexicographically larger
if (X[i] < Y[i])
{
return false;
}
}
return true;
}
// Utility function to check
// minimum value of d
static int solve(int []X, int []Y, int n)
{
int ans = 0;
// If X is already larger
// do not need to.Add anything
if (IsLarger(X, Y, n))
ans = 0;
else
{
// Adding d to all elements of X
int d = Y[0] - X[0];
for (int i = 0; i < n; i++)
X[i] += d;
// If X is larger now
// print d
if (IsLarger(X, Y, n))
ans = d;
// else print d + 1
else
{
ans = d + 1;
}
}
return ans;
}
// Driver Code
public static void Main(String[] args)
{
// Taking the numbers as sequences
int []X = { 2, 3, 6, 9 };
int []Y = { 3, 4, 8, 1 };
int n = X.Length;
Console.WriteLine(solve(X, Y, n));
}
}
// This code is contributed by PrinciRaj1992
输出:
2
时间复杂度: ,其中N是X或Y的长度