# Longest alternating (positive and negative) subarray starting at every index

A subarray is called alternating if any two consecutive numbers in it have opposite signs (i.e. one of them should be negative, whereas the other should be positive).

Given an array of n integers. For each index i, we need to find the length if the longest alternating subarray starting at i.

Examples:

Input : a[] = {1, -5, 1, -5} Output : For index 0, {1, -5, 1, -5} = 4 index 1, {-5, 1, -5} = 3 index 2, {1, -5} = 2 index 3, {-5} = 1. Input :a[] = {-5, -1, -1, 2, -2, -3} Output : index 0 = 1, index 1 = 1, index 2 = 3, index 3 = 2, index 4 = 1, index 5 = 1,

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.

A **Naive approach** is to use two loops in which we traverse the whole array starting from every index i (0 to n-1) and calculate the length of the alternating subarray.

Time Complexity: O(n^{2}).**Efficient Approach:**

Observe that when *a[i]* and *a[i+1]* have opposite signs, *count[i]* will be 1 more than *count[i+1]*. Otherwise when they have same sign *count[i]* will be 1.

We use Dynamic Programming here.

## C++

`// CPP program to find longest alternating` `// subarray starting from every index.` `#include <bits/stdc++.h>` `using` `namespace` `std;` `void` `longestAlternating(` `int` `arr[], ` `int` `n)` `{` ` ` `int` `count[n];` ` ` `// Fill count[] from end.` ` ` `count[n - 1] = 1;` ` ` `for` `(` `int` `i = n - 2; i >= 0; i--) {` ` ` `if` `(arr[i] * arr[i + 1] < 0)` ` ` `count[i] = count[i + 1] + 1;` ` ` `else` ` ` `count[i] = 1;` ` ` `}` ` ` `// Print result` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `cout << count[i] << ` `" "` `;` `}` `// Driver code` `int` `main()` `{` ` ` `int` `a[] = { -5, -1, -1, 2, -2, -3 };` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]);` ` ` `longestAlternating(a, n);` ` ` `return` `0;` `}` |

## Java

`// Java program to find longest alternating` `// subarray starting from every index.` `import` `java.util.*;` `class` `Longest{` ` ` ` ` `public` `static` `void` `longestAlternating(` `int` `arr[],` ` ` `int` `n)` ` ` `{` ` ` `int` `[] count = ` `new` `int` `[n];` ` ` `// Fill count[] from end.` ` ` `count[n - ` `1` `] = ` `1` `;` ` ` `for` `(` `int` `i = n - ` `2` `; i >= ` `0` `; i--) {` ` ` `if` `(arr[i] * arr[i + ` `1` `] < ` `0` `)` ` ` `count[i] = count[i + ` `1` `] + ` `1` `;` ` ` `else` ` ` `count[i] = ` `1` `;` ` ` `}` ` ` `// Print result` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++)` ` ` `System.out.print(count[i] + ` `" "` `);` ` ` `}` ` ` ` ` `// driver program` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `a[] = { -` `5` `, -` `1` `, -` `1` `, ` `2` `, -` `2` `, -` `3` `};` ` ` `int` `n = ` `6` `;` ` ` `longestAlternating(a, n);` ` ` `}` `}` `// This code is contributed by rishabh_jain` |

## Python3

`# Python3 program to find longest alternating` `# subarray starting from every index.` `def` `longestAlternating(arr, n) :` ` ` `count ` `=` `[` `None` `] ` `*` `n` ` ` `# Fill count[] from end.` ` ` `count[n ` `-` `1` `] ` `=` `1` ` ` `i ` `=` `n ` `-` `2` ` ` ` ` `while` `i >` `=` `0` `:` ` ` `if` `(arr[i] ` `*` `arr[i ` `+` `1` `] < ` `0` `) :` ` ` `count[i] ` `=` `count[i ` `+` `1` `] ` `+` `1` ` ` `else` `:` ` ` `count[i] ` `=` `1` `;` ` ` `i ` `=` `i ` `-` `1` ` ` `i ` `=` `0` ` ` ` ` `# Print result` ` ` `while` `i < n :` ` ` `print` `(count[i], end ` `=` `" "` `)` ` ` `i ` `=` `i ` `+` `1` `# Driver Code` `a ` `=` `[ ` `-` `5` `, ` `-` `1` `, ` `-` `1` `, ` `2` `, ` `-` `2` `, ` `-` `3` `]` `n ` `=` `len` `(a)` `longestAlternating(a, n);` `# This code is contributed by rishabh_jain` |

