-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from rndastech/main
Added Question
- Loading branch information
Showing
5 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
For calculating maxx, we can sort the given string x using a comparator function where the priority is given as F>E>D…>A>9>8>7…>1>0. | ||
To find the minx, we can reverse maxx. If minx contains leading zeros, we can use a for loop to find the position of the first non zero number and swap it with the first position of minx. | ||
To compare the hexadecimal numbers, we can convert them to integers and then compare them. | ||
We need to check for “0” manually as it is the only number having leading zeros. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
long long int exp(long long int x, long long int n) { | ||
long long int res = 1; | ||
while (n > 0) { | ||
if (n % 2 == 1) { res = res * x; } | ||
x = x * x; | ||
n /= 2;} | ||
return res;} | ||
bool cmp(char f, char s){ | ||
char arr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; | ||
long long int p1, p2; | ||
for(long long int i = 0; i<16;i++){ | ||
if(f==arr[i]) | ||
p1 = i; | ||
if(s==arr[i]) | ||
p2 = i;} | ||
return (p1>=p2);} | ||
long long int hexint(string s, long long int n){ | ||
long long int ans = 0; | ||
for(long long int i=0;i<n;i++){ | ||
if(s[i]>='A' && s[i]<='F'){ | ||
ans+= (s[i]-'A'+10)*exp(16, n-1-i);} | ||
if(s[i]>='0' && s[i]<='9'){ | ||
ans+= (s[i]-'0')*exp(16, n-1-i);}} | ||
return ans; | ||
} | ||
int main() { | ||
ios_base::sync_with_stdio(false); | ||
cin.tie(NULL); | ||
cout.tie(NULL); | ||
long long int t; | ||
cin>>t; | ||
while(t--){ | ||
string xs; | ||
cin>>xs; | ||
if(xs=="0"){ | ||
cout<<"NO"<<endl;} | ||
else{ | ||
string maxxs = xs; | ||
sort(maxxs.begin(), maxxs.end(), cmp); | ||
string minxs = maxxs; | ||
reverse(minxs.begin(), minxs.end()); | ||
if(minxs[0]=='0'){ | ||
long long int p1 = 0; | ||
for(long long int i=0;i<minxs.size();i++){ | ||
if(minxs[i]!='0'){ | ||
p1=i; | ||
break;}} | ||
minxs[0] = minxs[p1]; | ||
minxs[p1] = '0';} | ||
long long int x = hexint(xs, xs.size()); | ||
long long int maxx = hexint(maxxs, maxxs.size()); | ||
long long int minx = hexint(minxs, minxs.size()); | ||
if(maxx - x > x - minx) | ||
cout<<"YES"<<endl; | ||
else | ||
cout<<"NO"<<endl; | ||
}} | ||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
Title | ||
Hex Sort | ||
|
||
Problem Statement | ||
A hexadecimal number consists of numbers 0-9 and letters A-F, where A = 10, B = 11,..., F = 15. Given a hexadecimal number x, let the minimum and maximum number formed possible by rearranging all the hexadecimal digits without adding or removing any digit and without containing leading zeros (except the number 0) be denoted by minx and maxx respectively. Is |maxx - x| > |x - minx|? | ||
|
||
|
||
Input Format + Constraints | ||
The first line contains an integer t (1≤t≤10^5) — the number of test cases. | ||
The only line of each test case contains a string x (1<= x.size <=10) consisting of digits ‘0’ - ‘9’ and letters ‘A’ - ‘F’. | ||
|
||
Output Format | ||
For each test case, print on one line "YES" if |maxx - x| > |x - minx|. Otherwise, print "NO". | ||
|
||
|
||
Explanation: | ||
For 1st test case, | ||
minx = ABCD | ||
maxx = DCBA | ||
maxx - x = 30ED | ||
x - minx = 0 | ||
Therefore, maxx - x > x - minx | ||
|
||
For 2nd test case, | ||
minx = 8 | ||
maxx = 8 | ||
maxx - x = 0 | ||
x - minx = 0 | ||
Therefore, maxx - x = x - minx | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
2 | ||
ABCD | ||
8 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
YES | ||
NO |