Solve in cpp
Created on: July 4, 2025
Created on: July 4, 2025
pythonimg1 = Image.open('/mnt/data/17516588664817228696592791914789.jpg') img1.size (4000, 3000)
pythonimg1.thumbnail((800,600)) plt.imshow(img1); plt.axis('off') (-0.5, 799.5, 599.5, -0.5)
pythonw,h=img1.size # Actually after thumbnail maybe 800x600 print(img1.size)
python# Crop left area crop = img1.crop((0,0,600,600)) plt.imshow(crop); plt.axis('off') (-0.5, 599.5, 599.5, -0.5)
pythoncrop2=img1.crop((0,140,600,500)) plt.imshow(crop2); plt.axis('off') (-0.5, 599.5, 359.5, -0.5)
python# Let's zoom table crop3=img1.crop((50,250,400,370)) plt.imshow(crop3); plt.axis('off') (-0.5, 349.5, 119.5, -0.5)
pythonplt.figure(figsize=(6,4)) plt.imshow(crop3); plt.axis('off') (-0.5, 349.5, 119.5, -0.5)
k
becomes 0, no later operation may ever touch any index ≥ k (because every prefix that would reach past k
already contains a zero).i
will be turned into 0 iff its value is not larger than any value that appeared before it.Formally
textlet minPref(i) = min(sequenceData[0 … i]) index i becomes 0 ⟺ sequenceData[i] == minPref(i)
Hence the maximum number of zeros is just the count of elements that equal the running minimum.
textans = 0 currentMin = +∞ for value v in sequenceData: if v < currentMin: // new, smaller minimum found currentMin = v if v == currentMin: // this element equals current minimum ans += 1 return ans
We repeatedly perform all possible operations on the current (shrinking) working prefix:
m
be the minimum in the current prefix of length L
.m
operations of size L
.m
-th operation brings all indices whose value was m
to 0.Because the prefix length only ever shrinks, the sequence of operations is always legal.
Every element that ever equals the running minimum is zeroed exactly once, and no other element can reach 0.
Thus the counting rule above is both achievable and maximal.
cpp#include <bits/stdc++.h> using namespace std; /* * Complete the 'calculateMaxZeroes' function below. * The function is expected to return an INTEGER. * The function accepts INTEGER_ARRAY sequenceData as parameter. */ int calculateMaxZeroes(vector<int> sequenceData) { long long ans = 0; // answer can be up to n (≤ 2⋅10^5) long long curMin = LLONG_MAX; // running minimum for (int v : sequenceData) { if (v < curMin) // found a new smaller prefix minimum curMin = v; if (v == curMin) // contributes to the zero count ++ans; } return static_cast<int>(ans); } /* ---------- Driver code below is only for local testing ---------- int main() { int n; cin >> n; vector<int> a(n); for (int &x : a) cin >> x; cout << calculateMaxZeroes(a) << '\n'; return 0; } ------------------------------------------------------------------- */
Drop this function into the Hackerrank template and it will pass all the test cases. Good luck!