Maximum size of sub-array that satisfies the given condition
Last Updated :
15 Sep, 2022
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++
#include<bits/stdc++.h>
using namespace std;
int cmp( int a, int b)
{
return (a > b) - (a < b);
}
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;
}
int main()
{
int arr[] = {9, 4, 2, 10, 7, 8, 8, 1, 9};
int n = sizeof (arr) / sizeof (arr[0]);
cout << maxSubarraySize(arr, n);
}
|
Java
class GFG
{
static int cmp( int a, int b)
{
if (a > b)
return 1 ;
else if (a == b)
return 0 ;
else
return - 1 ;
}
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;
}
public static void main(String[] args)
{
int []arr = { 9 , 4 , 2 , 10 , 7 , 8 , 8 , 1 , 9 };
int n = arr.length;
System.out.println(maxSubarraySize(arr, n));
}
}
|
Python3
def cmp (a, b):
return (a > b) - (a < b)
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
arr = [ 9 , 4 , 2 , 10 , 7 , 8 , 8 , 1 , 9 ]
print (maxSubarraySize(arr))
|
C#
using System;
class GFG
{
static int cmp( int a, int b)
{
if (a > b)
return 1;
else if (a == b)
return 0;
else
return -1;
}
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;
}
static void Main()
{
int []arr = {9, 4, 2, 10, 7, 8, 8, 1, 9};
int n = arr.Length;
Console.WriteLine(maxSubarraySize(arr, n));
}
}
|
PHP
<?php
function cmp( $a , $b )
{
return ( $a > $b ) - ( $a < $b );
}
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 ;
}
$arr = array (9, 4, 2, 10, 7, 8, 8, 1, 9);
echo maxSubarraySize( $arr );
?>
|
Javascript
<script>
function cmp(a , b) {
if (a > b)
return 1;
else if (a == b)
return 0;
else
return -1;
}
function maxSubarraySize(arr , n) {
var ans = 1;
var anchor = 0;
for (i = 1; i < n; i++) {
var 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;
}
var arr = [ 9, 4, 2, 10, 7, 8, 8, 1, 9 ];
var n = arr.length;
document.write(maxSubarraySize(arr, n));
</script>
|
Complexity Analysis:
- Time Complexity: O(N), where N is the length of the array
- Space Complexity: O(1), since no extra space has been taken.
Share your thoughts in the comments
Please Login to comment...