Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8791041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:47:19+00:00 2026-06-13T22:47:19+00:00

I want to design a function to find the k biggest elements in a

  • 0

I want to design a function to find the k biggest elements in a unordered set of N elements with time complexity: Θ(N+klogN) on an online judge.

here is a sample:


Input

LN 1 : N K

LN 2 : N numbers

Output

LN 1 : K biggest number

LN 2 : Final heap

Sample Input

10 4
17 19 26 37 30 11 5 29 32 1

Sample Output

29
26 19 11 17 1 5

And here is my code:

#include <iostream>

using namespace std;

int main(){

    int i,j,rc,temp,temp1,length,K;
    cin>>length>>K;
    int *heap = new int[length];

    for(i=0;i<length;i++) cin>>heap[i];
    for(i=length/2-1;i>=0;i--){                  //build a max heap first with Θ(N)
        while(!((i>=length/2)&&(i<length))){
            j = 2*i+1;
            rc = 2*i+2;
            if((rc<length)&&(heap[j]<heap[rc])) j=rc;
            if(heap[i]>heap[j]) break;
            temp = heap[i];
            heap[i]=heap[j];
            heap[j]=temp;
            i=j;
        }
    }
    int k,n=length;
    for(k=0;k<K;k++){                         //shiftdown k times to find k biggest 
        temp1=heap[--n];                      //numbers with Θ(klogN)
        heap[n] = heap[0];
        heap[0] = temp1;
        if(n!=0) {
            i=0;
                while(!((i>=n/2)&&(i<n))){
                     j = 2*i+1;
                    rc = 2*i+2;
                    if((rc<n)&&(heap[j]<heap[rc])) j=rc;
                    if(heap[i]>heap[j]) break;
                    temp = heap[i];
                    heap[i]=heap[j];
                    heap[j]=temp;
                    i=j;
                }
            }
        }


    cout<<heap[length-K]<<endl;
    for(i=0;i<length-K;i++)
        cout<<heap[i]<<" ";
    return 0;
}

It’s all right but one of datas is Time Limit Exceed ,I am so confused with how to solve this problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:47:20+00:00Added an answer on June 13, 2026 at 10:47 pm

    Your sift-down operation doesn’t seem correct. There shouldn’t be two nested loops there. You should just start at the root and keep swapping it with one of its children until it’s larger than both. The outer loop for(i=n/2-1;i>=0;i--) shouldn’t be there (it causes each sift-down to take O(n)) – I think you should just set i to 0 to start at the root.

    Edit: Your heapify operation is too slow as well: You are using the same loop variable i for both the outer and inner loop, so it will alternately grow larger and smaller. The inner loop should start with the outer loop’s i, but shouldn’t affect the value of i in the outer loop’s next iteration. I suggest putting the sift-down operation in its own function. This will both fix this problem, and avoid having sift-down coded twice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to change my window design rapidly. I have OnPaint function which I
I want to design an interface has the function to do mapping from Entity
Here's the design I want to accomplish in Perl: A master script calls multiple
I want design my startup screen with progress bar. But I don't how to
Am working on international project, and want design 3 major tables structure as required
I want to design a webpage login that when click on the login button
I want to design a page in django that has a search bar in
I want to design a c# program which can run program a 3rd exe
We want to design a simple domain specific language for writing test scripts to
I want to design a chat view like BlackBerry messenger (unfortunately new users are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.