Similar Problems

Similar Problems not available

Biggest Single Number - Leetcode Solution

Companies:

LeetCode:  Biggest Single Number Leetcode Solution

Difficulty: Easy

Topics: database  

Problem statement:

Given a non-negative integer n, find the largest integer that is less than or equal to n and has the same digits as n.

Example 1: Input: n = 213 Output: 213

Example 2: Input: n = 1234 Output: 4321

Example 3: Input: n = 10 Output: 10

Approach:

The problem asks to find the largest number less than or equal to n with the same digits as n. One way to solve this is to traverse the integers from n to 1 and check if each number has the same digits as n. Once we find a number with the same digits, we can return it as the answer. However, this approach would have a time complexity of O(n*d) where n is the input number and d is the number of digits in the input number.

A more efficient way to solve this problem would be to traverse the digits of the input number from left to right, and find the first instance where a digit is smaller than the preceding digit. This is because replacing the smaller digit with a larger digit would result in a number that is less than or equal to n with the same digits as n. Once we find this digit, we can swap it with the largest digit to its right that is smaller than it, and then sort all the digits to the right of the swapped digit in descending order. This would result in the largest number with the same digits as n.

For example, consider the number 1234. We traverse the digits from left to right and find the first instance where a digit is smaller than the preceding digit, which is at the digit 2. We then find the largest digit to the right of 2 that is smaller than 2, which is 1. We swap 2 and 1 to get the number 1324, which is greater than the input number 1234. We then sort all the digits to the right of 2 in descending order to obtain the final answer, which is 1243.

Implementation:

We can start by converting the input number into an array of digits. We can then traverse this array from right to left and find the first instance where a digit is smaller than the preceding digit, which we will call the 'pivot' index. If no such instance is found, it means that the input number is already the largest number with the same digits, and we can simply return the input number as the answer.

Once we have found the pivot index, we can then find the largest digit to the right of the pivot index that is smaller than the digit at the pivot index, which we will call the 'swap' index. We then swap the digits at the pivot index and the swap index.

Finally, we sort all the digits to the right of the pivot index in descending order using a simple bubble sort.

The implementation for the same is provided below:

class Solution { public: int maximumSwap(int num) { vector<int> digits; while(num > 0) { digits.push_back(num%10); num /= 10; } reverse(digits.begin(), digits.end()); int n = digits.size(); int pivot = -1; int swap = -1; for(int i=n-1; i>0; i--) { if(digits[i] > digits[i-1]) { pivot = i-1; break; } } if(pivot == -1) { return accumulate(digits.begin(), digits.end(), 0, [](int x, int y) { return x10 + y; }); } for(int i=n-1; i>pivot; i--) { if(digits[i] > digits[pivot]) { swap = i; break; } } swap(digits[pivot], digits[swap]); for(int i=pivot+1; i<n; i++) { for(int j=pivot+1; j<n-i+pivot; j++) { if(digits[j] < digits[j+1]) { swap(digits[j], digits[j+1]); } } } return accumulate(digits.begin(), digits.end(), 0, [](int x, int y) { return x10 + y; }); } };

Time Complexity:

The time complexity of this implementation is O(d^2), where d is the number of digits in the input number. This is because we are using a bubble sort to sort the digits to the right of the pivot index, which has a worst case time complexity of O(d^2). However, the average case time complexity of bubble sort is O(d*log(d)), which makes this implementation quite efficient for larger inputs.

Space Complexity:

The space complexity of this implementation is O(d), where d is the number of digits in the input number. This is because we are using an array to store the digits of the input number.

Biggest Single Number Solution Code

1