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

Pls Explain how to do this.

asked in Algorithms by Loyal (4.7k points)   | 57 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 Boss (7.3k 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.4k 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
1
asked in Algorithms by Arnabi Loyal (4.7k points)   | 41 views
0 votes
0 answers
2
asked in Algorithms by Aboveallplayer Veteran (18.2k points)   | 145 views
Top Users Feb 2017
  1. Arjun

    5410 Points

  2. Bikram

    4230 Points

  3. Habibkhan

    3952 Points

  4. Aboveallplayer

    3086 Points

  5. Debashish Deka

    2564 Points

  6. sriv_shubham

    2318 Points

  7. Smriti012

    2240 Points

  8. Arnabi

    2008 Points

  9. mcjoshi

    1696 Points

  10. sh!va

    1684 Points

Monthly Topper: Rs. 500 gift card

20,864 questions
26,030 answers
59,704 comments
22,144 users