public static void main(String[] args) { int[]myArray={1,2,33,56,34,85,32}; int[]mystore=new int[7]; int max=0; for(int i=0;i<myArray.length;i++) { if (myArray[i]>max) { max=myArray[i]; } } System.out.println("maximum num = " + max); System.out.println(Arrays.toString(mystore)); /*int secondLargest = 0; for(int i=0; i<myArray.length; i++) { if(myArray[i] < max && myArray[i] > secondLargest) { secondLargest = myArray[i]; } } System.out.println("The second Largest num in the array is = " + secondLargest); */ int secondlargest=0; for(int i=0;i<myArray.length;i++) { mystore[i]=max-myArray[i]; // max - myArray[i] = 85-1= 84, 85-2= 83, 85-33= 52... } System.out.println(Arrays.toString(mystore)); for(int i=0;i<myArray.length-1;i++) { if ((mystore[i]!=0) && (mystore[i]<=mystore[i+1]+1)) secondlargest=i; } System.out.println("second largest num = " + myArray[secondlargest]); } } //outPut: maximum num = 85 [0, 0, 0, 0, 0, 0, 0] [84, 83, 52, 29, 51, 0, 53] second largest num = 56
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter