Skip to content

Commit

Permalink
Merge pull request #83 from rndastech/main
Browse files Browse the repository at this point in the history
Added Question
  • Loading branch information
rishabhking authored Dec 19, 2024
2 parents b6d69fc + 5be6458 commit 62e18af
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Problem-setting/rndastech-1/explanation.txt
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.
61 changes: 61 additions & 0 deletions Problem-setting/rndastech-1/solution.cpp
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;
}
30 changes: 30 additions & 0 deletions Problem-setting/rndastech-1/statement.txt
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

3 changes: 3 additions & 0 deletions Problem-setting/rndastech-1/test0.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2
ABCD
8
2 changes: 2 additions & 0 deletions Problem-setting/rndastech-1/test0.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
YES
NO

0 comments on commit 62e18af

Please sign in to comment.