kth max and min element in array code example
Example 1: find min and max in array c++
#include<iostream>
using namespace std;
public void getMax_MinValue(int arr[])
{
int max, min;
max = arr[0];
min = arr[0];
for (int i = 0; i < sizeof(arr); i++)
{
if (max < arr[i])
max = arr[i];
else if (min > arr[i])
min = arr[i];
}
cout << "Largest element : " << max;
cout << "Smallest element : " << min;
}
Example 2: kth largest element in an array javascript
function nthlargest(arra,highest){
var x = 0,
y = 0,
z = 0,
temp = 0,
tnum = arra.length,
flag = false,
result = false;
while(x < tnum){
y = x + 1;
if(y < tnum){
for(z = y; z < tnum; z++){
if(arra[x] < arra[z]){
temp = arra[z];
arra[z] = arra[x];
arra[x] = temp;
flag = true;
}else{
continue;
}
}
}
if(flag){
flag = false;
}else{
x++;
if(x === highest){
result = true;
}
}
if(result){
break;
}
}
return (arra[(highest - 1)]);
}
console.log(nthlargest([ 43, 56, 23, 89, 88, 90, 99, 652], 4));