Length of the longest alternating increasing decreasing subarray
Last Updated :
22 Aug, 2022
Given an array arr[], the task is to find the length of the longest alternating subarray.
A subarray {x1, x2, .. xn} is an alternating increasing decreasing sequence if its elements satisfy one of the following relations :
x1 < x2 > x3 < x4 > x5 < …. xn or
x1 > x2 < x3 > x4 < x5 > …. xn
Example:
Input: arr = {1, 2, 3, 2, 5, 6}
Output: 4
Explanation: The first elements form index 1 to 4 inclusive are alternating subarray – {2, 3, 2, 5}
Input: arr = {5, 2, 1}
Output: 2
Naive Approach: The naive approach is to check for each subarray if the subarrays starting at every index are alternating. The idea is to use two nested for-loops.
Below is the implementation of the above approach
C++14
#include <bits/stdc++.h>
using namespace std;
int maxAlternatingSubarray( int n, int * arr)
{
int i, j, max = 1, count = 1, a, b;
bool check;
if (n == 1)
return 1;
for (i = 0; i < n; i++) {
count = 1;
for (j = i; j < n - 1; j++) {
a = j;
b = j + 1;
if (j == i) {
if (arr[a] < arr[b])
check = 1;
else if (arr[a] > arr[b])
check = 0;
}
if (check && arr[a] < arr[b]) {
count++;
}
else if (!check && arr[a] > arr[b]) {
count++;
}
else
break ;
check = !check;
}
if (max < count)
max = count;
}
return max;
}
int main()
{
int arr[] = { 1, 2, 3, 2, 5, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << maxAlternatingSubarray(n, arr);
}
|
Java
import java.io.*;
class GFG {
static int maxAlternatingSubarray( int n, int [] arr)
{
int i, j, max = 1 , count = 1 , a, b;
boolean check= false ;
if (n == 1 )
return 1 ;
for (i = 0 ; i < n; i++) {
count = 1 ;
for (j = i; j < n - 1 ; j++) {
a = j;
b = j + 1 ;
if (j == i) {
if (arr[a] < arr[b])
check = true ;
else if (arr[a] > arr[b])
check = false ;
}
if (check== true && arr[a] < arr[b]) {
count++;
}
else if (check== false && arr[a] > arr[b]) {
count++;
}
else
break ;
check = !check;
}
if (max < count)
max = count;
}
return max;
}
public static void main (String[] args)
{
int arr[] = { 1 , 2 , 3 , 2 , 5 , 7 };
int n = arr.length;
System.out.println( maxAlternatingSubarray(n, arr));
}
}
|
Python3
def maxAlternatingSubarray(n, arr):
max = 1
count = 1
a = 0
b = 0
check = 0
if (n = = 1 ):
return 1 ;
for i in range (n):
count = 1 ;
for j in range (i, n - 1 ):
a = j;
b = j + 1 ;
if (j = = i):
if (arr[a] < arr[b]):
check = 1 ;
elif (arr[a] > arr[b]):
check = 0 ;
if (check and arr[a] < arr[b]):
count + = 1
elif ( not check and arr[a] > arr[b]):
count + = 1
else :
break ;
check = not check;
if ( max < count):
max = count;
return max ;
arr = [ 1 , 2 , 3 , 2 , 5 , 7 ];
n = len (arr);
print (maxAlternatingSubarray(n, arr));
|
C#
using System;
class GFG {
static int maxAlternatingSubarray( int n, int []arr)
{
int i, j, max = 1, count = 1, a, b;
bool check= false ;
if (n == 1)
return 1;
for (i = 0; i < n; i++) {
count = 1;
for (j = i; j < n - 1; j++) {
a = j;
b = j + 1;
if (j == i) {
if (arr[a] < arr[b])
check = true ;
else if (arr[a] > arr[b])
check = false ;
}
if (check== true && arr[a] < arr[b]) {
count++;
}
else if (check== false && arr[a] > arr[b]) {
count++;
}
else
break ;
check = !check;
}
if (max < count)
max = count;
}
return max;
}
public static void Main ()
{
int []arr = { 1, 2, 3, 2, 5, 7 };
int n = arr.Length;
Console.Write(maxAlternatingSubarray(n, arr));
}
}
|
Javascript
<script>
function maxAlternatingSubarray(n, arr)
{
let i, j, max = 1, count = 1, a, b;
let check;
if (n == 1)
return 1;
for (i = 0; i < n; i++) {
count = 1;
for (j = i; j < n - 1; j++) {
a = j;
b = j + 1;
if (j == i) {
if (arr[a] < arr[b])
check = 1;
else if (arr[a] > arr[b])
check = 0;
}
if (check && arr[a] < arr[b]) {
count++;
}
else if (!check && arr[a] > arr[b]) {
count++;
}
else
break ;
check = !check;
}
if (max < count)
max = count;
}
return max;
}
let arr = [ 1, 2, 3, 2, 5, 7 ];
let n = arr.length;
document.write(maxAlternatingSubarray(n, arr));
</script>
|
Time Complexity: O(N^2)
Auxiliary Space: O(1)
Approach: The given problem can be optimized by iterating the array and precomputing the difference between consecutive elements. Follow the steps below to solve the problem:
- Iterate the array from index 1 till the end and at every iteration:
- If the current element is greater than the previous element then increment currGreater by 1 and reset currSmaller to 0
- Else if the current element is smaller than the previous element then increment currSmaller by 1 and reset currGreater to 0
- Else if current and previous elements are equal then reset both currGreater and currSmaller to 0
- Swap the values of currGreater and currSmaller
- Update the value of maximum length by comparing it to currGreater and currSmaller
- Return the value of maximum length calculated
C++14
#include <bits/stdc++.h>
using namespace std;
int longestAltSubarray( int N, int * arr)
{
if (N == 1)
return 1;
int maxLen = 0;
int currGreater = 0, currSmaller = 0;
for ( int i = 1; i < N; i++) {
if (arr[i] > arr[i - 1]) {
currGreater++;
currSmaller = 0;
}
else if (arr[i] < arr[i - 1]) {
currSmaller++;
currGreater = 0;
}
else {
currGreater = 0;
currSmaller = 0;
}
int temp = currGreater;
currGreater = currSmaller;
currSmaller = temp;
maxLen = max(maxLen,
max(currGreater, currSmaller));
}
return maxLen + 1;
}
int main()
{
int arr[] = { 1, 2, 3, 2, 5, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << longestAltSubarray(n, arr);
}
|
Java
import java.util.*;
public class GFG
{
static int longestAltSubarray( int N, int []arr)
{
if (N == 1 )
return 1 ;
int maxLen = 0 ;
int currGreater = 0 , currSmaller = 0 ;
for ( int i = 1 ; i < N; i++) {
if (arr[i] > arr[i - 1 ]) {
currGreater++;
currSmaller = 0 ;
}
else if (arr[i] < arr[i - 1 ]) {
currSmaller++;
currGreater = 0 ;
}
else {
currGreater = 0 ;
currSmaller = 0 ;
}
int temp = currGreater;
currGreater = currSmaller;
currSmaller = temp;
maxLen = Math.max(maxLen,
Math.max(currGreater, currSmaller));
}
return maxLen + 1 ;
}
public static void main(String args[])
{
int []arr = { 1 , 2 , 3 , 2 , 5 , 7 };
int n = arr.length;
System.out.println(longestAltSubarray(n, arr));
}
}
|
Python3
from builtins import range
def longestAltSubarray(N, arr):
if (N = = 1 ):
return 1
maxLen = 0
currGreater = 0
currSmaller = 0
for i in range ( 1 , N, 1 ):
if (arr[i] > arr[i - 1 ]):
currGreater + = 1
currSmaller = 0
elif (arr[i] < arr[i - 1 ]):
currSmaller + = 1
currGreater = 0
else :
currGreater = 0
currSmaller = 0
temp = currGreater
currGreater = currSmaller
currSmaller = temp
maxLen = max (maxLen, max (currGreater,
currSmaller))
return maxLen + 1
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 2 , 5 , 7 ]
n = len (arr)
print (longestAltSubarray(n, arr))
|
C#
using System;
class GFG
{
static int longestAltSubarray( int N, int []arr)
{
if (N == 1)
return 1;
int maxLen = 0;
int currGreater = 0, currSmaller = 0;
for ( int i = 1; i < N; i++) {
if (arr[i] > arr[i - 1]) {
currGreater++;
currSmaller = 0;
}
else if (arr[i] < arr[i - 1]) {
currSmaller++;
currGreater = 0;
}
else {
currGreater = 0;
currSmaller = 0;
}
int temp = currGreater;
currGreater = currSmaller;
currSmaller = temp;
maxLen = Math.Max(maxLen,
Math.Max(currGreater, currSmaller));
}
return maxLen + 1;
}
public static void Main()
{
int []arr = { 1, 2, 3, 2, 5, 7 };
int n = arr.Length;
Console.Write(longestAltSubarray(n, arr));
}
}
|
Javascript
<script>
function longestAltSubarray(N, arr) {
if (N == 1)
return 1;
let maxLen = 0;
let currGreater = 0, currSmaller = 0;
for (let i = 1; i < N; i++) {
if (arr[i] > arr[i - 1]) {
currGreater++;
currSmaller = 0;
}
else if (arr[i] < arr[i - 1]) {
currSmaller++;
currGreater = 0;
}
else {
currGreater = 0;
currSmaller = 0;
}
let temp = currGreater;
currGreater = currSmaller;
currSmaller = temp;
maxLen = Math.max(maxLen,
Math.max(currGreater, currSmaller));
}
return maxLen + 1;
}
let arr = [1, 2, 3, 2, 5, 7];
let n = arr.length
document.write(longestAltSubarray(n, arr))
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...