Skip to content
Related Articles

Related Articles

Largest element in the longest Subarray consisting of only Even or only Odd numbers
  • Last Updated : 13 Apr, 2021

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:
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.

 

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++




// C++ program to implement
// the above approach
 
#include <iostream>
using namespace std;
 
// Function to find the largest element
// of the longest subarray consisting
// only of odd or even elements only
int maxelementLongestSub(int arr[], int n)
{
    // Stores largest element of the
    // longest subarray till i-th index
    int MaxElem = arr[0];
 
    // Stores maximum length of the
    // longest subarray till i-th index
    int maxLen = 1;
 
    // Stores length of the current
    // subarray including the i-th element
    int Len = 1;
 
    // Stores largest element in
    // current subarray
    int Max = arr[0];
 
    // Traverse the array
    for (int i = 1; i < n; i++) {
 
        // If arr[i] and arr[i - 1]
        // are either even numbers
        // or odd numbers
        if (arr[i] % 2 == arr[i - 1] % 2) {
 
            // Update Len
            Len++;
 
            // Update Max
            if (arr[i] > Max)
                Max = arr[i];
 
            // If Len greater than
            // maxLen
            if (Len >= maxLen) {
                maxLen = Len;
 
                // Update MaxElem
                if (Max >= MaxElem)
                    MaxElem = Max;
            }
        }
 
        else {
 
            // Update Len
            Len = 1;
 
            // Update Max
            Max = arr[i];
 
            // If Len greater
            // than maxLen
            if (Len >= maxLen) {
 
                // Update maxLen
                maxLen = Len;
 
                // If Max greater
                // than MaxElem
                if (Max >= MaxElem) {
 
                    // Update MaxElem
                    MaxElem = Max;
                }
            }
        }
    }
 
    return MaxElem;
}
 
// Driver Code
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




// C program to implement
// the above approach
#include <stdio.h>
 
// Function to find the largest element
// of the longest subarray consisting
// only of odd or even elements only
int maxelementLongestSub(int arr[], int n)
{
     
    // Stores largest element of the
    // longest subarray till i-th index
    int MaxElem = arr[0];
 
    // Stores maximum length of the
    // longest subarray till i-th index
    int maxLen = 1;
 
    // Stores length of the current
    // subarray including the i-th element
    int Len = 1;
 
    // Stores largest element in
    // current subarray
    int Max = arr[0];
 
    // Traverse the array
    for(int i = 1; i < n; i++)
    {
         
        // If arr[i] and arr[i - 1]
        // are either even numbers
        // or odd numbers
        if (arr[i] % 2 == arr[i - 1] % 2)
        {
             
            // Update Len
            Len++;
 
            // Update Max
            if (arr[i] > Max)
                Max = arr[i];
 
            // If Len greater than
            // maxLen
            if (Len >= maxLen)
            {
                maxLen = Len;
 
                // Update MaxElem
                if (Max >= MaxElem)
                    MaxElem = Max;
            }
        }
 
        else
        {
             
            // Update Len
            Len = 1;
 
            // Update Max
            Max = arr[i];
 
            // If Len greater
            // than maxLen
            if (Len >= maxLen)
            {
                 
                // Update maxLen
                maxLen = Len;
 
                // If Max greater
                // than MaxElem
                if (Max >= MaxElem)
                {
                     
                    // Update MaxElem
                    MaxElem = Max;
                }
            }
        }
    }
    return MaxElem;
}
 
// Driver Code
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;
}
 
// This code is contributed by sourav singh

Java




// Java program to implement
// the above approach
import java.io.*;
 
class GFG{
 
// Function to find the largest element
// of the longest subarray consisting
// only of odd or even elements only    
static int maxelementLongestSub(int arr[], int n)
{
     
    // Stores largest element of the
    // longest subarray till i-th index
    int MaxElem = arr[0];
 
    // Stores maximum length of the
    // longest subarray till i-th index
    int maxLen = 1;
 
    // Stores length of the current
    // subarray including the i-th element
    int Len = 1;
 
    // Stores largest element in
    // current subarray
    int Max = arr[0];
 
    // Traverse the array
    for(int i = 1; i < n; i++)
    {
         
        // If arr[i] and arr[i - 1]
        // are either even numbers
        // or odd numbers
        if (arr[i] % 2 == arr[i - 1] % 2)
        {
             
            // Update Len
            Len++;
 
            // Update Max
            if (arr[i] > Max)
                Max = arr[i];
 
            // If Len greater than
            // maxLen
            if (Len >= maxLen)
            {
                maxLen = Len;
                 
                // Update MaxElem
                if (Max >= MaxElem)
                    MaxElem = Max;
            }
        }
        else
        {
             
            // Update Len
            Len = 1;
 
            // Update Max
            Max = arr[i];
 
            // If Len greater
            // than maxLen
            if (Len >= maxLen)
            {
                 
                // Update maxLen
                maxLen = Len;
 
                // If Max greater
                // than MaxElem
                if (Max >= MaxElem)
                {
                     
                    // Update MaxElem
                    MaxElem = Max;
                }
            }
        }
    }
    return MaxElem;
}
 
// Driver Code
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));
}
}
 
// This code is contributed by sourav singh

Python3




