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++
// 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; } |
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.
Recommended Posts:
- Even numbers at even index and odd numbers at odd index
- Count numbers from given range having odd digits at odd places and even digits at even places
- Count subarrays consisting of only 0's and only 1's in a binary array
- Length of longest Subarray with equal number of odd and even elements
- Length of the longest alternating even odd subarray
- Length of the longest Subarray with only Even Elements
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Check if a number has an odd count of odd divisors and even count of even divisors
- Maximum length of subarray consisting of same type of element on both halves of sub-array
- Count of subarrays consisting of only prime numbers
- Length of longest Fibonacci subarray formed by removing only one element
- Length of longest subarray having only K distinct Prime Numbers
- Largest Even and Odd N-digit numbers
- Largest Even and Odd N-digit numbers in Octal Number System
- Largest Even and Odd N-digit numbers in Hexadecimal Number System
- Largest Even and Odd N-digit numbers of base B
- Longest Subarray with first element greater than or equal to Last element
- Length of largest subsequence consisting of a pair of alternating digits
- Lexicographically largest string possible consisting of at most K consecutive similar characters
- Count of substrings consisting of even number of vowels
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.

