Skip to content

Commit 62e18af

Browse files
authored
Merge pull request #83 from rndastech/main
Added Question
2 parents b6d69fc + 5be6458 commit 62e18af

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
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.
2+
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.
3+
To compare the hexadecimal numbers, we can convert them to integers and then compare them.
4+
We need to check for “0” manually as it is the only number having leading zeros.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
long long int exp(long long int x, long long int n) {
4+
long long int res = 1;
5+
while (n > 0) {
6+
if (n % 2 == 1) { res = res * x; }
7+
x = x * x;
8+
n /= 2;}
9+
return res;}
10+
bool cmp(char f, char s){
11+
char arr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
12+
long long int p1, p2;
13+
for(long long int i = 0; i<16;i++){
14+
if(f==arr[i])
15+
p1 = i;
16+
if(s==arr[i])
17+
p2 = i;}
18+
return (p1>=p2);}
19+
long long int hexint(string s, long long int n){
20+
long long int ans = 0;
21+
for(long long int i=0;i<n;i++){
22+
if(s[i]>='A' && s[i]<='F'){
23+
ans+= (s[i]-'A'+10)*exp(16, n-1-i);}
24+
if(s[i]>='0' && s[i]<='9'){
25+
ans+= (s[i]-'0')*exp(16, n-1-i);}}
26+
return ans;
27+
}
28+
int main() {
29+
ios_base::sync_with_stdio(false);
30+
cin.tie(NULL);
31+
cout.tie(NULL);
32+
long long int t;
33+
cin>>t;
34+
while(t--){
35+
string xs;
36+
cin>>xs;
37+
if(xs=="0"){
38+
cout<<"NO"<<endl;}
39+
else{
40+
string maxxs = xs;
41+
sort(maxxs.begin(), maxxs.end(), cmp);
42+
string minxs = maxxs;
43+
reverse(minxs.begin(), minxs.end());
44+
if(minxs[0]=='0'){
45+
long long int p1 = 0;
46+
for(long long int i=0;i<minxs.size();i++){
47+
if(minxs[i]!='0'){
48+
p1=i;
49+
break;}}
50+
minxs[0] = minxs[p1];
51+
minxs[p1] = '0';}
52+
long long int x = hexint(xs, xs.size());
53+
long long int maxx = hexint(maxxs, maxxs.size());
54+
long long int minx = hexint(minxs, minxs.size());
55+
if(maxx - x > x - minx)
56+
cout<<"YES"<<endl;
57+
else
58+
cout<<"NO"<<endl;
59+
}}
60+
return 0;
61+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Title
2+
Hex Sort
3+
4+
Problem Statement
5+
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|?
6+
7+
8+
Input Format + Constraints
9+
The first line contains an integer t (1≤t≤10^5) — the number of test cases.
10+
The only line of each test case contains a string x (1<= x.size <=10) consisting of digits ‘0’ - ‘9’ and letters ‘A’ - ‘F’.
11+
12+
Output Format
13+
For each test case, print on one line "YES" if |maxx - x| > |x - minx|. Otherwise, print "NO".
14+
15+
16+
Explanation:
17+
For 1st test case,
18+
minx = ABCD
19+
maxx = DCBA
20+
maxx - x = 30ED
21+
x - minx = 0
22+
Therefore, maxx - x > x - minx
23+
24+
For 2nd test case,
25+
minx = 8
26+
maxx = 8
27+
maxx - x = 0
28+
x - minx = 0
29+
Therefore, maxx - x = x - minx
30+

Problem-setting/rndastech-1/test0.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2
2+
ABCD
3+
8

Problem-setting/rndastech-1/test0.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
YES
2+
NO

0 commit comments

Comments
 (0)