/*Find the maximum algorithm * 1. Assume that the first number is the maximum value * int max = arr[0]; * 2. Traverse the remaining elements, using the remaining elements to compare with Max, if the remaining elements are greater than Max, modify the max value to be larger * for (int i=1;i<arr.length;i++) {* if (Arr[i]>max) {* Max=arr[i] ; * } * } * */Importjava.util.Arrays; Public classMaxofarray { Public Static voidMain (string[] args) {int[] arr =New int[10]; for(inti=0;i<arr.length;i++) {Arr[i]=(int) (Math.random () *100);//assigning random numbers to arraysSystem.out.println (Arr[i]);//assignment good one output a}/*//for (int i=0;i<arr.length;i++) {System.out.println (arr[i]);//All values are assigned good after output}*/ intmax = arr[0];//assuming the first element is the largest for(inti=1;i<arr.length;i++) {//traversing the remaining elements if(Arr[i]>max) {//The remaining elements are compared to max, and if the remaining elements are larger than Max, modify the max value toMax=arr[i];//change the max value to a larger}}system.out.println ("Maximum value is:" +max); //Expand one capacity (the extended element is the default)arr = arrays.copyof (arr,arr.length+1); //assign Max Max to the last element in ArrARR[ARR.LENGTH-1] =Max; for(inti=0;i<arr.length;i++) {System.out.println (arr[i]); } } }
Find Maximum algorithm (interview question)