Program to Find K largest elements from array

Description

To find K largest elements from array, sort the array in ascending
order and print the elements from end.

C/C++

/* C program to Find K largest elements from array */
//Save it as FindGivenLargestElementArray.c

#include<stdio.h>
int main(){

    int i, k, n;

    printf("Enter the size of array : ");
    scanf("%d",&n);

    int arr[n];

    printf("Enter the array element : ");
    for(i=0;i<n;i++) {
        scanf("%d",&arr[i]);
    }

    printf("Enter the number of largest element to find : ");
    scanf("%d",&k);

    //Sort the array
    asc_sort(arr,n);

    //Print K number of elements from last
    printf("The %d largest elements of array : ",k);
    for(i=n-1;i>=(n-k);i--) {
        printf("%d ",arr[i]);
    }

    return 0;
}

//Function for sorting the array
void asc_sort(int a[], int n)
{
 int i, j, temp;
 for(i=0;i<n-1;i++)
 {
  for(j=i+1;j<n;j++)
  {
   if(a[i]>a[j])
   {
    temp = a[i];
    a[i] = a[j];
    a[j] = temp;
   }
  }
 }
}
Input:
Enter the size of array : 
5
Enter the array element : 
12
52
34
61
23
Enter the number of largest element to find : 
3

Output:
The 3 largest elements of array : 
61 52 34

Java

/* Java program to Find K largest elements from array */
//Save it as FindGivenLargestElementArray.java

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

public class FindGivenLargestElementArray {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter the size of array : ");
        int n = scanner.nextInt();
        
        int arr[] = new int[n];
        
        System.out.println("Enter the array element : ");
        for(int i=0;i<n;i++) {
            arr[i] = scanner.nextInt();
        }
        
        System.out.println("Enter the number of larget element to find : ");
        int k = scanner.nextInt();
                
                //Sort the array
                Arrays.sort(arr);
        
                //Print K number of elements from last
                System.out.println("The "+k+" largest elements of array : ");
                for(int i=n-1;i>=(n-k);i--) {
        	        System.out.print(arr[i]+" ");
                }
    }
}
Input:
Enter the size of array : 
5
Enter the array element : 
12
52
34
61
23
Enter the number of larget element to find : 
3

Output:
The 3 largest elements of array : 
61 52 34

Related Programs

1) Find Smallest element and it’s position of an Array
2) Program for finding Second Largest Element Of Array
3) Program to swap maximum and minimum element of Array
4) Program to Find the smallest missing number
5) Program to Find missing odd number in first n odd number
6) Program to Find Value equal to index value
7) write a Program to find index of two array elements whose sum is equal to given value
8) Program to find maximum and second maximum if elements of array is space separated input
9) Program to make pair of elements alternatively
10) Program to Print How Many Numbers Smaller than Current Number
Share Me

Leave a Reply