+ Reply to Thread
Results 1 to 5 of 5

Thread: Find 6th largest element in an array

  1. #1
    Surfer is offline Senior Member
    Join Date
    Mar 2010
    Posts
    321

    Find 6th largest element in an array

    Find 6th largest element in an integer array.

  2. #2
    Surfer is offline Senior Member
    Join Date
    Mar 2010
    Posts
    321
    Code:
    int sixthLargest(const int a[]) {
        int max[6] = {INT_MIN};
        for (int i = 0; i < N; ++i)
            if (a[i] > max[0]) max[0] = a[i];
        for (int x = 1; x < 6; ++x)
            for (int i = 0; i < N; ++i)
                if (a[i] > max[x] && a[i] < max[x - 1]) max[x] = a[i];
        return max[5];
    }
    T: O(6n) = O(n)
    S: O(6) = O(1)

  3. #3
    game.iiith Guest
    @surfer
    The solution which you provide is brute force and the analysis is also flawed. Do the analysis based on fact that find the kth largest element. Now the analysis will yield O(k*n) which is actually considered O(n^2) since 1<=k<=n . Thus, your solution is O(n^2)
    For O(n) solution, visit Selection algorithm - Wikipedia, the free encyclopedia

    Avi
    Avi Dullu&#039;s Code Arena

  4. #4
    puspendra Guest
    1. build heap of first 6 element
    2. if new element if greater than root element of min Heap, Delete root and insert new element and maintain heap

  5. #5
    santoshkiitk is offline Junior Member
    Join Date
    Dec 2010
    Posts
    1

    6th largest element

    Arr[n] //given array
    arr[6] = {0};
    arr[0] = Arr[0];
    i = 0;
    for(j=1; j<n; j++)
    {
    if(arr[i] < Arr[j])
    {
    i = (i + 1)%6;
    arr[i] = Arr[j];
    }
    }
    i = (i +1)% 6;
    printf("6 th largest = %d",arr[i]);

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Find largest subsequence sum
    By TopGun in forum Algorithm/Data Structure Questions
    Replies: 2
    Last Post: 4th November 2010, 18:59
  2. Find 2nd largest
    By JavaGuy in forum Apple
    Replies: 0
    Last Post: 27th May 2010, 00:02
  3. Find 4th smallest element of a tree
    By TopGun in forum Adobe
    Replies: 2
    Last Post: 22nd April 2010, 23:31
  4. Find level of an element in a tree
    By Surfer in forum Adobe
    Replies: 1
    Last Post: 23rd March 2010, 20:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts