Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Number of steps problem solution

YASH PAL, 31 July 202413 February 2026
In this HackerEarth Number of steps 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 HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

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