# 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
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.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: This problem can be solved easily by breaking it into three cases

• Case 1: Find if X is already lexicographically larger than Y. If yes, then we don’t need to do anything.
• Case 2: Otherwise add (Y – X) to all elements of X and then check if X is lexicographically larger than Y or not.
• Case 3: If it is still not larger, then answer will be (Y – X) + 1 because the first elements of X become larger than first element of Y means X > Y.

Below is the implementation of the above approach:

## 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 - X; ` ` `  `        ``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); ` `    ``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 - X; ` ` `  `            ``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 `

Output:

```2
```

Time Complexity: , where N is the length of X or Y

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.