Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

c++ longest interval's first and last element where elements are bigger than 0

I have an array called days[m] which contains m numbers.

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.

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;
question from:https://stackoverflow.com/questions/65851380/c-longest-intervals-first-and-last-element-where-elements-are-bigger-than-0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...