📌  相关文章
📜  乘积大于或等于 0 的最长子数组的长度

📅  最后修改于: 2021-09-07 02:25:38             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是找到乘积大于或等于 0 的最长子数组的长度。
例子:

方法:

  1. 检查给定数组中所有元素的乘积是否大于或等于零。
  2. 如果是,则乘积大于或等于零的最长子数组长度就是数组长度
  3. 如果上述陈述不成立,则数组包含奇数个负元素。在这种情况下,要找到最长的子数组,请执行以下操作:
    • 对于数组中出现的每个负元素,当前元素左边和右边的子数组给出大于或等于 0 的乘积。因此所需的最长子数组的长度为:
L = max(L, max(i, N - i - 1))
  • 为数组中找到的每个负元素不断更新子数组的长度。
  • L的值是乘积大于等于 0 的最长子数组的长度。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
 
#include 
using namespace std;
 
// Function that count the length
// of longest subarray with product
// greater than or equals to zero
int maxLength(int arr[], int N)
{
    int product = 1, len = 0;
 
    for (int i = 0; i < N; i++) {
        product *= arr[i];
    }
 
    // If product is greater than
    // zero, return array size
    if (product >= 0) {
        return N;
    }
 
    // Traverse the array and if
    // any negative element found
    // then update the length of
    // longest subarray with the
    // length of left and right subarray
    for (int i = 0; i < N; i++) {
        if (arr[i] < 0) {
            len = max(len,
                      max(N - i - 1, i));
        }
    }
 
    return len;
}
 
// Driver Code
int main()
{
    int arr[] = { -1, 1, 1, -2, 3, 2, -1 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << maxLength(arr, N) << endl;
 
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
 
public class GFG{
// Function that count the length
// of longest subarray with product
// greater than or equals to zero
    static int maxLength(int arr[], int N)
    {
        int product = 1, len = 0;
     
        for (int i = 0; i < N; i++) {
            product *= arr[i];
        }
     
        // If product is greater than
        // zero, return array size
        if (product >= 0) {
            return N;
        }
     
        // Traverse the array and if
        // any negative element found
        // then update the length of
        // longest subarray with the
        // length of left and right subarray
        for (int i = 0; i < N; i++) {
            if (arr[i] < 0) {
                len = Math.max(len, Math.max(N - i - 1, i));
            }
        }
     
        return len;
    }
     
    // Driver Code
    public static void main(String args[])
    {
        int arr[] = { -1, 1, 1, -2, 3, 2, -1 };
        int N = arr.length;
        System.out.println(maxLength(arr, N));
     
    }
}
 
// This code is contributed by AbhiThakur


Python3
# Python3 implementation of the above approach
 
# Function that count the Length
# of longest subarray with product
# greater than or equals to zero
def maxLength(arr, N):
    product = 1
    Len = 0
 
    for i in arr:
        product *= i
 
    # If product is greater than
    # zero, return array size
    if (product >= 0):
        return N
 
    # Traverse the array and if
    # any negative element found
    # then update the Length of
    # longest subarray with the
    # Length of left and right subarray
    for i in range(N):
        if (arr[i] < 0):
            Len = max(Len,max(N - i - 1, i))
 
    return Len
 
# Driver Code
if __name__ == '__main__':
    arr = [-1, 1, 1, -2, 3, 2, -1]
    N = len(arr)
 
    print(maxLength(arr, N))
 
# This code is contributed by mohit kumar 29


C#
// C# implementation of the above approach
using System;
 
class GFG{
// Function that count the length
// of longest subarray with product
// greater than or equals to zero
    static int maxLength(int []arr, int N)
    {
        int product = 1, len = 0;
     
        for (int i = 0; i < N; i++) {
            product *= arr[i];
        }
     
        // If product is greater than
        // zero, return array size
        if (product >= 0) {
            return N;
        }
     
        // Traverse the array and if
        // any negative element found
        // then update the length of
        // longest subarray with the
        // length of left and right subarray
        for (int i = 0; i < N; i++) {
            if (arr[i] < 0) {
                len = Math.Max(len, Math.Max(N - i - 1, i));
            }
        }
     
        return len;
    }
     
    // Driver Code
    public static void Main()
    {
        int []arr = { -1, 1, 1, -2, 3, 2, -1 };
        int N = arr.Length;
        Console.WriteLine(maxLength(arr, N));
     
    }
}
 
// This code is contributed by abhaysingh290895


Javascript


输出:
6

时间复杂度: O(N) ,其中 N 是数组的长度。
辅助空间: O(1)