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 Digit Problem solution

YASH PAL, 31 July 2024
In this HackerEarth Digit problem solution we have Given two integers X and K, find the largest number that can be formed by changing digits at atmost K places in the number X.
HackerEarth Digit Problem solution

HackerEarth Digit Problem solution.

#include<bits/stdc++.h>

using namespace std;

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define eps 1e-9

const int MOD = 1e9+7;

typedef long long ll;
typedef pair<int,int> pii;

ll POWER[65];
ll power(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}
ll inv(ll x) {return power(x,MOD-2);}

void precompute() {
POWER[0]=1;
for(int i=1;i<63;i++) POWER[i]=POWER[i-1]<<1LL;
}
int main() {
precompute();
ll n,k;
cin>>n>>k;
assert(n>=1LL and n<=1000000000000000000LL);
assert(k>=0 and k<=9);
vector<int> v;
while(n) {
v.pb(n%10);
n/=10;
}
reverse(all(v));
for(int i=0;i<sz(v) and k--;i++) {
if(v[i]==9) {
k++;
continue;
}
else v[i]=9;
}
for(auto it:v) cout<<it;
cout<<endl;
return 0;
}

Second solution

#include <bits/stdc++.h>

using namespace std;

int main()
{
string s;
int k;

cin >> s >> k;

assert(k >= 0 && k <= 9);
assert(s.size() >= 1 && s.size() <= 19);

if ( s.size() == 19 ) assert(s == "1000000000000000000");

for ( int i = 0; i < s.size(); i++ ) assert(s[i] >= '0' && s[i] <= '9');

for ( int i = 0; i < s.size() && k > 0; i++ ) {
if ( s[i] == '9' ) continue;
s[i] = '9', k--;
}

cout << s << endl;

return 0;
}
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