## C#

`//C# program to find longest alternating` `// subarray starting from every index.` `using` `System;` `class` `Longest` `{` ` ` ` ` `public` `static` `void` `longestAlternating(` `int` `[]arr,` ` ` `int` `n)` ` ` `{` ` ` `int` `[] count = ` `new` `int` `[n];` ` ` `// Fill count[] from end.` ` ` `count[n - 1] = 1;` ` ` `for` `(` `int` `i = n - 2; i >= 0; i--)` ` ` `{` ` ` `if` `(arr[i] * arr[i + 1] < 0)` ` ` `count[i] = count[i + 1] + 1;` ` ` `else` ` ` `count[i] = 1;` ` ` `}` ` ` `// Print result` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `Console.Write(count[i] + ` `" "` `);` ` ` `}` ` ` ` ` `// Driver program` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `int` `[]a = { -5, -1, -1, 2, -2, -3 };` ` ` `int` `n = 6;` ` ` `longestAlternating(a, n);` ` ` `}` `}` `// This code is contributed by vt_m` |

## PHP

`<?php` `// PHP program to find longest alternating` `// subarray starting from every index.` `function` `longestAlternating( ` `$arr` `, ` `$n` `)` `{` ` ` `$count` `= ` `array` `();` ` ` `// Fill count[] from end.` ` ` `$count` `[` `$n` `- 1] = 1;` ` ` `for` `( ` `$i` `= ` `$n` `- 2; ` `$i` `>= 0; ` `$i` `--)` ` ` `{` ` ` `if` `(` `$arr` `[` `$i` `] * ` `$arr` `[` `$i` `+ 1] < 0)` ` ` `$count` `[` `$i` `] = ` `$count` `[` `$i` `+ 1] + 1;` ` ` `else` ` ` `$count` `[` `$i` `] = 1;` ` ` `}` ` ` `// Print result` ` ` `for` `( ` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++)` ` ` `echo` `$count` `[` `$i` `] , ` `" "` `;` `}` ` ` `// Driver code` ` ` `$a` `= ` `array` `( -5, -1, -1, 2, -2, -3 );` ` ` `$n` `=` `count` `(` `$a` `);` ` ` `longestAlternating(` `$a` `, ` `$n` `);` `// This code is contributed by anuj_67.` `?>` |

## Javascript

`<script>` `// JavaScript program to find longest alternating` `// subarray starting from every index.` `function` `longestAlternating(arr, n)` `{` ` ` `let count = ` `new` `Array(n);` ` ` `// Fill count[] from end.` ` ` `count[n - 1] = 1;` ` ` `for` `(let i = n - 2; i >= 0; i--) {` ` ` `if` `(arr[i] * arr[i + 1] < 0)` ` ` `count[i] = count[i + 1] + 1;` ` ` `else` ` ` `count[i] = 1;` ` ` `}` ` ` `// Prlet result` ` ` `for` `(let i = 0; i < n; i++)` ` ` `document.write(count[i] + ` `" "` `);` `}` `// Driver code` ` ` `let a = [ -5, -1, -1, 2, -2, -3 ];` ` ` `let n = a.length;` ` ` `longestAlternating(a, n);` `// This code is contributed by Surbhi Tyagi.` `</script>` |

Output:

1 1 3 2 1 1