find kth max and min element in an array

Solutions on MaxInterview for find kth max and min element in an array by the best coders in the world

showing results for - "find kth max and min element in an array"
Brinley
23 Jul 2018
1#include <bits/stdc++.h> 
2using namespace std;
3
4int main(){
5  	const int size = 10;
6  	int k = 3; //Can be any number smalled than size
7	int array[size] = {5,2,8,34,73,82,1,6,19,29};
8	sort(array,array+size);
9	int smallestKthElement = array[k-1];	
10	int largestKthElement = array[size-k];
11}
Evan
26 Feb 2019
1def divideAndConquer_Max(arr, ind, len):
2	maximum = -1;
3
4	if (ind >= len - 2):
5		if (arr[ind] > arr[ind + 1]):
6			return arr[ind];
7		else:
8			return arr[ind + 1];
9
10
11	maximum = divideAndConquer_Max(arr, ind + 1, len);
12
13	if (arr[ind] > maximum):
14		return arr[ind];
15	else:
16		return maximum;
17
18
19def divideAndConquer_Min(arr, ind, len):
20	minimum = 0;
21	if (ind >= len - 2):
22		if (arr[ind] < arr[ind + 1]):
23			return arr[ind];
24		else:
25			return arr[ind + 1];
26
27	minimum = divideAndConquer_Min(arr, ind + 1, len);
28
29	if (arr[ind] < minimum):
30		return arr[ind];
31	else:
32		return minimum;
33
34
35if __name__ == '__main__':
36
37	minimum, maximum = 0, -1;
38
39	# array initialization
40	arr = [6, 4, 8, 90, 12, 56, 7, 1, 63];
41
42	maximum = divideAndConquer_Max(arr, 0, 9);
43	minimum = divideAndConquer_Min(arr, 0, 9);
44
45	print("The minimum number in the array is: ", minimum);
46	print("The maximum number in the array is: ", maximum);
Kenny
06 Feb 2017
1
2function nthlargest(arra,highest){
3			var x = 0,
4				y = 0,
5				z = 0,
6				temp = 0,
7				tnum = arra.length, 
8				flag = false, 
9				result = false; 
10   
11			while(x < tnum){
12				y = x + 1; 
13				
14				if(y < tnum){
15					for(z = y; z < tnum; z++){
16						
17						if(arra[x] < arra[z]){
18							temp = arra[z];
19							arra[z] = arra[x];
20							arra[x] = temp;
21							flag = true; 
22						}else{
23							continue;
24						}	
25					}					
26				}
27				
28				if(flag){
29					flag = false;
30				}else{
31					x++; 
32					if(x === highest){ 
33                      
34						result = true;
35					}	
36				}
37				if(result){
38					break;
39				}
40			}
41
42			return (arra[(highest - 1)]);	
43		}
44		
45console.log(nthlargest([ 43, 56, 23, 89, 88, 90, 99, 652], 4)); 
46
47
queries leading to this page
find the kth minimum and maximum in an arrayhow to find the minimum and maximum number of a arraykth min max elementmaximum and minimum element in the given array by comparisonsjavascript finding the smallest element in an arrayfind the kth max and min element of an array13 find largest element of given array largest element in arrayhow to find the k maximum element in an arraykth max and min element in array bestfind largest number in arrayusing javascriptyou are given an array of integers find the sum of first k smallest numbers js largest number in arrayfinding the largest number in an array javascriptfind fourth largest in unsorted arrayarray largest number in jsget largest number in array javascriptfind k largest elements in array gfgfind minimum and maximum valuefind the minimum and maximum in an arrayalgorithm to find maximum and minimum number in an arrayfind the kth max and min element of an array practicehow to find largest element in arraylargest element in array javascriptkth largest element in an arrayfind the largest number in array javascriptfind k largest elements in array javascripthow to find largest number in array javascriptfind the minimum and maximum element in an arrayk largest elementswhat are the maximum and minimum values of a functionkth largest element in an array using heapkth smallest element array using javascriptfind the minimum and maximum element in an array minimum number of steps to get the maximum score of the arraylargest javascript arraykth max and min element in array2nd largest number in array javascriptcalculate the minimum element in the arrayfind the kth largest element in an unsorted array javascripthow to get the largest element in an arraylargest element in array pythonfind biggest arrar element in jshow to define maximum and minimum value of an arrayfind the min and max elems of the arrayfunction that returns the largest number in the array in javascriptfind maximum and minumum arrayhow to find minimum in arrayfind maximum and minimum element in an array gfgkth largest element in an array javafind kth largest element in arrayextract kth from array javascriptthis function sorts the elements of the array in descending order and returns the element at the nth index in the sorted array sample input 3a array 3d 5b43 2c 56 2c 23 2c 89 2c 88 2c 90 2c 99 2c 652 5d 2c positive integer n 3d 4 2cthen the return value must be 88 minimum and maximum element in an array in onfind minimum number in array using min and maxmaximum and minimum element in the given arraymaximum and minimum elements in an arraykth largest element in an array coding ninjsfind largest number in array javascriptsee which array is the biggest element javascriptfinding minimum value in array of arraysfunction that takes an array of numbers and return both the minimum and maximum numbers 2c in that order function find largest index the array javascripthow to get the largest number in array jshow can i find the maximum minimum from an array 3fwrite a program to find the maximum and minimum value of an arrayfind the 22kth 22 max and min element of an arrayjavascript k largest elementkth max element in arrayhow to find the minimum element in an arrayhow to find the largest element in an arraymaximum and minimum in an arrayfind kth largest element in array jslargest value in array javascriptfind largest number in array jsjs array find largest valuefind minimum and maximum element in an array in javafind out the maximum and minimumfrom an arrayjavascript largest number in arrayfind minimum value in arrayfind the minimum number in an arrayfind the kth max and min element of an array pythonminimum and maximum e element in an arrayhow to find the minimum and maximum of a functionfind kth max and min element in an array practicelargest number in array javascriptfind kth min max arrayfind the largest element of an array in javascriptkth largest element in an array javascriptjavascript find largest element in arrayfind maximum and second max element in arrayfind largest index javascriptwrite a javascript function to get largest element from an unsorted arrayget the largest number in array javascriptfind the kth largest element javascriptkth largest element in an array heapkth largest elementminimum and maximum value of a functionfinding minimum value in an array find the kth maximum and minimum element in an arrayhow to find maximum and minimum element in two arraysmaximum and minimum element in an arrayfind kth max and min element in an arrayjs find largest number in arrayfind nth largest number in array jsfind the largest number from array in javascripthow to find minimum value of arrayk th min max element in arrayfind maximum and minimum element in an arrayjs take largest element from each arrayfind the k th max 2fmin in an arraymaximum minimum element in arrayminimum and maximum element in an arrayfinding largest number in array javascriptmaximum element in array c 2b 2bfind largest among array jsfind largest element in array javascriptfind minimum number in array using min and max functionget largest no in the array javascriptwrite a javascript function to get nth largest element from an unsorted array how to find the largest number in an array javascriptfind maximum and minimum value of functionjavascript k largest element arr 2cn 2ckhow to find minimum element in an arrayfind k th largest element in jsmaximum and minimum of an arrayfind the minimum and maximum element in an arrafind max and min value in array algorithmgetting minimum and maximum value of a functionkth max min element in arrayfind largest value in array javascriptfind maximum and of k elements in an arrayreturn largest number in array javascriptmaximum and minimum of an array using minimum numberfind minimum and maximum element in an arraywrite a program to find min and max element in an arraywrite a javascript function to get nth largest element from an unsorted arrayfind out minimum and maximum numbers in an arrayminimum and maximum element in an array hackerrankfind the maximum and minimum element in an arrayfind minimum element in arraykth largest element in an array js solutionmaximum element in an arraykth largest element in an array gfgkth largest element in an array jsmaximum and minimum values of arrayfind maximum and minimum number arrayfind the largest element in an array javascripthow can i find the maximum minimum from a array 3ffind the biggest element in an array javascriptfind largest element in an unsorted array jsmaximum and minimum of array elementsfunction that finds and returns the minimum value in an array find kth max and min element in an array