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 Minimum operations problem solution

YASH PAL, 31 July 202413 February 2026
In this HackerEarth Minimum operations problem solution You are given an array A of length N and can perform the following operation on the array:
 
Select a subarray from array A having the same value of elements and decrease the value of all the elements in that subarray by any positive integer x.
Find the minimum number of operations required to make all the elements of array A equal to zero.
 
 
Hackerearth Minimum operations problem solution

 

 

HackerEarth Minimum operations problem solution.

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

void solve(){
int n;
cin >> n;
assert(1 <= n and n <= 500000);

int a[n+1];
for(int i = 1 ; i <= n ; i++){
cin >> a[i];
assert(1 <= a[i] and a[i] <= 1000000000);
}

int ans = 0;
for(int i = 1 ; i <= n ; i++){
int j = i;
ans++;
while(j <= n and a[j] == a[i]){
j++;
}
i = j - 1;
}
cout << ans << endl;
}

signed main(){
solve();
}
 

Second solution

n = int(input())
a = list(map(int, input().split()))
last = -1
ans = 0
for x in a:
ans += last != x
last = x
print(ans)
 
 
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