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

Pls Explain how to do this.

asked in Algorithms by Boss (6.2k points)   | 61 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.6k 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.7k 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..



Top Users Aug 2017
  1. ABKUNDAN

    4660 Points

  2. Bikram

    4366 Points

  3. akash.dinkar12

    3258 Points

  4. rahul sharma 5

    3042 Points

  5. manu00x

    2682 Points

  6. makhdoom ghaya

    2410 Points

  7. just_bhavana

    2100 Points

  8. Tesla!

    1918 Points

  9. stblue

    1682 Points

  10. joshi_nitish

    1608 Points


24,928 questions
32,024 answers
74,385 comments
30,113 users