Question_2

PHOTO EMBED

Tue May 30 2023 19:06:34 GMT+0000 (Coordinated Universal Time)

Saved by @Mohamedshariif #java

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
content_copyCOPY