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 Erasing an array problem solution

YASH PAL, 31 July 202413 February 2026
In this HackerEarth Erasing an Array problem solution you are given a binary array A of N elements. The array consists of 0’s and 1’s. You can perform the following operations as many times as possible:
  1. Select a subarray starting from the first index that is inversion-free and delete it.
  2. Determine the minimum number of operations to delete the entire array.
Inversion free: There are no two indices i and j in array A such that (i < j) and (Ai > Aj). 
Subarray: A subarray is an array obtained after deleting some elements from the beginning (prefix) possibly 0 and deleting some elements from the end (suffix) possibly 0.
 
 
hackerEarth Erasing an array problem solution
 
 

HackerEarth Erasing an array problem solution.

#include<bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mod 1000000007
#define endl "n"
#define test ll t; cin>>t; while(t--)
typedef long long int ll;
int main() {
FIO;
test
{
ll n; cin>>n;
vector<ll>a(n);
for(auto &it:a) cin>>it;
int ans=0,ind=0;
while(ind<n){
ans++;
while(ind<n && a[ind]==0){
ind++;
}
while(ind<n && a[ind]==1){
ind++;
}
}
cout<<ans<<endl;
}
return 0;
}
 

second solution

t = int(input())
while t > 0:
t -= 1
n = int(input())
a = list(map(int, input().split()))
ans = 1
for i in range(n - 1):
ans += a[i] - a[i + 1] == 1
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