Kth largest element in an array (Method 1)

    by nikoo28

    Question: Given an unsorted array of integers and a number ‘k’. Return the kth largest element in the array.
    Input: arr = {3,2,1,5,6,4}, k = 2
    Output: 5

    Given an unsorted array of integers, you need to determine the kth largest element. By taking a first glance at the problem we can be sure of one thing. If the array is already sorted in ascending order, we can simply return the element at arr[size – k]. This would give us the kth largest element. This is because the 2nd largest element is the largest element in the array and will exist at arr[size-2]. Since, the array starts at 0.

    Let us start solving this problem with one of the most naive methods. By sorting the array. We can use any of the sorting methods we have discussed till now. QuickSort performs the fastest and hence we shall use it.

    Once the array is sorted simply return the (size – k)th element.

    #include<stdio.h>
    
    void swap(int *i, int *j)
    {
        int temp = *i;
        *i = *j;
        *j = temp;
    }
    
    int partition(int arr[], int start, int end)
    {
        int pivot = arr[end];
        int i = start;
        int j = end-1;
        while(i <= j)
        {
            while(arr[i] < pivot) i++; while(arr[j] > pivot)
                j--;
     		if(i <= j)
            {
                swap(&arr[i], &arr[j]);
                i++;
                j--;
            }
        }
    
        swap(&arr[i], &arr[end]);
        return i;
    }
    
    void performQuickSort(int arr[], int start, int end)
    {
        if(start < end)
        {
            int p = partition(arr, start, end);
            performQuickSort(arr, start, p-1);
            performQuickSort(arr, p+1, end);
        }
    }
    
    void quickSort(int arr[], int size)
    {
        performQuickSort(arr, 0, size-1);
    }
    
    // driver program
    int main(void)
    {
        int i;
        int arr[10] = {2, 6, 4, 10, 8, 1, 9, 5, 3, 7};
    
        quickSort(arr,10);
    
        printf("The 5th largest number in array is:-  %d", arr[10 - 5]);
        return 0;
    }
    Code language: C++ (cpp)

    A working implementation of the code can be found at this location. http://ideone.com/AXvFAq

    Note that this is a very naive approach.
    Time Complexity:- O(n log(n))
    Space Complexity:- O(n)

    6 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • ArraysMisc

    Convert a number into a string of words

    by nikoo28
    4 minutes read

    Question: Given an integer N, convert it into a string of words. Input: N = 345 Output: three hundred forty five This is one of the most common problems that we come across in out daily lives. We need to input a number from the user and print it in words. First, we perform a number of checks on the input string. If it is in any way invalid, the result is the empty string (“”). Next, we take note of whether or not the string begins with a ‘-‘. …

    0 FacebookTwitterLinkedinWhatsappEmail
  • MiscStrings

    Common String Algorithms

    by nikoo28
    7 minutes read

    In this post, I would try to implement the following string-related algorithm questions. I would like to implement these using Java and try to make additional comments which I think that are useful for understanding the implementations of some type of data structures in Java programming language. Non Repeated Characters in String : Return the unique characters in a given letter. Anagram Strings : Given two strings, check if they’re anagrams or not. Two strings are anagrams if they are written using the same exact letters, ignoring space, punctuation and …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    Find the first N prime numbers. (Method 3)

    by nikoo28
    3 minutes read

    Question: Given an integer N, find the prime numbers in that range from 1 to N. Input: N = 25Output: 2, 3, 5, 7, 11, 13, 17, 19, 23 We discussed the basic approaches to find the first N prime numbers in these posts Find the first N prime numbers (Method 1) Find the first N prime numbers (Method 2) It is advised that you go through the above posts to get a basic understanding of the approach that we have used to determine the prime numbers. Now lets analyze …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    Find the first N prime numbers. (Method 2)

    by nikoo28
    3 minutes read

    Question: Given an integer N, find the prime numbers in that range from 1 to N. Input: N = 25 Output: 2, 3, 5, 7, 11, 13, 17, 19, 23 We discussed the most basic approach to find the first N prime numbers in this post. Find the first N prime numbers. (Method 1) Please go through the post if you are not familiar with the naive methods. They are necessary to learn how the code can be optimized further. So let us try to further optimize the previously discussed …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    Find the first N prime numbers. (Method 1)

    by nikoo28
    4 minutes read

    Question: Given an integer N, find the prime numbers in that range from 1 to N. Input: N = 25 Output: 2, 3, 5, 7, 11, 13, 17, 19, 23 Today let us discuss about a very common but very interesting problem “To find prime numbers in first N Natural numbers “. I will be taking here a very specific approach of first giving definition of prime numbers , using that definition to derive the algorithm, and then using different properties or results that can be further applied to our …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    Determine if 2 rectangles overlap

    by nikoo28
    3 minutes read

    Question: You are given two axis-aligned rectangles. You have to determine if these rectangles overlap each other or not. Rectangle 1 : P1 (x, y), P2 (x,y) Rectangle 2 : P3 (x,y), P4 (x,y) In this problem statement, we are given co-ordinates for 2 rectangles and we have to determine if they overlap or not. In a way we can say that our use case looks like this. At the first glance, the problem seems to look very complicated. If you are coming up with a complicated set of conditionals, …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More