Find the maximum elements in the first and the second halves of the Array

, Given an array arr[] of N integers. The task is to find the largest elements in the first half and the second half of the array. Note that if the size of the array is odd then the middle element will be included in both halves.
Examples:Â
Input: arr[] = {1, 12, 14, 5}Â
Output: 12, 14Â
First half is {1, 12} and the second half is {14, 5}.
Input: arr[] = {1, 2, 3, 4, 5}Â
Output: 3, 5Â
Approach: Calculate the middle index of the array as mid = N / 2. Now the first halve elements will be present in the subarray arr[0…mid-1] and arr[mid…N-1] if N is even.Â
If N is odd then the halves are arr[0…mid] and arr[mid…N-1]
Below is the implementation of the above approach:Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to print largest element in// first half and second half of an arrayvoid findMax(int arr[], int n){Â
    // To store the maximum element    // in the first half    int maxFirst = INT_MIN;Â
    // Middle index of the array    int mid = n / 2;Â
    // Calculate the maximum element    // in the first half    for (int i = 0; i < mid; i++)        maxFirst = max(maxFirst, arr[i]);Â
    // If the size of array is odd then    // the middle element will be included    // in both the halves    if (n % 2 == 1)        maxFirst = max(maxFirst, arr[mid]);Â
    // To store the maximum element    // in the second half    int maxSecond = INT_MIN;Â
    // Calculate the maximum element    // int the second half    for (int i = mid; i < n; i++)        maxSecond = max(maxSecond, arr[i]);Â
    // Print the found maximums    cout << maxFirst << ", " << maxSecond;}Â
// Driver codeint main(){Â Â Â Â int arr[] = { 1, 12, 14, 5 };Â Â Â Â int n = sizeof(arr) / sizeof(arr[0]);Â
    findMax(arr, n);Â
    return 0;} |
Java
// Java implementation of the approachimport java.io.*;Â
class GFG{    static void findMax(int []arr, int n)     {              // To store the maximum element         // in the first half         int maxFirst = Integer.MIN_VALUE;              // Middle index of the array         int mid = n / 2;              // Calculate the maximum element         // in the first half         for (int i = 0; i < mid; i++)         {            maxFirst = Math.max(maxFirst, arr[i]);         }             // If the size of array is odd then         // the middle element will be included         // in both the halves         if (n % 2 == 1)         {            maxFirst = Math.max(maxFirst, arr[mid]);         }                 // To store the maximum element         // in the second half         int maxSecond = Integer.MIN_VALUE;              // Calculate the maximum element         // int the second half         for (int i = mid; i < n; i++)         {            maxSecond = Math.max(maxSecond, arr[i]);         }                 // Print the found maximums         System.out.print(maxFirst + ", " + maxSecond);        // cout << maxFirst << ", " << maxSecond;     }          // Driver Code    public static void main(String[] args)    {        int []arr = { 1, 12, 14, 5 };         int n = arr.length;              findMax(arr, n);     } }Â
// This code is contributed by anuj_67.. |
Python3
# Python3 implementation of the approachimport sysÂ
# Function to print largest element in# first half and second half of an arraydef findMax(arr, n) :Â
    # To store the maximum element    # in the first half    maxFirst = -sys.maxsize - 1Â
    # Middle index of the array    mid = n // 2;Â
    # Calculate the maximum element    # in the first half    for i in range(0, mid):        maxFirst = max(maxFirst, arr[i])Â
    # If the size of array is odd then    # the middle element will be included    # in both the halves    if (n % 2 == 1):        maxFirst = max(maxFirst, arr[mid])Â
    # To store the maximum element    # in the second half    maxSecond = -sys.maxsize - 1Â
    # Calculate the maximum element    # int the second half    for i in range(mid, n):        maxSecond = max(maxSecond, arr[i])Â
    # Print the found maximums    print(maxFirst, ",", maxSecond)Â
# Driver codearr = [1, 12, 14, 5 ]n = len(arr)Â
findMax(arr, n)Â
# This code is contributed by ihritik |
C#
// C# implementation of the approachusing System;Â
class GFG{    static void findMax(int []arr, int n)     {              // To store the maximum element         // in the first half         int maxFirst = int.MinValue;              // Middle index of the array         int mid = n / 2;              // Calculate the maximum element         // in the first half         for (int i = 0; i < mid; i++)         {            maxFirst = Math.Max(maxFirst, arr[i]);         }             // If the size of array is odd then         // the middle element will be included         // in both the halves         if (n % 2 == 1)         {            maxFirst = Math.Max(maxFirst, arr[mid]);         }                 // To store the maximum element         // in the second half         int maxSecond = int.MinValue;              // Calculate the maximum element         // int the second half         for (int i = mid; i < n; i++)         {            maxSecond = Math.Max(maxSecond, arr[i]);         }                 // Print the found maximums         Console.WriteLine(maxFirst + ", " + maxSecond);        // cout << maxFirst << ", " << maxSecond;     }          // Driver Code    public static void Main()    {        int []arr = { 1, 12, 14, 5 };         int n = arr.Length;              findMax(arr, n);     } }Â
// This code is contributed by nidhiva |
Javascript
// javascript implementation of the approach    function findMax(arr, n)    {              // To store the maximum element        // in the first half                 var maxFirst = Number.MIN_VALUE              // Middle index of the array        var mid = n / 2;              // Calculate the maximum element        // in the first half        for (var i = 0; i < mid; i++)        {            maxFirst = Math.max(maxFirst, arr[i]);        }              // If the size of array is odd then        // the middle element will be included        // in both the halves        if (n % 2 == 1)        {            maxFirst = Math.max(maxFirst, arr[mid]);        }                  // To store the maximum element        // in the second half        var maxSecond = Number.MIN_VALUE              // Calculate the maximum element        // int the second half        for (var i = mid; i < n; i++)        {            maxSecond = Math.max(maxSecond, arr[i]);        }                  // Print the found maximums        document.write(maxFirst + ", " + maxSecond);    }          // Driver Code        var arr = [ 1, 12, 14, 5 ];        var n = arr.length;              findMax(arr, n);Â
 // This code is contributed by bunnyram19. |
12, 14
Â
Time Complexity: O(n), since the loop runs from 0 to (mid – 1), and then from mid to (n – 1).
Auxiliary Space: O(1), since no extra space has been taken.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



