Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • 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
Programming101
Programmingoneonone

Learn everything about programming

HackerEarth Interesting Coins problem solution

YASH PAL, 31 July 2024
In this HackerEarth Interesting Coins problem solution, Roy has a number of coins with him that he loves playing with by arranging them in the form of N stacks numbered sequentially. Lucy now gives him a task to make his arrangement of coins even more interesting. The arrangement will be interesting if the number of coins in the stacks are in strictly increasing order.
For this task Lucy gives Roy the option to modify his arrangement by increasing or decreasing the number of coins in each of the stack by atmost C coins. Also the number of coins in a stack should not fall below 1.
Your mission, should you choose to accept it, is to report the minimum C that helps Roy complete the assigned task.
HackerEarth Interesting Coins problem solution

HackerEarth Interesting Coins problem solution.

#include <bits/stdc++.h>

using namespace std;



#define int long long int
#define rep(i,a,b) for(int i=a;i<b;i++)
#define pb push_back
#define F first
#define S second
#define endl "n"
#define FAST ios::sync_with_stdio(0);cin.tie(0);



typedef vector<int> vi;
typedef set<int> si;
typedef multiset<int> msi;
typedef map<int,int> mii;
typedef unordered_set<int> usi;
typedef unordered_multiset<int> umsi;
typedef unordered_map<int,int> umii;
typedef pair<int,int> pi;
typedef deque<int,int> di;



int is_increasing(int arr[],int n){

rep(i,1,n){

if(arr[i]<=arr[i-1]) return 0;

}

return 1;

}

int evaluate(int n,int ar[],int k){

int arr[n];

rep(i,0,n) arr[i]=ar[i];

arr[0]=max(1LL,arr[0]-k);

rep(i,1,n){

if(arr[i]>arr[i-1]){

arr[i]=max(arr[i-1]+1,arr[i]-k);

}

else{

arr[i]=min(arr[i-1]+1,arr[i]+k);

}

}

//rep(i,0,n) cout<<arr[i]<<" ";

//cout<<endl;

if(is_increasing(arr,n)) return 1;

else return 0;

}



signed main()

{

FAST
int t;
cin>>t;

while(t--){

int n,ans;

cin>>n;

int arr[n];

rep(i,0,n) cin>>arr[i];

int lo=0,hi=1e9;

while(lo<=hi){

int mid=(lo+hi)/2;

int bac=evaluate(n,arr,mid);

if(bac==1){

ans=mid;

hi=mid-1;

}

else{

lo=mid+1;

}

}

cout<<ans<<endl;

}

}
coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes