📌  相关文章
📜  具有正积的最长子阵列的长度

📅  最后修改于: 2021-09-22 09:50:33             🧑  作者: 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:交换名次NEG以及由1递增负片如果至少一个元素存在于与正产品的子阵列,则增量名次也。
  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


Javascript


输出:
2

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程