Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • 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
Programmingoneonone

Hackerearth Minimum operations problem solution

YASH PAL, 31 July 2024
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

Post navigation

Previous post
Next post

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