Given an array arr[] of integers. The task is to return the length of the maximum size sub-array such that either one of the condition is satisfied:
- arr[k] > arr[k + 1] when k is odd and arr[k] < arr[k + 1] when k is even.
- arr[k] > arr[k + 1] when k is even and arr[k] < arr[k + 1] when k is odd.
Examples:
Input: arr[] = {9, 4, 2, 10, 7, 8, 8, 1, 9}
Output: 5
The required sub-array is {4, 2, 10, 7, 8} which satisfies the first condition.Input: arr[] = {4, 8, 12, 16}
Output: 2
Approach: As only comparisons between adjacent elements is required. So if the comparisons are to be represented by -1, 0, 1 then we want the longest sequence of alternating 1, -1, 1, …, -1 (starting with either 1 or -1) where:
- 0 -> arr[i] = arr[i + 1]
- 1 -> arr[i] > arr[i + 1]
- -1 -> arr[i] < arr[i + 1]
For example, if we have an array arr[] = {9, 4, 2, 10, 7, 8, 8, 1, 9} then the comparisons will be {1, 1, -1, 1, -1, 0, -1, 1} and all possible sub-arrays are {1}, {1, -1, 1, -1}, {0}, {-1, 1}.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include<bits/stdc++.h> using namespace std; // Function that compares a and b int cmp(int a, int b) { return (a > b) - (a < b); } // Function to return length of longest subarray // that satisfies one of the given conditions int maxSubarraySize(int arr[], int n) { int ans = 1; int anchor = 0; for (int i = 1; i < n; i++) { int c = cmp(arr[i - 1], arr[i]); if (c == 0) anchor = i; else if (i == n - 1 || c * cmp(arr[i], arr[i + 1]) != -1) { ans = max(ans, i - anchor + 1); anchor = i; } } return ans; } // Driver Code int main() { int arr[] = {9, 4, 2, 10, 7, 8, 8, 1, 9}; int n = sizeof(arr) / sizeof(arr[0]); // Print the required answer cout << maxSubarraySize(arr, n); } // This code is contributed by // Surendra_Gangwar |
Java
// Java implementation of the approach class GFG { // Function that compares a and b static int cmp(int a, int b) { if(a > b) return 1; else if(a == b) return 0; else return -1; } // Function to return length of longest subarray // that satisfies one of the given conditions static int maxSubarraySize(int []arr, int n) { int ans = 1; int anchor = 0; for (int i = 1; i < n; i++) { int c = cmp(arr[i - 1], arr[i]); if (c == 0) anchor = i; else if (i == n - 1 || c * cmp(arr[i], arr[i + 1]) != -1) { ans = Math.max(ans, i - anchor + 1); anchor = i; } } return ans; } // Driver Code public static void main(String[] args) { int []arr = {9, 4, 2, 10, 7, 8, 8, 1, 9}; int n = arr.length; // Print the required answer System.out.println(maxSubarraySize(arr, n)); } } // This code has been contributed by 29AjayKumar |
Python3
# Python3 implementation of the approach # Function that compares a and b def cmp(a, b): return (a > b) - (a < b) # Function to return length of longest subarray # that satisfies one of the given conditions def maxSubarraySize(arr): N = len(arr) ans = 1 anchor = 0 for i in range(1, N): c = cmp(arr[i - 1], arr[i]) if c == 0: anchor = i elif i == N - 1 or c * cmp(arr[i], arr[i + 1]) != -1: ans = max(ans, i - anchor + 1) anchor = i return ans # Driver program arr = [9, 4, 2, 10, 7, 8, 8, 1, 9] # Print the required answer print(maxSubarraySize(arr)) |
C#
// C# implementation of the approach using System; class GFG { // Function that compares a and b static int cmp(int a, int b) { if(a > b) return 1; else if(a == b) return 0; else return -1; } // Function to return length of longest subarray // that satisfies one of the given conditions static int maxSubarraySize(int []arr, int n) { int ans = 1; int anchor = 0; for (int i = 1; i < n; i++) { int c = cmp(arr[i - 1], arr[i]); if (c == 0) anchor = i; else if (i == n - 1 || c * cmp(arr[i], arr[i + 1]) != -1) { ans = Math.Max(ans, i - anchor + 1); anchor = i; } } return ans; } // Driver Code static void Main() { int []arr = {9, 4, 2, 10, 7, 8, 8, 1, 9}; int n = arr.Length; // Print the required answer Console.WriteLine(maxSubarraySize(arr, n)); } } // This code is contributed by mits |
PHP
<?php // PHP implementation of the approach // Function that compares a and b function cmp($a, $b) { return ($a > $b) - ($a < $b); } // Function to return length of longest subarray // that satisfies one of the given conditions function maxSubarraySize($arr) { $N = sizeof($arr); $ans = 1; $anchor = 0; for ($i = 1; $i < $N; $i++) { $c = cmp($arr[$i - 1], $arr[$i]); if ($c == 0) $anchor = $i; else if ($i == $N - 1 or $c * cmp($arr[$i], $arr[$i + 1]) != -1) { $ans = max($ans, $i - $anchor + 1); $anchor = $i; } } return $ans; } // Driver Code $arr = array(9, 4, 2, 10, 7, 8, 8, 1, 9); // Print the required answer echo maxSubarraySize($arr); // This code is contributed by Ryuga ?> |
5
Time Complexity: O(N), where N is the length of array
Space Complexity: 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:
- Subarray permutation that satisfies the given condition
- Smallest index in the given array that satisfies the given condition
- Minimum changes required such that the string satisfies the given condition
- Find the lexicographically smallest string which satisfies the given condition
- Find permutation of first N natural numbers that satisfies the given condition
- Partition the digits of an integer such that it satisfies a given condition
- Generate an array of size K which satisfies the given conditions
- Find an array of size N that satisfies the given conditions
- First subarray having sum at least half the maximum sum of any subarray of size K
- Maximum length sub-array which satisfies the given conditions
- Maximum length sub-array which satisfies the given conditions
- Maximum subarray size, such that all subarrays of that size have sum less than k
- Generate an array B[] from the given array A[] which satisfies the given conditions
- Find an integer in the given range that satisfies the given conditions
- Divide an array into K subarray with the given condition
- Longest sub-sequence that satisfies the given conditions
- Count of operations required to update the array such that it satisfies the given conditions
- Find count of numbers from 0 to n which satisfies the given equation for a value K
- Find the maximum sum (a+b) for a given input integer N satisfying the given condition
- Find the minimum value of m that satisfies ax + by = m and all values after m also satisfy
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.

