Largest element in the longest Subarray consisting of only Even or only Odd numbers
Last Updated :
09 Nov, 2023
Given an array arr[] of size N, the task is to find the largest element in the longest subarray consisting only of even numbers or odd numbers.
Examples:
Input: arr[] = { 2, 4, 6, 9, 10, 11 }
Output: 6
Explanation:
The longest subarray consisting of only even or odd numbers is { arr[0], arr[1], arr[2] }.
Since the largest element of the subarray is arr[2], the required output is 6.
Input: arr[] = { 3, 5, 7, 4, 9, 11, 13 }
Output: 13
Explanation:
The longest subarray consisting of only even or odd numbers are { {3, 5, 7 }, { 9, 11, 13 } }.
The largest elements in the subarrays are 7 and 13 respectively. 13 being the largest, is the required output.
Naive Approach
The idea is to find all subarrays and in that find those subarrays whose all elements are even or all elements are odd. Then in those subarrays find the longest subarray and then the largest element of that longest subarray.
Steps to implement-
- Declare a variable temp to store the length of the required longest subarray
- Declare a variable ans to store the largest element of the required longest subarray
- Run two for loops to find all subarrays and simultaneously find the length of the subarray and the largest element present in that subarray
- Pick that sub-array which contain all even elements or odd elements
- Then if that subarray has the longest length till now from all other subarrays which contain all even elements or all odd elements
- Then update ans with the maximum of ans and the largest element present in that subarray
Code-
C++
#include <bits/stdc++.h>
using namespace std;
int maxelementLongestSub( int arr[], int N)
{
int temp=0;
int ans=INT_MIN;
for ( int i=0;i<N;i++){
int length=0;
int largest=INT_MIN;
for ( int j=i;j<N;j++){
length++;
largest=max(largest,arr[j]);
bool val= false ;
int k=i;
while (k<=j){
if (arr[k]%2!=0){ break ;}
k++;
}
if (k==j+1){val= true ;}
k=i;
while (k<=j){
if (arr[k]%2!=1){ break ;}
k++;
}
if (k==j+1){val= true ;}
if (val== true ){
if (length>=temp){
temp=length;
ans=max(ans,largest);
}
}
}
}
return ans;
}
int main()
{
int arr[] = { 1, 3, 5, 7, 8, 12, 10 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << maxelementLongestSub(arr, N);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int maxelementLongestSub( int [] arr, int N)
{
int temp = 0 ;
int ans = Integer.MIN_VALUE;
for ( int i = 0 ; i < N; i++) {
int length = 0 ;
int largest = Integer.MIN_VALUE;
for ( int j = i; j < N; j++) {
length++;
largest = Math.max(largest, arr[j]);
boolean val = false ;
int k = i;
while (k <= j) {
if (arr[k] % 2 != 0 ) {
break ;
}
k++;
}
if (k == j + 1 ) {
val = true ;
}
k = i;
while (k <= j) {
if (arr[k] % 2 != 1 ) {
break ;
}
k++;
}
if (k == j + 1 ) {
val = true ;
}
if (val == true ) {
if (length >= temp) {
temp = length;
ans = Math.max(ans, largest);
}
}
}
}
return ans;
}
public static void main(String[] args)
{
int [] arr = { 1 , 3 , 5 , 7 , 8 , 12 , 10 };
int N = arr.length;
System.out.println(maxelementLongestSub(arr, N));
}
}
|
Python3
def maxelementLongestSub(arr, N):
temp = 0
ans = float ( "-inf" )
for i in range (N):
length = 0
largest = float ( "-inf" )
for j in range (i, N):
length + = 1
largest = max (largest, arr[j])
val = False
k = i
while k < = j:
if arr[k] % 2 ! = 0 :
break
k + = 1
if k = = j + 1 :
val = True
k = i
while k < = j:
if arr[k] % 2 ! = 1 :
break
k + = 1
if k = = j + 1 :
val = True
if val:
if length > = temp:
temp = length
ans = max (ans, largest)
return ans
if __name__ = = "__main__" :
arr = [ 1 , 3 , 5 , 7 , 8 , 12 , 10 ]
N = len (arr)
print (maxelementLongestSub(arr, N))
|
C#
using System;
public class GFG
{
static int MaxElementLongestSub( int [] arr, int N)
{
int temp = 0;
int ans = int .MinValue;
for ( int i = 0; i < N; i++)
{
int length = 0;
int largest = int .MinValue;
for ( int j = i; j < N; j++)
{
length++;
largest = Math.Max(largest, arr[j]);
bool val = false ;
int k = i;
while (k <= j)
{
if (arr[k] % 2 != 0)
{
break ;
}
k++;
}
if (k == j + 1)
{
val = true ;
}
k = i;
while (k <= j)
{
if (arr[k] % 2 != 1)
{
break ;
}
k++;
}
if (k == j + 1)
{
val = true ;
}
if (val == true )
{
if (length >= temp)
{
temp = length;
ans = Math.Max(ans, largest);
}
}
}
}
return ans;
}
public static void Main( string [] args)
{
int [] arr = { 1, 3, 5, 7, 8, 12, 10 };
int N = arr.Length;
Console.WriteLine(MaxElementLongestSub(arr, N));
}
}
|
Javascript
function maxelementLongestSub(arr, N)
{
let temp=0;
let ans=Number. MIN_VALUE;
for (let i=0;i<N;i++){
let length=0;
let largest=Number. MIN_VALUE;
for (let j=i;j<N;j++){
length++;
largest=Math.max(largest,arr[j]);
let val= false ;
let k=i;
while (k<=j){
if (arr[k]%2!=0){ break ;}
k++;
}
if (k==j+1){val= true ;}
k=i;
while (k<=j){
if (arr[k]%2!=1){ break ;}
k++;
}
if (k==j+1){val= true ;}
if (val== true ){
if (length>=temp){
temp=length;
ans=Math.max(ans,largest);
}
}
}
}
return ans;
}
let arr = [ 1, 3, 5, 7, 8, 12, 10 ];
let N = arr.length;
console.log(maxelementLongestSub(arr, N));
|
Output-
7
Time Complexity: O(N3), because of two nested loops to find all subarray and a third loop to find that subarray contains all even elements or odd elements
Auxiliary Space: O(1), because no extra space has been used
Approach: Follow the steps below to solve the problem:
- Initialize a variable, say maxLen, to store the length of the longest subarray obtained till ith index, which contains either even numbers or odd numbers only.
- Initialize a variable, say Len, to store the length of the current subarray upto the ith array element, consisting only of even or odd numbers.
- Initialize a variable, say MaxElem, to store the largest element of the longest subarray obtained till ith index which consists only of even or odd elements.
- Traverse the array using variable i. For every ith array element, check if arr[i] % 2 is equal to arr[i – 1] % 2 or not. If found to be true, then increment the value of Len.
- Otherwise, update the value of Len = 1.
- If Len >= maxLen, then update MaxElem = max(MaxElem, arr[i]).
- Finally, print the value of MaxElem.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int maxelementLongestSub( int arr[], int n)
{
int MaxElem = arr[0];
int maxLen = 1;
int Len = 1;
int Max = arr[0];
for ( int i = 1; i < n; i++) {
if (arr[i] % 2 == arr[i - 1] % 2) {
Len++;
if (arr[i] > Max)
Max = arr[i];
if (Len >= maxLen) {
maxLen = Len;
if (Max >= MaxElem)
MaxElem = Max;
}
}
else {
Len = 1;
Max = arr[i];
if (Len >= maxLen) {
maxLen = Len;
if (Max >= MaxElem) {
MaxElem = Max;
}
}
}
}
return MaxElem;
}
int main()
{
int arr[] = { 1, 3, 5, 7, 8, 12, 10 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << maxelementLongestSub(arr, n);
return 0;
}
|
C
#include <stdio.h>
int maxelementLongestSub( int arr[], int n)
{
int MaxElem = arr[0];
int maxLen = 1;
int Len = 1;
int Max = arr[0];
for ( int i = 1; i < n; i++)
{
if (arr[i] % 2 == arr[i - 1] % 2)
{
Len++;
if (arr[i] > Max)
Max = arr[i];
if (Len >= maxLen)
{
maxLen = Len;
if (Max >= MaxElem)
MaxElem = Max;
}
}
else
{
Len = 1;
Max = arr[i];
if (Len >= maxLen)
{
maxLen = Len;
if (Max >= MaxElem)
{
MaxElem = Max;
}
}
}
}
return MaxElem;
}
int main()
{
int arr[] = { 1, 3, 5, 7, 8, 12, 10 };
int n = sizeof (arr) / sizeof (arr[0]);
printf ( "%d" , maxelementLongestSub(arr, n));
return 0;
}
|
Java
import java.io.*;
class GFG{
static int maxelementLongestSub( int arr[], int n)
{
int MaxElem = arr[ 0 ];
int maxLen = 1 ;
int Len = 1 ;
int Max = arr[ 0 ];
for ( int i = 1 ; i < n; i++)
{
if (arr[i] % 2 == arr[i - 1 ] % 2 )
{
Len++;
if (arr[i] > Max)
Max = arr[i];
if (Len >= maxLen)
{
maxLen = Len;
if (Max >= MaxElem)
MaxElem = Max;
}
}
else
{
Len = 1 ;
Max = arr[i];
if (Len >= maxLen)
{
maxLen = Len;
if (Max >= MaxElem)
{
MaxElem = Max;
}
}
}
}
return MaxElem;
}
public static void main(String[] args)
{
int arr[] = { 1 , 3 , 5 , 7 , 8 , 12 , 10 };
int n = arr.length;
System.out.print(maxelementLongestSub(arr, n));
}
}
|
Python3
def maxelementLongestSub(arr, n):
MaxElem = arr[ 0 ]
maxLen = 1
Len = 1
Max = arr[ 0 ]
for i in range ( 1 , n):
if arr[i] % 2 = = arr[i - 1 ] % 2 :
Len + = 1
if arr[i] > Max :
Max = arr[i]
if Len > = maxLen:
maxLen = Len
if Max > = MaxElem:
MaxElem = Max
else :
Len = 1
Max = arr[i]
if Len > = maxLen:
maxLen = Len
if Max > = MaxElem:
MaxElem = Max
return MaxElem
arr = [ 1 , 3 , 5 , 7 , 8 , 12 , 10 ]
n = len (arr)
print (maxelementLongestSub(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int maxelementLongestSub( int [] arr,
int n)
{
int MaxElem = arr[0];
int maxLen = 1;
int Len = 1;
int Max = arr[0];
for ( int i = 1; i < n; i++)
{
if (arr[i] % 2 == arr[i - 1] % 2)
{
Len++;
if (arr[i] > Max)
Max = arr[i];
if (Len >= maxLen)
{
maxLen = Len;
if (Max >= MaxElem)
MaxElem = Max;
}
}
else
{
Len = 1;
Max = arr[i];
if (Len >= maxLen)
{
maxLen = Len;
if (Max >= MaxElem)
{
MaxElem = Max;
}
}
}
}
return MaxElem;
}
public static void Main(String[] args)
{
int [] arr = { 1, 3, 5, 7, 8, 12, 10 };
int n = arr.Length;
Console.Write(maxelementLongestSub(arr, n));
}
}
|
Javascript
<script>
function maxelementLongestSub(arr, n)
{
var MaxElem = arr[0];
var maxLen = 1;
var Len = 1;
var Max = arr[0];
for ( var i = 1; i < n; i++) {
if (arr[i] % 2 == arr[i - 1] % 2) {
Len++;
if (arr[i] > Max) Max = arr[i];
if (Len >= maxLen) {
maxLen = Len;
if (Max >= MaxElem) MaxElem = Max;
}
} else {
Len = 1;
Max = arr[i];
if (Len >= maxLen) {
maxLen = Len;
if (Max >= MaxElem) {
MaxElem = Max;
}
}
}
}
return MaxElem;
}
var arr = [1, 3, 5, 7, 8, 12, 10];
var n = arr.length;
document.write(maxelementLongestSub(arr, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...