Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerEarth Special numbers problem solution

YASH PAL, 31 July 2024
In this HackerEarth Special numbers problem solution, Alice is traveling to a special town with her friends. All the residents of this special town communicate through a special language. This special language consists of 4 special alphabets a, b, c and d. This language contains special words. These words are composed of these 4 special alphabets and are always even in length. The words are always palindromic in nature. The words are also assigned special numbers. A special number is the lexicographical rank of special word with respect to other special words. 
You are given a special number. Your task is to determine the special word corresponding to that special number.
HackerEarth Special numbers problem solution

HackerEarth Special numbers problem solution.

#include<bits/stdc++.h>
using namespace std;
/*#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
/*template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
*/typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;

#define LOCAL 0
#define dbg(x) cout << #x << " is " << x << "n"
#define gll(x) scanf("%d",&x)
#define gll2(x,y) scanf("%d%d",&x,&y)
#define gll3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define gllarr(arr,n) f(i,n) gll(arr[i]);
#define sz(x) ((int)x.size())
#define s(x) sort(x.begin(),x.end())
#define all(v) v.begin(),v.end()
#define rs(v) { s(v) ; r(v) ; }
#define r(v) {reverse(all(v));}
#define pb push_back
#define f(i,n) for(int i=0;i<n;i++)
#define fr(i,n) for(int i=n-1;i>=0;i--)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repr(i,a,b) for(int i=a;i>=b;i--)

const ll mod = (ll)1e9 + 7;
const ll inf = (ll)1e16;
const ld eps = 1e-12;
const ll N = (int)1e5 + 5;
const ll LOGN = 19;
const ld PI = 3.14159265358979323846;
inline ll mul(ll a, ll b, ll m = mod) { return (ll)(a * b) % m;}
inline ll add(ll a, ll b, ll m = mod) { a += b; if(a >= m) a -= m; if(a < 0) a += m; return a;}
inline ll power(ll a, ll b, ll m = mod) { if(b == 0) return 1; if(b == 1) return (a % m); ll x = power(a, b / 2, m); x = mul(x, x, m); if(b % 2) x = mul(x, a, m); return x;}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
if (LOCAL) {
freopen("C:\Users\Dishant\Desktop\Collection-DEV c++\input.txt", "r", stdin);
freopen("C:\Users\Dishant\Desktop\Collection-DEV c++\output.txt", "w", stdout);
}
int t;
cin>>t;
while(t--){
int k;
cin>>k;
assert(k <= (int)1e9);
int len = 1;
ll curr = 0, next = 4;
while(curr + next < k) {
curr += next;
next *= 4ll;
len++;
}
k -= curr;
k--;
string temp;
int tmp = len;
while(tmp--) {
int to = k % 4;
k /= 4;
temp.pb(to + 'a');
}
string put = temp;
r(temp);
cout<<temp<<put<<endl;
}
return 0;
}

Second solution

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define REP(i,n) for(i=1;i<=n;i++)
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define vl vector<LL>
#define itr ::iterator it
#define lb lower_bound
#define ub upper_bound
#define LL long long
#define ULL unsigned long long
#define ret return
LL t,n,i,j,ans,k;
LL a[100],b[45] ;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>t ;
a[1] = 4 ;
LL p = 16 ;
for(i=2;i<=30;i++) a[i] = p + a[i-1],p = 4 * p ;
while(t--)
{ cin>>k ;
string ans ;
REP(i,30)
{ if(a[i]<k) continue ;
LL start,buck ;
buck = (a[i] - a[i-1])/4 ;

if(a[i-1] + buck>=k) start = a[i-1] ,ans.pb('a') ;
else if(a[i-1] + 2 * buck>=k) start = a[i-1] + buck ,ans.pb('b') ;
else if(a[i-1] + 3 * buck>=k) start = a[i-1] + 2 * buck ,ans.pb('c') ;
else start = a[i-1] + 3 * buck ,ans.pb('d') ;

while(ans.length()<i)
{ buck /= 4 ;

if(start + buck>=k) ans.pb('a') ;
else if(start + 2 * buck>=k) start = start + buck,ans.pb('b') ;
else if(start + 3 * buck>=k) start = start + 2 * buck,ans.pb('c') ;
else start = start + 3 * buck,ans.pb('d') ;

}
break ;
}
cout<<ans ;
reverse(all(ans)) ;
cout<<ans<<endl ;
}
}
coding problems solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes