给定一个由N 个整数组成的数组arr[] ,任务是用正积打印最长子数组的长度。
例子:
Input: arr[] ={0, 1, -2, -3, -4}
Output: 3
Explanation:
The longest subarray with positive products is: {1, -2, -3}. Therefore, the required length is 3.
Input: arr[]={-1, -2, 0, 1, 2}
Output: 2
Explanation:
The longest subarray with positive products are: {-1, -2}, {1, 2}. Therefore, the required length is 2.
朴素方法:解决问题的最简单方法是生成所有可能的子数组并检查其乘积是否为正。在所有这些子数组中,打印获得的最长子数组的长度。
时间复杂度: (N 3 )
辅助空间: O(1)
有效的方法:该问题可以使用动态规划解决。这里的想法是保持正元素和负元素的数量,使它们的乘积为正。请按照以下步骤解决问题:
- 初始化变量,例如res ,以存储具有正积的最长子数组的长度。
- 初始化两个变量Pos和Neg ,分别存储当前子数组的正积和负积的长度。
- 遍历数组。
- 如果 arr[i] = 0:重置Pos和Neg的值。
- 如果 arr[i] > 0:将Pos增加 1。如果子数组中至少存在一个具有负乘积的元素,则将Neg增加 1。
- 如果ARR [I] <0:交换名次和NEG以及由1递增负片如果至少一个元素存在于与正产品的子阵列,则增量名次也。
- 更新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 现场工作专业课程和学生竞争性编程现场课程。