Created
June 15, 2025 08:13
-
-
Save SuryaPratapK/f9f8f85188d4f5054072b54c8b020dc7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int maxDiff(int num) { | |
string s = to_string(num); | |
string max_s = s; | |
string min_s = s; | |
int n = s.size(); | |
//Maximize number: The first digit<9 should be made 9 | |
for(int i=0;i<n;++i){ | |
if(s[i]!='9'){ | |
for(int j=0;j<n;++j) | |
if(max_s[j]==s[i]) | |
max_s[j]='9'; | |
break; | |
} | |
} | |
//Minimize number | |
if(s[0]>'1'){ | |
for(int i=0;i<n;++i) | |
if(min_s[i]==s[0]) | |
min_s[i]='1'; | |
}else{ | |
//Find 1st digit>0 from index 1 to (n-1) | |
for(int i=1;i<n;++i){ | |
if(s[i]>'1'){ | |
for(int j=i;j<n;++j) | |
if(min_s[j]==s[i]) | |
min_s[j]='0'; | |
break; | |
} | |
} | |
} | |
return stoi(max_s)-stoi(min_s); | |
} | |
}; | |
/* | |
//JAVA | |
public class Solution { | |
public int maxDiff(int num) { | |
String s = Integer.toString(num); | |
char[] maxChars = s.toCharArray(); | |
char[] minChars = s.toCharArray(); | |
int n = s.length(); | |
// Maximize number: The first digit < '9' should be made '9' | |
for (int i = 0; i < n; ++i) { | |
if (maxChars[i] != '9') { | |
char target = maxChars[i]; | |
for (int j = 0; j < n; ++j) { | |
if (maxChars[j] == target) { | |
maxChars[j] = '9'; | |
} | |
} | |
break; | |
} | |
} | |
// Minimize number | |
if (minChars[0] > '1') { | |
char target = minChars[0]; | |
for (int i = 0; i < n; ++i) { | |
if (minChars[i] == target) { | |
minChars[i] = '1'; | |
} | |
} | |
} else { | |
// Find first digit > '0' from index 1 to (n-1) | |
for (int i = 1; i < n; ++i) { | |
if (minChars[i] > '1') { | |
char target = minChars[i]; | |
for (int j = i; j < n; ++j) { | |
if (minChars[j] == target) { | |
minChars[j] = '0'; | |
} | |
} | |
break; | |
} | |
} | |
} | |
int maxNum = Integer.parseInt(new String(maxChars)); | |
int minNum = Integer.parseInt(new String(minChars)); | |
return maxNum - minNum; | |
} | |
} | |
#Python | |
class Solution: | |
def maxDiff(self, num: int) -> int: | |
s = str(num) | |
max_s = list(s) | |
min_s = list(s) | |
n = len(s) | |
# Maximize number: The first digit < '9' should be made '9' | |
for i in range(n): | |
if max_s[i] != '9': | |
target = max_s[i] | |
for j in range(n): | |
if max_s[j] == target: | |
max_s[j] = '9' | |
break | |
# Minimize number | |
if min_s[0] > '1': | |
target = min_s[0] | |
for i in range(n): | |
if min_s[i] == target: | |
min_s[i] = '1' | |
else: | |
# Find first digit > '0' from index 1 to (n-1) | |
for i in range(1, n): | |
if min_s[i] > '1': | |
target = min_s[i] | |
for j in range(i, n): | |
if min_s[j] == target: | |
min_s[j] = '0' | |
break | |
max_num = int(''.join(max_s)) | |
min_num = int(''.join(min_s)) | |
return max_num - min_num | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment