Largest Element in an Array

Methods to Find the Largest Element

There are commonly two methods to find the largest element in an array.

  • Iterative Approach

  • Recursive Approach

  • Built-in functions in various languages

Iterative Approach

Algorithm:

  1. Start with an array:

    Let’s say our array is: [5,2,9,1,7]

  2. Initialize a variable to store the largest element of the array.

    • We’ll call this variable largest.

    • Initially, we’ll set the largest to the first element of the array.

    • So, largest=5

  3. Iterate through the array, comparing each element with the largest:

    Step 1:

    • Compare largest (5) With the next element, 2.

    • Is 2 greater than 5? No.

    • largest remains 5.

Step 2:

  • Compare largest (5) with the next element, 9.

  • Is 9 greater than 5? Yes.

  • Update largest to 9.

Step 3:

  • Compare largest (9) with the next element, 1.

  • Is 1 greater than 9? No.

  • largest remains 9.

Step 4:

  • Compare largest (9) with the next element, 7.

  • Is 7 greater than 9? No.

  • largest remains 9.

  1. At the end, the value stored in the largest (9) is our answer.

     #include <iostream>
     #include <bits/stdc++.h>
     using namespace std;
     int findLargest(vector<int> arr){
          int largest=arr[0];
         for(int i=0;i<arr.size();i++){
             if(arr[i]>largest){
                 largest=arr[i];
             }
         }
         return largest;
     }
     int main()
     {
         vector<int> arr={9,2,1,4,90};
         int larg=findLargest(arr,0);
         cout<<larg<<endl;
         return 0;
     }
    

Time Complexity: O(N)

Space Complexity: O(1)

Recursive Approach

Algorithm:

  1. Start with an array:

    Let's say our array is [5,2,9,1,7]

  2. Define a recursive function:

    • The function will take the array and the current index as input.

    • The base case is when the index reaches the last element of the array. In this case, the function returns that element.

    • For the recursive step, the function compares the current element with the largest element of the rest of the array(found by calling the function recursively).

  3. Recursive process:

    Here’s how the function will execute:

    • Initial call: findLargest([15,6,22,8,19],0)

    • Step 1:

      • Compare 15 with the largest of [6,22,8,19]

      • Call findLargest([15,6,22,8,19],1)

    • Step 2:

      • Compare 6 with the largest of [22,8,19]

      • Call findLargest([15,6,22,8,19],2)

    • Step 3:

      • Compare 22 with the largest of[8,19]

      • Call findLargest([15,6,22,8,19],3)

    • Step 4:

      • Compare 8 with the largest of[19]

      • Call findLargest([15,6,22,8,19],4)

    • Base case:

      • index 4 is the last element (19), return 19
    • Step 4 (return):

      • Compare 8 with 19, return 19
    • Step 3 (return):

      • Compare 22 with 19, return 22
    • Step 2 (return):

      • Compare 6 with 22, return 22
    • Step 1 (return):

      • Compare 15 with 22, return 22
    • Result:

      • The largest element is 22.
    #include <iostream>
    #include <bits/stdc++.h>
    using namespace std;
    int findLargest(vector<int> arr,int index){
        if(index==arr.size()-1){
            return arr[index];
        }
        int curr=arr[index];
        return max(curr,findLargest(arr,index+1));
    }
    int main()
    {
        vector<int> arr={9,2,1,4,90};
        int larg=findLargest(arr,0);
        cout<<larg<<endl;
        return 0;
    }

Time Complexity: O(N)

Space Complexity: O(N) => Because a recursive stack space is used

Using Built-in Functions:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int arr[]={9,2,1,4,90};
    int n=5;
    int larg=*max_element(arr,arr+n);
    cout<<larg<<endl;
    return 0;
}

Time Complexity: O(N)

Space Complexity: O(1)

1
Subscribe to my newsletter

Read articles from Gaurav Dungriyal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Gaurav Dungriyal
Gaurav Dungriyal