Length of the longest ZigZag subarray of the given array
Last Updated :
10 Jun, 2022
Given an array arr[] containing n numbers, the task is to find the length of the longest ZigZag subarray such that every element in the subarray should be in form
a < b > c < d > e < f
Examples:
Input: arr[] = {12, 13, 1, 5, 4, 7, 8, 10, 10, 11}
Output: 6
Explanation:
The subarray is {12, 13, 1, 5, 4, 7} whose length is 6 and is in zigzag fashion.
Input: arr[] = {1, 2, 3, 4, 5}
Output: 2
Explanation:
The subarray is {1, 2} or {2, 3} or {4, 5} whose length is 2.
Approach: To solve the problem mentioned above following steps are followed:
- Initially initialize cnt, a counter as 1.
- Iterate among the array elements, check if elements are in form
a < b > c < d > e < f
- If true Increase the cnt by 1. If it is not in form
a < b > c < d > e < f
- then re-initialize cnt by 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int lenOfLongZigZagArr( int a[], int n)
{
int max = 1,
len = 1;
for ( int i = 0; i < n - 1; i++) {
if (i % 2 == 0
&& (a[i] < a[i + 1]))
len++;
else if (i % 2 == 1
&& (a[i] > a[i + 1]))
len++;
else {
if (max < len)
max = len;
len = 1;
}
}
if (max < len)
max = len;
return max;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << lenOfLongZigZagArr(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int lenOfLongZigZagArr( int a[], int n)
{
int max = 1 ,
len = 1 ;
for ( int i = 0 ; i < n - 1 ; i++)
{
if (i % 2 == 0 && (a[i] < a[i + 1 ]))
len++;
else if (i % 2 == 1 && (a[i] > a[i + 1 ]))
len++;
else
{
if (max < len)
max = len;
len = 1 ;
}
}
if (max < len)
max = len;
return max;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int n = arr.length;
System.out.println(lenOfLongZigZagArr(arr, n));
}
}
|
Python3
def lenOfLongZigZagArr(a, n):
_max = 1
_len = 1
for i in range (n - 1 ):
if i % 2 = = 0 and a[i] < a[i + 1 ]:
_len + = 1
elif i % 2 = = 1 and a[i] > a[i + 1 ]:
_len + = 1
else :
if _max < _len:
_max = _len
_len = 1
if _max < _len:
_max = _len
return _max
arr = [ 1 , 2 , 3 , 4 , 5 ]
n = len (arr)
print (lenOfLongZigZagArr(arr, n))
|
C#
using System;
class GFG{
static int lenOflongZigZagArr( int []a, int n)
{
int max = 1,
len = 1;
for ( int i = 0; i < n - 1; i++)
{
if (i % 2 == 0 && (a[i] < a[i + 1]))
len++;
else if (i % 2 == 1 && (a[i] > a[i + 1]))
len++;
else
{
if (max < len)
max = len;
len = 1;
}
}
if (max < len)
max = len;
return max;
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5 };
int n = arr.Length;
Console.WriteLine(lenOflongZigZagArr(arr, n));
}
}
|
Javascript
<script>
function lenOfLongZigZagArr( a, n)
{
var max = 1,
len = 1;
for ( var i = 0; i < n - 1; i++) {
if (i % 2 == 0
&& (a[i] < a[i + 1]))
len++;
else if (i % 2 == 1
&& (a[i] > a[i + 1]))
len++;
else {
if (max < len)
max = len;
len = 1;
}
}
if (max < len)
max = len;
return max;
}
var arr = [ 1, 2, 3, 4, 5 ];
var n = arr.length;
document.write( lenOfLongZigZagArr(arr, n));
</script>
|
Complexity Analysis :
Time Complexity – O(n), where n is the length of the array.
Auxiliary Space – O(1), no extra space required.
Share your thoughts in the comments
Please Login to comment...