Length of the longest alternating subarray - GeeksforGeeks

11 Aug, 2021
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, check if it has an alternate sign.
Increase the cnt by 1 if it has a 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++
Java
Python 3
C#
PHP
Javascript
// C++ program to find the longest alternating// subarray in an array of N number#include <bits/stdc++.h>using namespace std; // Function to find the longest subarrayint longestAlternatingSubarray(int a[], int n){    // Length of longest alternating    int longest = 1;    int cnt = 1;     // Iterate in the array    for (int i = 1; i < n; i++) {         // Check for alternate        if (a[i] * a[i - 1] < 0) {            cnt++;            longest = max(longest, cnt);        }        else            cnt = 1;    }    return longest;} /* Driver program to test above functions*/int main(){    int a[] = { -5, -1, -1, 2, -2, -3 };    int n = sizeof(a) / sizeof(a[0]);     // Function to find the longest subarray    cout << longestAlternatingSubarray(a, n);    return 0;}
// Java program to find the longest alternating// subarray in an array of N numberclass GFG{     // Function to find the longest subarray    static int longestAlternatingSubarray(int a[], int n)    {        // Length of longest alternating        int longest = 1;        int cnt = 1;             // Iterate in the array        for (int i = 1; i < n; i++)        {                 // Check for alternate            if (a[i] * a[i - 1] < 0)            {                cnt++;                longest = Math.max(longest, cnt);            }            else                cnt = 1;        }        return longest;    }         /* Driver program to test above functions*/    public static void main (String[] args)    {        int a[] = { -5, -1, -1, 2, -2, -3 };        int n = a.length ;             // Function to find the longest subarray        System.out.println(longestAlternatingSubarray(a, n));    }} // This code is contributed by Ryuga
# Python3 program to find the longest alternating# subarray in an array of N number # Function to find the longest subarraydef longestAlternatingSubarray(a, n):         # Length of longest alternating    longest = 1    cnt = 1     # Iterate in the array    i = 1    while i < n:         # Check for alternate        if (a[i] * a[i - 1] < 0):            cnt = cnt + 1            longest = max(longest, cnt)                 else:            cnt = 1        i = i + 1         return longest # Driver Codea = [ -5, -1, -1, 2, -2, -3 ]n = len(a) # Function to find the longest subarrayprint(longestAlternatingSubarray(a, n)) # This code is contributed# by shashank_sharma
// C# program to find the longest alternating// subarray in an array of N numberusing System;class GFG{ // Function to find the longest subarraystatic int longestAlternatingSubarray(int []a,                                      int n){    // Length of longest alternating    int longest = 1;    int cnt = 1;     // Iterate in the array    for (int i = 1; i < n; i++)    {         // Check for alternate        if (a[i] * a[i - 1] < 0)        {            cnt++;            longest = Math.Max(longest, cnt);        }        else            cnt = 1;    }    return longest;} // Driver Codepublic static void Main(){    int []a = { -5, -1, -1, 2, -2, -3 };    int n = a.Length;     // Function to find the longest subarray    Console.Write(longestAlternatingSubarray(a, n));}} // This code is contributed// by Akanksha Rai
<?php// PHP program to find the longest alternating// subarray in an array of N number // Function to find the longest subarrayfunction longestAlternatingSubarray($a, $n){         // Length of longest alternating    $longest = 1;    $cnt = 1;     // Iterate in the array    for ($i = 1; $i < $n; $i++)    {         // Check for alternate        if ($a[$i] * $a[$i - 1] < 0)        {            $cnt++;            $longest = max($longest, $cnt);        }        else            $cnt = 1;    }    return $longest;} // Driver Code$a = array(-5, -1, -1, 2, -2, -3 );$n = sizeof($a); // Function to find the longest subarrayecho longestAlternatingSubarray($a, $n); // This code is contributed by akt_mit?>
<script>// JavaScript program to find the longest alternating// subarray in an array of N number // Function to find the longest subarrayfunction longestAlternatingSubarray(a, n){     // Length of longest alternating    let longest = 1;    let cnt = 1;     // Iterate in the array    for (let i = 1; i < n; i++) {         // Check for alternate        if (a[i] * a[i - 1] < 0) {            cnt++;            longest = Math.max(longest, cnt);        }        else            cnt = 1;    }    return longest;} /* Driver program to test above functions*/    let a = [ -5, -1, -1, 2, -2, -3 ];    let n = a.length;     // Function to find the longest subarray    document.write(longestAlternatingSubarray(a, n)); // This code is contributed by Surbhi Tyagi.</script>
3
 
Time Complexity: O(N)Auxiliary Space: O(1)
jit_t
Akanksha_Rai
Shashank_Sharma
ankthon
surbhityagi15
pankajsharmagfg
array-rearrange
Constructive Algorithms
subarray
Algorithms
Arrays
Arrays
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
SDE SHEET - A Complete Guide for SDE Preparation
DSA Sheet by Love Babbar
Introduction to Algorithms
Recursive Practice Problems with Solutions
Quick Sort vs Merge Sort
Arrays in Java
Arrays in C/C++
Program for array rotation
Stack Data Structure (Introduction and Program)
Largest Sum Contiguous Subarray