joker
joker

Reputation: 74

How to get largest n element from array?

I want to get larget n number of elements from array. Here down is my program that prints only maximum number from array but I want to print higest 10 elements or n elements from array that depends on user how many higest number he want to print from the array.

import java.util.*;
public class anna
{
    public static void main(String[] args)
    {
        int n, max;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the number of elements in the array:");
        n = s.nextInt();
        int a[] = new int[n];
        System.out.println("Enter the elements of array:");
        for(int i = 0; i < n; i++)
        {
            a[i] = s.nextInt();
        }
        max = a[0];
        for(int i = 0; i < n; i++)
        {
            if(max < a[i])
            {
                max = a[i];
            }
        }
        System.out.println("Maximum value in the array is:"+max);
    }
}

Upvotes: 0

Views: 120

Answers (2)

Yuanhao
Yuanhao

Reputation: 136

https://stackoverflow.com/a/72474427/15968954
This answer is Selection Sort, I think it's may difficult for you.
Maybe this code segment will help you.

import java.util.*;
public class anna
{
    public static void main(String[] args)
    {
        int n, max;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of elements in the array:");
        n = sc.nextInt();
        int arr[] = new int[n];
        System.out.println("Enter the elements of array:");
        for(int i = 0; i < n; i++)
        {
            arr[i] = s.nextInt();
        }

        // Sort Array
        Arrays.sort(arr);
        int k = sc.nextInt();
        System.out.println("Largest k Maximum value in the array is:" + arr[k - 1]);
    }
}

If you are a new learner, I suggest that you can more exercise through OJ.

Upvotes: 1

Faheem azaz Bhanej
Faheem azaz Bhanej

Reputation: 2396

You have to apply Selection Sort on your array then printout highest n number from array.

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        int n;
        int howMuch;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the number of elements in the array: ");
        n = s.nextInt();
        
        int arr[] = new int[n];
        System.out.println("Enter the elements of array: ");
        for(int i = 0; i < n; i++)
        {
            arr[i] = s.nextInt();
        }
        
        for(int i = 0; i < arr.length - 1; i++)
        {
            int max = i;
            for(int j = i + 1; j < arr.length; j++)
            {
                if (arr[j] > arr[max]) {
                    max = j;
                }
            }
            int temp = arr[i];
            arr[i] = arr[max];
            arr[max] = temp;
        }
        
        System.out.println("How many maximum number you have to print: ");
        howMuch = s.nextInt();
        
        if(howMuch < arr.length)
        {
            for(int i = 0; i < howMuch; i++)
            {
                System.out.print(arr[i] + " ");
            }
        }
        else
        {
            System.out.println("Please insert valid number because you have " + arr.length + " elements in your array");

        }
    }
}

Upvotes: 1

Related Questions