In the Previous Tutorial (Array KickStart) We done with Array Basic CRUD Operation. Now this is the time to Dirty our Hands to Practice More Questions

Let's start Practice the Array Questions

Problem-1 Start with Finding the Largest Element in Array Problem

Example: Given is an array {100,20,900,1000,555,22}, The Largest Element is 1000

The Idea comes in Mind is Compare all the Array Elements One By One with others Elements if any next element comes greater than exit and choose the next element and compare the same with all and repeat the process



public class LargestElementInArray02 {
    int arr [];
    LargestElementInArray02(){
        arr = new int [] {100,20,900,1000,555,22};
    }
    /*
    In this Approach Compare all the Elements One By One with all 
    Elements if any next element comes greater than exit and 
    choose the next element and compare the same with all and 
    repeat the process
    */
    void approach1(){
        boolean isMax ;        
        for(int i = 0; i<arr.length; i++){
            isMax = true;
            for(int j= 0; j<arr.length; j++){
                if(arr[j]>arr[i]){
                    isMax = false;
                    break; // exit from the current loop
                } // if ends
            } // inner loop ends
            if(isMax){
                System.out.println("Largest Element is "+arr[i]);
                return ; 
            }
        }
    }
    public static void main(String[] args) {
            new LargestElementInArray02().approach1();   
    }
}

Time Complexity of the Above Approach is O(N * N)

Another Approach can be Sort in Array in Descending Order and get the 0th index element.

 Arrays.sort(arr, Collections.reverseOrder());
        System.out.println(arr[0]);

But Keep in mind Arrays.sort with Collections.reverserOrder() not work with primitive types so you need to change it into Wrapper Types. e.g Integer arr = new Integer [] {100,20,900,1000,555,22};

Time Complexity of the Above Approach is (NLogN). Java Uses Internally Quick Sort and TC is NLogN.

Let's Think for Better Solution.

The Solution is Single Pass, We Iterate all the elements one by one and storing the max value in the max variable. In Each Iteration we are checking is our current value in array is greater than the max so it replace the previous max value.

void approach2(){
        int max = arr[0];
        for(int i = 1; i<arr.length; i++){
            if(arr[i]>max){
                max = arr[i];
            }
        }
        System.out.println("Max is "+max);
    }

Now Jumping to the Next Problem Statement.

Problem2 : Find the Second Largest Element in the Array?

Given is the Array as Input int arr[] = {10,20,3,20,19,15}; Output is 19 (It is the Second Largest Element in the Array)

Approach is First Find the First Largest Element in an Array and then assign the previous largest in the secondMax variable. Also add the special case mention in the code.

You can practice this problem Here Second Largest Element in Array

public class FindTheSecondLargestElement {

    static void approach(){
          //int arr[] = {10,20,3,20,19,15};
        //int arr[] = {1000,1000,1000,100}; // special case
        int arr[] = {1000,1000,1000}; // no second largest
        // first find the first max element
        int max = arr[0];
        int secondMax = -1;
        for(int i = 1; i<arr.length; i++){
            if(arr[i]>max){
                secondMax = max;
                max = arr[i];
            }
            if(arr[i]!=max){
                if(arr[i]>secondMax){
                    secondMax = arr[i];
                }
            }
            
        }
        System.out.println("Second Largest is "+secondMax);

    }
    public static void main(String[] args) {
        approach();

    }    
}

Now Moving to the Next Problem Statement

Problem -3 Span of an Array Problem

Here in this Question you need to find the diff b/w min and max element of an array. this is called as span of an array

Approach is find the Max Element and Min Element of an Array and then print the diff b/w the max and min.


public class SpanOfAnArray04 {
  public static void main(String[] args) {
      int arr[] = {10,20,11,90,9};
      int max = Integer.MIN_VALUE;
      int min = Integer.MAX_VALUE;
      for(int element : arr){
        if(element>max){
          max = element;
        }
        
        if(element<min){
          min = element;
        }
      }
      System.out.println("Span "+(max - min));

  }  
}

That's all Folks , Meet you in Next Tutorial 😀