Training algorithm to find the maximum value in the array
Resource constraints
Time limit: 1.0s Memory Limit: 512.0MB
Problem Description
For a given integer array A [], where to find the maximum value, and returns the index.
Input Format
Integer array a [], an equal number of array elements is less than 100. The output data is divided into two lines: the first is only a number representing the number of array elements; behavior of each element of the second array.
Output Format
Outputting value, and subscript
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] list = new int[n];
int maxIndex = 0;
for (int i = 0; i < n; i++) {
list[i] = in.nextInt();
if (list[i] > list[maxIndex]) {
maxIndex = i;
}
}
System.out.println(list[maxIndex] + " " + maxIndex);
}
}