[Solved] The big number difference with Java, C++, Python

The big number difference: There are small numbers and big numbers. Its difficult to sometime handle the big numbers and Jon is stuck in one such problem where he is supposed to do some complex calculations on big numbers.
Given a number X, Jon has to find largest number less than equal to X which has maximum sum of its digits. Lets call that number Y. Jon needs your help in writing a program to find that number Y.

Input Format:

First and only line will contain a number X of length <=10^3.
X will be greater than 0.

Output Format:

Print the number Y

Sample Input 1:

100

Sample Output 1:

99

Explanation:

99 is the biggest number less than or equal to x which has maximum sum of digits. Sum of digits will be 18.

Sample Input 2:

9999999999999999999999999999999999999

Sample Output 2:

9999999999999999999999999999999999999

Solution

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String x = sc.nextLine();
        int sumX = getDigitSum(x);

        String y;
        if (isPowerOf10(x)) {
            y = subtractOne(x);
        } else {
            y = x.substring(0, x.length() - 1) + "0";
        }

        while (getDigitSum(y) < sumX) {
            y = addOne(y);
        }

        System.out.println(y);
    }

    private static int getDigitSum(String num) {
        int sum = 0;
        for (int i = 0; i < num.length(); i++) {
            sum += Character.getNumericValue(num.charAt(i));
        }
        return sum;
    }

    private static boolean isPowerOf10(String num) {
        return num.matches("10*");
    }

    private static String subtractOne(String num) {
        int n = num.length();
        char[] result = new char[n - 1];
        for (int i = 0; i < n - 1; i++) {
            result[i] = '9';
        }
        return new String(result);
    }

    private static String addOne(String num) {
        int n = num.length();
        char[] result = new char[n];
        for (int i = 0; i < n; i++) {
            result[i] = num.charAt(i);
        }
        int i = n - 1;
        while (i >= 0 && result[i] == '9') {
            result[i] = '0';
            i--;
        }
        if (i < 0) {
            result = new char[n + 1];
            result[0] = '1';
            for (int j = 1; j < n + 1; j++) {
                result[j] = '0';
            }
        } else {
            result[i] += 1;
        }
        return new String(result);
    }
}
// c++
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int getDigitSum(string num) {
    int sum = 0;
    for (int i = 0; i < num.length(); i++) {
        sum += num[i] - '0';
    }
    return sum;
}

bool isPowerOf10(string num) {
    for (int i = 0; i < num.length(); i++) {
        if (num[i] != '0') {
            return false;
        }
    }
    return true;
}

string subtractOne(string num) {
    int n = num.length();
    string result = "";
    for (int i = 0; i < n - 1; i++) {
        result += '9';
    }
    return result;
}

string addOne(string num) {
    int n = num.length();
    string result = num;
    int i = n - 1;
    while (i >= 0 && result[i] == '9') {
        result[i] = '0';
        i--;
    }
    if (i < 0) {
        result = "1";
        for (int j = 1; j < n + 1; j++) {
            result += '0';
            // sauravhathi
        }
    } else {
        result[i] += 1;
    }
    return result;
}

int main() {
    string x;
    cin >> x;
    int sumX = getDigitSum(x);

    string y;
    if (isPowerOf10(x)) {
        y = subtractOne(x);
    } else {
        y = x.substr(0, x.length() - 1) + "0";
        // sauravhathi
    }

    while (getDigitSum(y) < sumX) {
        y = addOne(y);
    }

    cout << y << endl;
    return 0;
}
# python
def get_digit_sum(num):
    sum = 0
    for digit in num:
        sum += int(digit)
    return sum

def is_power_of_10(num):
    return num == '1' + '0' * (len(num) - 1)

def subtract_one(num):
    return str(int(num) - 1)

def add_one(num):
    n = len(num)
    result = list(num)
    i = n - 1
    while i >= 0 and result[i] == '9':
        result[i] = '0'
        i -= 1
    if i < 0:
        result.insert(0, '1')
    else:
        result[i] = str(int(result[i]) + 1)
        # sauravhathi
    return ''.join(result)

x = input().strip()
sum_x = get_digit_sum(x)

if is_power_of_10(x):
    y = subtract_one(x)
else:
    y = x[:-1] + '0'

while get_digit_sum(y) < sum_x:
    y = add_one(y)

print(y)

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *