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

Pls Explain how to do this.

asked in Algorithms by Boss (7k points) 5 32 85 | 62 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 (12.2k points) 9 73 139
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 (4.5k points) 2 6 16

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



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
Top Users Oct 2017
  1. Arjun

    23684 Points

  2. Bikram

    17288 Points

  3. Habibkhan

    9194 Points

  4. srestha

    6486 Points

  5. Debashish Deka

    5478 Points

  6. jothee

    5178 Points

  7. Sachin Mittal 1

    4910 Points

  8. joshi_nitish

    4504 Points

  9. sushmita

    4080 Points

  10. Rishi yadav

    3998 Points


Recent Badges

Nice Question Ashwani Kumar 2
Nice Comment Pooja Palod
Famous Question Harsh181996
Verified Human ASK
Good Comment Bikram
Good Comment Arjun
Nice Comment Arjun
Famous Question Meenakshi Sharma
Famous Question Meenakshi Sharma
Nice Question smartmeet
27,426 questions
35,275 answers
84,602 comments
33,523 users