📜  阳性乘积的最长子阵列的长度

📅  最后修改于: 2021-05-04 17:12:57             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是打印带有正积的最长子数组的长度。

例子:

天真的方法:解决问题的最简单方法是生成所有可能的子数组,并检查其乘积是否为正。在所有此类子阵列中,打印获得的最长子阵列的长度。
时间复杂度: (N 3 )
辅助空间: O(1)
高效方法:可以使用动态编程解决问题。这里的想法是保持正元素和负元素的数量,以使它们的乘积为正。请按照以下步骤解决问题:

  1. 初始化变量,例如res ,以存储带正积的最长子数组的长度。
  2. 初始化两个变量PosNeg ,以分别存储具有正和负乘积的当前子数组的长度。
  3. 遍历数组。
  4. 如果arr [i] = 0:重置PosNeg的值。
  5. 如果arr [i]> 0:将Pos递增1。如果子数组中至少存在一个元素为负数,则将Neg递增1。
  6. 如果arr [i] <0:交换PosNeg,然后将Neg递增1。如果子数组中至少有一个元素与正乘积一起存在,则Pos也将递增。
  7. 更新res = max(res,Pos)。
C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the length of
// longest subarray whose product
// is positive
int maxLenSub(int arr[], int N)
{
    // Stores the length of current
    // subarray with positive product
    int Pos = 0;
 
    // Stores the length of current
    // subarray with negative product
    int Neg = 0;
 
    // Stores the length of the longest
    // subarray with positive product
    int res = 0;
 
    for (int i = 0; i < N; i++) {
 
        if (arr[i] == 0) {
 
            // Reset the value
            Pos = Neg = 0;
        }
 
        // If current element is positive
        else if (arr[i] > 0) {
 
            // Increment the length of
            // subarray with positive product
            Pos += 1;
 
            // If at least one element is
            // present in the subarray with
            // negative product
            if (Neg != 0) {
 
                Neg += 1;
            }
 
            // Update res
            res = max(res, Pos);
        }
 
        // If current element is negative
        else {
 
            swap(Pos, Neg);
 
            // Increment the length of subarray
            // with negative product
            Neg += 1;
 
            // If at least one element is present
            // in the subarray with positive product
            if (Pos != 0) {
 
                Pos += 1;
            }
 
            // Update res
            res = max(res, Pos);
        }
    }
    return res;
}
 
// Driver Code
int main()
{
    int arr[] = { -1, -2, -3, 0, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << maxLenSub(arr, N);
}


Python3
# Python3 program to implement
# the above approach
 
# Function to find the length of
# longest subarray whose product
# is positive
def maxLenSub(arr, N):
     
    # Stores the length of current
    # subarray with positive product
    Pos = 0
 
    # Stores the length of current
    # subarray with negative product
    Neg = 0
 
    # Stores the length of the longest
    # subarray with positive product
    res = 0
 
    for i in range(N):
        if (arr[i] == 0):
 
            # Reset the value
            Pos = Neg = 0
 
        # If current element is positive
        elif (arr[i] > 0):
 
            # Increment the length of
            # subarray with positive product
            Pos += 1
 
            # If at least one element is
            # present in the subarray with
            # negative product
            if (Neg != 0):
                Neg += 1
 
            # Update res
            res = max(res, Pos)
 
        # If current element is negative
        else:
            Pos, Neg = Neg, Pos
 
            # Increment the length of subarray
            # with negative product
            Neg += 1
 
            # If at least one element is present
            # in the subarray with positive product
            if (Pos != 0):
                Pos += 1
 
            # Update res
            res = max(res, Pos)
             
    return res
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ -1, -2, -3, 0, 1 ]
    N = len(arr)
     
    print(maxLenSub(arr, N))
 
# This code is contributed by mohit kumar 29


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
 
// Function to find the length of
// longest subarray whose product
// is positive
static int maxLenSub(int arr[], int N)
{
    // Stores the length of current
    // subarray with positive product
    int Pos = 0;
 
    // Stores the length of current
    // subarray with negative product
    int Neg = 0;
 
    // Stores the length of the longest
    // subarray with positive product
    int res = 0;
 
    for (int i = 0; i < N; i++)
    {
        if (arr[i] == 0)
        {
            // Reset the value
            Pos = Neg = 0;
        }
 
        // If current element is positive
        else if (arr[i] > 0)
        {
            // Increment the length of
            // subarray with positive product
            Pos += 1;
 
            // If at least one element is
            // present in the subarray with
            // negative product
            if (Neg != 0)
            {
                Neg += 1;
            }
 
            // Update res
            res = Math.max(res, Pos);
        }
 
        // If current element is negative
        else
        {
            Pos = Pos + Neg;
            Neg = Pos - Neg;
            Neg = Pos - Neg;
 
            // Increment the length of subarray
            // with negative product
            Neg += 1;
 
            // If at least one element is present
            // in the subarray with positive product
            if (Pos != 0)
            {
                Pos += 1;
            }
 
            // Update res
            res = Math.max(res, Pos);
        }
    }
    return res;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = {-1, -2, -3, 0, 1};
    int N = arr.length;
    System.out.print(maxLenSub(arr, N));
}
}
 
// This code is contributed by Rajput-Ji


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to find the length of
// longest subarray whose product
// is positive
static int maxLenSub(int[] arr, int N)
{
    // Stores the length of current
    // subarray with positive product
    int Pos = 0;
 
    // Stores the length of current
    // subarray with negative product
    int Neg = 0;
 
    // Stores the length of the longest
    // subarray with positive product
    int res = 0;
 
    for (int i = 0; i < N; i++)
    {
        if (arr[i] == 0)
        {
            // Reset the value
            Pos = Neg = 0;
        }
 
        // If current element is positive
        else if (arr[i] > 0)
        {
            // Increment the length of
            // subarray with positive product
            Pos += 1;
 
            // If at least one element is
            // present in the subarray with
            // negative product
            if (Neg != 0)
            {
                Neg += 1;
            }
 
            // Update res
            res = Math.Max(res, Pos);
        }
 
        // If current element is negative
        else
        {
            Pos = Pos + Neg;
            Neg = Pos - Neg;
            Neg = Pos - Neg;
 
            // Increment the length of subarray
            // with negative product
            Neg += 1;
 
            // If at least one element is present
            // in the subarray with positive product
            if (Pos != 0)
            {
                Pos += 1;
            }
 
            // Update res
            res = Math.Max(res, Pos);
        }
    }
    return res;
}
 
// Driver Code
public static void Main()
{
    int[] arr = {-1, -2, -3, 0, 1};
    int N = arr.Length;
    Console.Write(maxLenSub(arr, N));
}
}
 
// This code is contributed by Chitranayal


输出:
2




时间复杂度: O(N)
辅助空间: O(1)