Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number.(Java)

huangapple go评论84阅读模式
英文:

Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number.(Java)

问题

import java.util.Arrays;
import java.util.Scanner;

public class ThirdLargest {
    public static void main(String[] args) {

        int temp = 0;
        int temp2 = 0;
        int max = 0;
        /*To get the length of the array from the user*/
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the length of the array");

        int length = sc.nextInt();
        int[] myArray = new int[length];

        System.out.println("Enter the elements of the array:");
        /*Adding the numbers provided by the user as an array*/
        for (int i = 0; i < length; i++) {
            myArray[i] = sc.nextInt();
        }

        /*Sorting the array in descending order*/
        Arrays.sort(myArray);
        int distinctCount = 1;

        /*Counting the number of distinct elements in the array*/
        for (int i = length - 2; i >= 0; i--) {
            if (myArray[i] != myArray[i + 1]) {
                distinctCount++;
                if (distinctCount == 3) {
                    System.out.println("The third maximum is: " + myArray[i]);
                    return;
                }
            }
        }

        System.out.println("The array does not have a third maximum element.");
    }
}
英文:

For a given array input,the output should be the third maximum number from the list of numbers provided by the user.
Example 1:
Input: [3, 2, 1]

Output: 1

Explanation: The third maximum is 1.
Example 2:
Input: [1, 2]

Output: 2
I have shared my code below along with the comments.

import java.util.Arrays;
import java.util.Scanner;

public class ThirdLargest {
public static void main(String[] args) {

    int temp = 0;
    int temp2 = 0;
    int max = 0;
    /*To get the length of the array from the user*/
    Scanner sc = new Scanner(System.in);
    System.out.println(&quot;Enter the length of the array&quot;);

    int length = sc.nextInt();
    int[] myArray = new int[length];
    
    System.out.println(&quot;Enter the elements of the array:&quot;);
    /*Adding the numbers provided by the user as an array*/
    for (int i = 0; i &lt; length; i++) {
        myArray[i] = sc.nextInt();
    }

    System.out.println(Arrays.toString(myArray));
    /*trying to get the maximum number from the array*/
    for (int i = 0; i + 1 &lt; length - 1; i++) {
        if (myArray[i] &lt; myArray[i + 1]) {
            /*Storing the maximum number in temp*/
            temp = myArray[i + 1];
        } else
            temp = myArray[i];
        /*Trying to get the second maximum number from the array and storing it in temp2*/
        if (myArray[i] &gt; myArray[i + 1] &amp;&amp; myArray[i] &lt; temp)
            temp2 = myArray[i];
        /*Trying to get the third maximum number from the array and storing it as max*/
        if (myArray[i] &gt; myArray[i + 1] &amp;&amp; myArray[i] &lt; temp &amp;&amp; myArray[i] &lt; temp2)
            max = myArray[i];
        System.out.println(max);
    }
}}

My result is:

Result

I am getting the result as 0 and 0. I should actually get 2. Please help.

答案1

得分: 0

import java.util.Arrays;
import java.util.Scanner;

public class ThirdLargest {
    public static void main(String[] args) {

        int temp = 0;
        int temp2 = 0;
        int max = 0;
        /*To get the length of the array from the user*/
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the length of the array");

        int length = sc.nextInt();
        int[] myArray = new int[length];

        System.out.println("Enter the elements of the array:");
        /*Adding the numbers provided by the user as an array*/
        for (int i = 0; i < length; i++) {
            myArray[i] = sc.nextInt();
        }

        System.out.println(Arrays.toString(myArray));
        for (int i = 0; i < length; i++) {
            for (int j = i + 1; j < length; j++) {
                if (myArray[i] > myArray[j]) {
                    temp = myArray[i];
                    myArray[i] = myArray[j];
                    myArray[j] = temp;
                }
            }
        }
        System.out.println(myArray[2]);
英文:
import java.util.Arrays;

import java.util.Scanner;

public class ThirdLargest {
public static void main(String[] args) {

    int temp = 0;
    int temp2 = 0;
    int max = 0;
    /*To get the length of the array from the user*/
    Scanner sc = new Scanner(System.in);
    System.out.println(&quot;Enter the length of the array&quot;);

    int length = sc.nextInt();
    int[] myArray = new int[length];

    System.out.println(&quot;Enter the elements of the array:&quot;);
    /*Adding the numbers provided by the user as an array*/
    for (int i = 0; i &lt; length; i++) {
        myArray[i] = sc.nextInt();
    }

    System.out.println(Arrays.toString(myArray));
    for (int i = 0; i &lt; length; i++) {
        for (int j = i + 1; j &lt; length; j++) {
            if (myArray[i] &gt; myArray[j]) {
                temp = myArray[i];
                myArray[i] = myArray[j];
                myArray[j] = temp;
            }
        }
    }
    System.out.println(myArray[2]);

答案2

得分: 0

以下是翻译好的代码部分:

public static void main(String[] args) {
    Random rr = new Random();
    int[] a = rr.ints(10, 1, 100).toArray();

    // 降序排列

    int first = Integer.MIN_VALUE;
    int second = Integer.MIN_VALUE;
    int third = Integer.MIN_VALUE;
    for (int v : a) {
        if (first < v) {
            third = second;
            second = first;
            first = v;
        } else if (second < first && second < v) {
            third = second;
            second = v;
        } else if (third < v) {
            third = v;
        }
    }
    int len = a.length;
    System.out.println(len > 2 ? "第三大的数 = " + third :
            len > 1 ? "第二大的数 = " + second :
            "最大的数 = " + first);
    System.out.println(Arrays.toString(a));
}
英文:

There is no need to sort the array to do this. It can be done in O(n) time by checking relative values as you iterate once thru the list.

	public static void main(String[] args) {
		Random rr = new Random();
		int[] a = rr.ints(10, 1, 100).toArray();
		
		// descending order
		
		int first = Integer.MIN_VALUE;
		int second = Integer.MIN_VALUE;
		int third = Integer.MIN_VALUE;
		for (int v : a) {
			if (first &lt; v) {
				third = second;
				second = first;
				first = v;
			} else if (second &lt; first &amp;&amp; second &lt; v) {
				third = second;
				second = v;
			} else if (third &lt; v) {
				third = v;
			}
		}
		int len = a.length;
		System.out.println(len &gt; 2 ? &quot;Third largest = &quot; + third :
				len &gt; 1 ? &quot;Second largest = &quot; + second :
				&quot;Largest = &quot; + first);
		System.out.println(Arrays.toString(a));
	}


</details>



huangapple
  • 本文由 发表于 2020年4月9日 22:10:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/61123149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定