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

Pls Explain how to do this.

asked in Algorithms by Active (1.7k points)   | 55 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 Loyal (4.9k 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 Active (1.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 Jan 2017
  1. Debashish Deka

    9716 Points

  2. sudsho

    5560 Points

  3. Bikram

    5290 Points

  4. Habibkhan

    4990 Points

  5. Vijay Thakur

    4498 Points

  6. Arjun

    4418 Points

  7. saurabh rai

    4236 Points

  8. Sushant Gokhale

    4226 Points

  9. Kapil

    3848 Points

  10. santhoshdevulapally

    3808 Points

Monthly Topper: Rs. 500 gift card

19,449 questions
24,228 answers
53,954 comments
20,373 users