I have an array called days[m] which contains m numbers.
days[m]
m
I am trying to get the longest interval's first and last elements index where every int between those 2 indexes is bigger than 0.
int
Example:
Input:
days[m] = {1,2,2,3,3,5}
Output:
1 3
Here's what I've tried:
int max = 0,ind,ind2,db; for(int i = 0;i < m;i++){ if(days[i] > 0){ db = 1; int j = i; while(days[j] > 0){ db++; j++; } if(db > max){ max = db; ind = i; ind2 = i+db; } } } cout<<ind<<" "<<ind2<<endl;
2.1m questions
2.1m answers
60 comments
57.0k users