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)