Open In App

Minimum number to be added to all digits of X to make X > Y

Last Updated : 08 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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:
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:
Explanation: 
X is already lexicographically larger than Y 
so the answer will be 0. 
 


 


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[0] - X[0]) 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[0] - X[0]) + 1 because the first elements of X become larger than first element of Y means X[0] > Y[0].


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 <bits/stdc++.h>
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
JavaScript
<script>
// javascript 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
function IsLarger(X, Y, n)
{
    for (let 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
function solve(X, Y, n)
{

    let 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
        let d = Y[0] - X[0];

        for (let 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

    // Taking the numbers as sequences
    let X = [ 2, 3, 6, 9 ];
    let Y = [ 3, 4, 8, 1 ];

    let n = X.length;
    document.write(solve(X, Y, n));

// This code is contributed by souravmahato348.
</script>

Output: 
2

 

Time Complexity: O(N)    , where N is the length of X or Y
 Auxiliary Space: O(1)


Similar Reads