Largest element in the longest Subarray consisting of only Even or only Odd numbers

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.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

7

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.