Skip to content
Programmingoneonone
Programmingoneonone
  • 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

HackerEarth Number of steps problem solution

YASH PAL, 31 July 2024
In this HackerEarth Maximum borders problem solution you are given two arrays a1, a2,…, an and b1,b2,…,bn. In each step, you can set ai = ai – bi if ai >= bi. Determine the minimum number of steps that are required to make all are equal.
Input format
First line: n 
Second line: a1, a2,…an
Third line: b1, b2,…, bn
hackerEarth Number of steps problem solution

HackerEarth Number of steps problem solution.

#include <bits/stdc++.h>
using namespace std;

const int maxn = 5e3 + 17;
int n, a[maxn], b[maxn];
int main(){
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
for(int i = 0; i < n; i++)
cin >> a[i];
for(int i = 0; i < n; i++)
cin >> b[i];
for(int x = *min_element(a, a + n); x >= 0; x--){
bool ok = 1;
int ans = 0;
for(int i = 0; i < n && ok; i++){
ok &= x == a[i] || b[i] > 0 && x % b[i] == a[i] % b[i];
if(b[i])
ans += (a[i] - x) / b[i];
}
if(ok)
return cout << ans << 'n', 0;
}
cout << "-1n";
}

coding problems solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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