Finding Kth largest number in given array of large numbers
Given an array arr[] of strings representing large numbers and an integer K, the task is to find Kth largest integer in given array.
Examples:
Input: arr[] = { “10”, “7”, “3”, “6” }, K = 3
Output: “6”
Explanation : Arranging the array in non-decreasing manner will give { “3”, “6”, “7”, “10” }.
Clearly 3rd largest integer is 6.
Input: arr[] = { “10”, “7”, “6”, “7”, “11” }, K = 2
Output: “10”
Explanation: Arranging the array in non-decreasing manner will give { “6”, “7”, “7”, “10”, “11” }
Clearly 2nd largest integer is 10.
Naive Approach (Incorrect): Usually such kind of problems can be solved by converting strings to integers and then finding the Kth largest number. But since we are talking about large numbers, i.e., strings representing integers up to length 100, so it not possible to convert them to integers.
Correct Approach: This problem can be solved using the Greedy Approach by implementing a custom comparator.
Follow the steps below to solve the given problem.
- Use custom sorting to sort the array in non-decreasing order.
- In the custom sort function there will be two cases :
- The size of two passed strings is equal, then swap only when string one is lexicographically smaller than the other.
- The size of two passed strings is un-equal, then swap only when the size of one string is smaller than the other.
- After sorting print Kth largest integer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool comp(string& a, string& b)
{
if (a.size() == b.size())
return a < b;
return a.size() < b.size();
}
string kthLargestInteger(
string arr[], int n, int k)
{
sort(arr, arr + n, comp);
return arr[n - k];
}
int main()
{
int N = 4;
string arr[N] = { "10" , "7" , "3" , "6" };
int K = 4;
cout << kthLargestInteger(arr, N, K)
<< endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static String kthLargestInteger(String arr[], int n,
int k)
{
Arrays.sort(arr, new Comparator<String>() {
@Override public int compare(String a, String b)
{
return Integer.valueOf(a).compareTo(
Integer.valueOf(b));
}
});
return arr[n - k];
}
public static void main(String[] args)
{
int N = 4 ;
String arr[] = { "10" , "7" , "3" , "6" };
int K = 4 ;
System.out.println(kthLargestInteger(arr, N, K));
}
}
|
Python3
def kthLargestInteger(
arr, n, k):
arr = [ int (i) for i in arr]
arr.sort()
return arr[n - k]
if __name__ = = "__main__" :
N = 4
arr = [ "10" , "7" , "3" , "6" ]
K = 4
print (kthLargestInteger(arr, N, K))
|
C#
using System;
public class GFG
{
static String kthLargestint(String []arr, int n,
int k)
{
Array.Sort(arr,CompareStrings);
return arr[n - k];
}
static int CompareStrings( string a, string b)
{
return Int32.Parse(a).CompareTo(
Int32.Parse(b));
}
public static void Main(String[] args)
{
int N = 4;
String []arr = { "10" , "7" , "3" , "6" };
int K = 4;
Console.WriteLine(kthLargestint(arr, N, K));
}
}
|
Javascript
<script>
function comp(a, b)
{
if (a.length == b.length)
return a - b;
return (a.length - b.length);
}
function kthLargestInteger(arr, n, k) {
arr.sort(comp);
console.log(arr)
return arr[n - k];
}
let N = 4;
let arr = [ "10" , "7" , "3" , "6" ];
let K = 4;
document.write(kthLargestInteger(arr, N, K))
</script>
|
Time Complexity: O(N * log N), Where N is the size of the array.
Auxiliary Space: O(1)
Last Updated :
19 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...