While solving Today's leetcode Daily challenge I wrote this code: (not the actual solution)
class Solution {
public:
long long countSubarrays(vector<int>& nums, int minK, int maxK) {
int j = -1;
int mintillnow = INT_MAX;
int maxtillnow = INT_MIN;
long long count = 0;
while(j<nums.size()){
cout<<"why not running"<<endl;
j++;
mintillnow = min(nums[j],mintillnow);
maxtillnow = max(nums[j],maxtillnow);
if(mintillnow==minK && maxtillnow == maxK){
count++;
}
if(mintillnow<minK || maxtillnow>maxK){
mintillnow = INT_MAX;
maxtillnow = INT_MIN;
}
}
return count;
}
};
The problem is when I initialize j = -1, the loop doesn't run and just returns count. But the loop works fine when I initialize j = 0 .
Why does this happen?