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++
#include <bits/stdc++.h>
using namespace std;
void findMax( int arr[], int n)
{
int maxFirst = INT_MIN;
int mid = n / 2;
for ( int i = 0; i < mid; i++)
maxFirst = max(maxFirst, arr[i]);
if (n % 2 == 1)
maxFirst = max(maxFirst, arr[mid]);
int maxSecond = INT_MIN;
for ( int i = mid; i < n; i++)
maxSecond = max(maxSecond, arr[i]);
cout << maxFirst << ", " << maxSecond;
}
int main()
{
int arr[] = { 1, 12, 14, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
findMax(arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void findMax( int []arr, int n)
{
int maxFirst = Integer.MIN_VALUE;
int mid = n / 2 ;
for ( int i = 0 ; i < mid; i++)
{
maxFirst = Math.max(maxFirst, arr[i]);
}
if (n % 2 == 1 )
{
maxFirst = Math.max(maxFirst, arr[mid]);
}
int maxSecond = Integer.MIN_VALUE;
for ( int i = mid; i < n; i++)
{
maxSecond = Math.max(maxSecond, arr[i]);
}
System.out.print(maxFirst + ", " + maxSecond);
}
public static void main(String[] args)
{
int []arr = { 1 , 12 , 14 , 5 };
int n = arr.length;
findMax(arr, n);
}
}
|
Python3
import sys
def findMax(arr, n) :
maxFirst = - sys.maxsize - 1
mid = n / / 2 ;
for i in range ( 0 , mid):
maxFirst = max (maxFirst, arr[i])
if (n % 2 = = 1 ):
maxFirst = max (maxFirst, arr[mid])
maxSecond = - sys.maxsize - 1
for i in range (mid, n):
maxSecond = max (maxSecond, arr[i])
print (maxFirst, "," , maxSecond)
arr = [ 1 , 12 , 14 , 5 ]
n = len (arr)
findMax(arr, n)
|
C#
using System;
class GFG
{
static void findMax( int []arr, int n)
{
int maxFirst = int .MinValue;
int mid = n / 2;
for ( int i = 0; i < mid; i++)
{
maxFirst = Math.Max(maxFirst, arr[i]);
}
if (n % 2 == 1)
{
maxFirst = Math.Max(maxFirst, arr[mid]);
}
int maxSecond = int .MinValue;
for ( int i = mid; i < n; i++)
{
maxSecond = Math.Max(maxSecond, arr[i]);
}
Console.WriteLine(maxFirst + ", " + maxSecond);
}
public static void Main()
{
int []arr = { 1, 12, 14, 5 };
int n = arr.Length;
findMax(arr, n);
}
}
|
Javascript
function findMax(arr, n)
{
var maxFirst = Number.MIN_VALUE
var mid = n / 2;
for ( var i = 0; i < mid; i++)
{
maxFirst = Math.max(maxFirst, arr[i]);
}
if (n % 2 == 1)
{
maxFirst = Math.max(maxFirst, arr[mid]);
}
var maxSecond = Number.MIN_VALUE
for ( var i = mid; i < n; i++)
{
maxSecond = Math.max(maxSecond, arr[i]);
}
document.write(maxFirst + ", " + maxSecond);
}
var arr = [ 1, 12, 14, 5 ];
var n = arr.length;
findMax(arr, n);
|
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.
Last Updated :
28 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...