# Python3 program to implement
# the above approach
 
# Function to find the largest element
# of the longest subarray consisting
# only of odd or even elements only
def maxelementLongestSub(arr, n):
     
    # Stores largest element of the
    # longest sub-array till i-th index
    MaxElem = arr[0]
 
    # Stores maximum length of the
    # longest sub-array till i-th index
    maxLen = 1
 
    # Stores length of the current
    # sub-array including the i-th element
    Len = 1
 
    # Stores largest element in
    # current sub-array
    Max = arr[0]
 
    for i in range(1, n):
         
        # If arr[i] and arr[i - 1]
        # are either even numbers
        # or odd numbers
        if arr[i] % 2 == arr[i - 1] % 2:
             
            # Update Len
            Len += 1
             
            # Update Max
            if arr[i] > Max:
                Max = arr[i]
                 
            # If Len greater than
            # maxLen
            if Len >= maxLen:
                maxLen = Len
                 
                # Update MaxElem
                if Max >= MaxElem:
                    MaxElem = Max
        else:
             
            # Update Len
            Len = 1
             
            # Update Max
            Max = arr[i]
             
            # If Len greater
            # than maxLen
            if Len >= maxLen:
                maxLen = Len
                 
                # If Max greater
                #   than MaxElem
                if Max >= MaxElem:
                    MaxElem = Max
                     
    return MaxElem
 
# Driver Code
arr = [ 1, 3, 5, 7, 8, 12, 10 ]
n = len(arr)
 
print(maxelementLongestSub(arr, n))
 
# This code is contributed by sourav singh

C#




// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the largest element
// of the longest subarray consisting
// only of odd or even elements only    
static int maxelementLongestSub(int[] arr,
                                int n)
{
     
    // Stores largest element of the
    // longest subarray till i-th index
    int MaxElem = arr[0];
 
    // Stores maximum length of the
    // longest subarray till i-th index
    int maxLen = 1;
 
    // Stores length of the current
    // subarray including the i-th element
    int Len = 1;
 
    // Stores largest element in
    // current subarray
    int Max = arr[0];
 
    // Traverse the array
    for(int i = 1; i < n; i++)
    {
         
        // If arr[i] and arr[i - 1]
        // are either even numbers
        // or odd numbers
        if (arr[i] % 2 == arr[i - 1] % 2)
        {
             
            // Update Len
            Len++;
 
            // Update Max
            if (arr[i] > Max)
                Max = arr[i];
 
            // If Len greater than
            // maxLen
            if (Len >= maxLen)
            {
                maxLen = Len;
                 
                // Update MaxElem
                if (Max >= MaxElem)
                    MaxElem = Max;
            }
        }
        else
        {
             
            // Update Len
            Len = 1;
 
            // Update Max
            Max = arr[i];
 
            // If Len greater
            // than maxLen
            if (Len >= maxLen)
            {
                 
                // Update maxLen
                maxLen = Len;
 
                // If Max greater
                // than MaxElem
                if (Max >= MaxElem)
                {
                     
                    // Update MaxElem
                    MaxElem = Max;
                }
            }
        }
    }
    return MaxElem;
}
 
//  Driver Code
public static void Main(String[] args)
{
    int[] arr = { 1, 3, 5, 7, 8, 12, 10 };
    int n = arr.Length;
 
    // Function call
    Console.Write(maxelementLongestSub(arr, n));
}
}
 
// This code is contributed by sourav singh

Javascript




<script>
 
      // JavaScript program to implement
      // the above approach
 
      // Function to find the largest element
      // of the longest subarray consisting
      // only of odd or even elements only
      function maxelementLongestSub(arr, n)
      {
        // Stores largest element of the
        // longest subarray till i-th index
        var MaxElem = arr[0];
 
        // Stores maximum length of the
        // longest subarray till i-th index
        var maxLen = 1;
 
        // Stores length of the current
        // subarray including the i-th element
        var Len = 1;
 
        // Stores largest element in
        // current subarray
        var Max = arr[0];
 
        // Traverse the array
        for (var i = 1; i < n; i++) {
          // If arr[i] and arr[i - 1]
          // are either even numbers
          // or odd numbers
          if (arr[i] % 2 == arr[i - 1] % 2) {
            // Update Len
            Len++;
 
            // Update Max
            if (arr[i] > Max) Max = arr[i];
 
            // If Len greater than
            // maxLen
            if (Len >= maxLen) {
              maxLen = Len;
 
              // Update MaxElem
              if (Max >= MaxElem) MaxElem = Max;
            }
          } else {
            // Update Len
            Len = 1;
 
            // Update Max
            Max = arr[i];
 
            // If Len greater
            // than maxLen
            if (Len >= maxLen) {
              // Update maxLen
              maxLen = Len;
 
              // If Max greater
              // than MaxElem
              if (Max >= MaxElem) {
                // Update MaxElem
                MaxElem = Max;
              }
            }
          }
        }
 
        return MaxElem;
      }
 
      // Driver Code
      var arr = [1, 3, 5, 7, 8, 12, 10];
      var n = arr.length;
 
      document.write(maxelementLongestSub(arr, n));
       
</script>
Output: 
7

 

Time Complexity: O(N) 
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :