Length of the longest alternating subarray
Last Updated :
16 Sep, 2022
Given an array of N including positive and negative numbers only. The task is to find the length of the longest alternating (means negative-positive-negative or positive-negative-positive) subarray present in the array.
Examples:
Input: a[] = {-5, -1, -1, 2, -2, -3}
Output: 3
The subarray {-1, 2, -2}
Input: a[] = {1, -5, 1, -5}
Output: 4
Approach: The following steps are followed to solve the problem:
- Initially initialize cnt as 1.
- Iterate among the array elements, and check if it has an alternate sign.
- Increase the cnt by 1 if it has an alternate sign.
- If it does not has an alternate sign, then re-initialize cnt by 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int longestAlternatingSubarray( int a[], int n)
{
int longest = 1;
int cnt = 1;
for ( int i = 1; i < n; i++) {
if (a[i] * a[i - 1] < 0) {
cnt++;
longest = max(longest, cnt);
}
else
cnt = 1;
}
return longest;
}
int main()
{
int a[] = { -5, -1, -1, 2, -2, -3 };
int n = sizeof (a) / sizeof (a[0]);
cout << longestAlternatingSubarray(a, n);
return 0;
}
|
Java
class GFG
{
static int longestAlternatingSubarray( int a[], int n)
{
int longest = 1 ;
int cnt = 1 ;
for ( int i = 1 ; i < n; i++)
{
if (a[i] * a[i - 1 ] < 0 )
{
cnt++;
longest = Math.max(longest, cnt);
}
else
cnt = 1 ;
}
return longest;
}
public static void main (String[] args)
{
int a[] = { - 5 , - 1 , - 1 , 2 , - 2 , - 3 };
int n = a.length ;
System.out.println(longestAlternatingSubarray(a, n));
}
}
|
Python 3
def longestAlternatingSubarray(a, n):
longest = 1
cnt = 1
i = 1
while i < n:
if (a[i] * a[i - 1 ] < 0 ):
cnt = cnt + 1
longest = max (longest, cnt)
else :
cnt = 1
i = i + 1
return longest
a = [ - 5 , - 1 , - 1 , 2 , - 2 , - 3 ]
n = len (a)
print (longestAlternatingSubarray(a, n))
|
C#
using System;
class GFG
{
static int longestAlternatingSubarray( int []a,
int n)
{
int longest = 1;
int cnt = 1;
for ( int i = 1; i < n; i++)
{
if (a[i] * a[i - 1] < 0)
{
cnt++;
longest = Math.Max(longest, cnt);
}
else
cnt = 1;
}
return longest;
}
public static void Main()
{
int []a = { -5, -1, -1, 2, -2, -3 };
int n = a.Length;
Console.Write(longestAlternatingSubarray(a, n));
}
}
|
PHP
<?php
function longestAlternatingSubarray( $a , $n )
{
$longest = 1;
$cnt = 1;
for ( $i = 1; $i < $n ; $i ++)
{
if ( $a [ $i ] * $a [ $i - 1] < 0)
{
$cnt ++;
$longest = max( $longest , $cnt );
}
else
$cnt = 1;
}
return $longest ;
}
$a = array (-5, -1, -1, 2, -2, -3 );
$n = sizeof( $a );
echo longestAlternatingSubarray( $a , $n );
?>
|
Javascript
<script>
function longestAlternatingSubarray(a, n)
{
let longest = 1;
let cnt = 1;
for (let i = 1; i < n; i++) {
if (a[i] * a[i - 1] < 0) {
cnt++;
longest = Math.max(longest, cnt);
}
else
cnt = 1;
}
return longest;
}
let a = [ -5, -1, -1, 2, -2, -3 ];
let n = a.length;
document.write(longestAlternatingSubarray(a, n));
</script>
|
Complexity Analysis:
- Time Complexity: O(N), as we are using a loop to traverse N times. Where N is the number of elements in the array.
- Auxiliary Space: O(1), as we are not using any extra space.
Share your thoughts in the comments
Please Login to comment...