Grokking Amazon Coding Interview
Ask Author
Back to course home

0% completed

Vote For New Content
Maximum Length of Subarray With Positive Product (medium)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given an integer array nums, find the maximum length of a subarray where the product of all its elements is greater than 0.

A subarray is a sequence of consecutive elements from the original array.

Examples

  • Example 1:

    • Input: nums = [1, -2, 3, 4]
    • Expected Output: 2
    • Justification: The longest subarray with a positive product is [3, 4], with a length of 2.
  • Example 2:

    • Input: nums = [-1, -2, -3, -4]
    • Expected Output: 4
    • Justification: The entire array produces a positive product since an even number of negative numbers multiply to a positive product. Hence, the longest length is 4.
  • Example 3:

    • Input: nums = [0, -1, 2, -3, 4, -5, 6]
    • Expected Output: 5
    • Justification: The longest subarray with a positive product is [2, -3, 4, -5, 6], with a length of 5.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible