GATE CSE
First time here? Checkout the FAQ!
x
+1 vote
59 views

Pls Explain how to do this.

asked in Algorithms by Boss (5.3k points)   | 59 views
A simple for loop will do.

for(i=1;i<n;i++)

{

       if(a[i]>j && a[i]<k)

       {

             count++;

       }

}

Complexity O(n)

2 Answers

+1 vote
Best answer
Just take a variable initialized to 0 and traverse the array and for each element compare if(A[i]<k &&A[i]>j) then i++

Thats it one time traversing array of size n, hence TC= O(n)
answered by Veteran (10.2k points)  
selected by
+1 vote

All you need to do is iterate through the array and increment a count varaible whenever you find an element between the range j to k. check the code below.

int count = 0;

for(i=0;i<n;i++){

      if(a[i]>=j&&a[i]<=k){

             count++;

        }

}

answered by Loyal (3.5k points)  

Thanx all..:)got it now..i didnt understand the last two lines in the answer given by ME team. pls explain last two lines..

Related questions

0 votes
1 answer
2
asked in Algorithms by Arnabi Boss (5.3k points)   | 46 views
0 votes
0 answers
3
asked in Algorithms by Aboveallplayer Veteran (18.3k points)   | 180 views


Top Users Mar 2017
  1. rude

    5118 Points

  2. sh!va

    3054 Points

  3. Rahul Jain25

    2920 Points

  4. Kapil

    2730 Points

  5. Debashish Deka

    2602 Points

  6. 2018

    1574 Points

  7. Vignesh Sekar

    1422 Points

  8. Akriti sood

    1382 Points

  9. Bikram

    1350 Points

  10. Sanjay Sharma

    1128 Points

Monthly Topper: Rs. 500 gift card

21,531 questions
26,861 answers
61,191 comments
23,207 users