📜  具有负积的最长子数组的长度

📅  最后修改于: 2021-09-07 03:28:27             🧑  作者: Mango

给定一个包含N 个元素的数组arr[] 。任务是找到最长子数组的长度,使得子数组的乘积为负。如果没有这样的子数组可用,则打印 -1。
例子:

方法:

  • 首先,检查数组的总乘积是否为负。如果数组的总积为负,则答案为 N。
  • 如果数组的总乘积不为负,则表示它为正。因此,我们的想法是从数组中找到一个负元素,这样排除该元素并比较数组的两个部分的长度,我们可以获得子数组与负乘积的最大长度。
  • 很明显——

以下是上述方法的实现-

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to find length of the
// longest subarray such that product
// of the subarray is negative
int maxLength(int a[], int n)
{
    int product = 1, len = -1;
 
    // Check if product of complete
    // array is negative
    for (int i = 0; i < n; i++)
        product *= a[i];
 
    // Total product is already
    // negative
    if (product < 0)
        return n;
 
    // Find an index i such the a[i]
    // is negative and compare length
    // of both halfs excluding a[i] to
    // find max length subarray
    for (int i = 0; i < n; i++) {
        if (a[i] < 0)
            len = max(len,
                      max(n - i - 1, i));
    }
 
    return len;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, -3, 2, 5, -6 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << maxLength(arr, N)
         << "\n";
 
    int arr1[] = { 1, 2, 3, 4 };
    N = sizeof(arr1) / sizeof(arr1[0]);
 
    cout << maxLength(arr1, N)
         << "\n";
 
    return 0;
}


Java
// Java implementation of the above approach
import java.util.Arrays;
 
class GFG{
     
// Function to find length of the
// longest subarray such that product
// of the subarray is negative
static int maxLength(int a[], int n)
{
    int product = 1, len = -1;
 
    // Check if product of complete
    // array is negative
    for(int i = 0; i < n; i++)
        product *= a[i];
 
    // Total product is already
    // negative
    if (product < 0)
        return n;
 
    // Find an index i such the a[i]
    // is negative and compare length
    // of both halfs excluding a[i] to
    // find max length subarray
    for(int i = 0; i < n; i++)
    {
        if (a[i] < 0)
            len = Math.max(len,
                  Math.max(n - i - 1, i));
    }
    return len;
}
     
// Driver code
public static void main (String[] args)
{
     
    // Given array arr[]
    int arr[] = new int[]{ 1, 2, -3,
                           2, 5, -6 };
    int N = arr.length;
     
    System.out.println(maxLength(arr, N));
     
    // Given array arr[]
    int arr1[] = new int[]{ 1, 2, 3, 4 };
    N = arr1.length;
 
    System.out.println(maxLength(arr1, N));
}
}
 
// This code is contributed by Pratima Pandey


Python3
# Python3 implementation of the above approach
# Function to find length of the
# longest subarray such that product
# of the subarray is negative
def maxLength(a, n):
    product = 1
    length = -1
 
    # Check if product of complete
    # array is negative
    for i in range (n):
        product *= a[i]
 
    # Total product is already
    # negative
    if (product < 0):
        return n
 
    # Find an index i such the a[i]
    # is negative and compare length
    # of both halfs excluding a[i] to
    # find max length subarray
    for i in range (n):
        if (a[i] < 0):
            length = max(length,
                         max(n - i - 1, i))
 
    return length
 
# Driver Code
if __name__ == "__main__": 
    arr = [1, 2, -3, 2, 5, -6]
    N = len(arr)
    print (maxLength(arr, N))
    arr1 = [1, 2, 3, 4]
    N = len(arr1)
    print (maxLength(arr1, N))
          
# This code is contributed by Chitranayal


C#
// C# implementation of the above approach
using System;
 
class GFG{
     
// Function to find length of the
// longest subarray such that product
// of the subarray is negative
static int maxLength(int []a, int n)
{
    int product = 1, len = -1;
 
    // Check if product of complete
    // array is negative
    for(int i = 0; i < n; i++)
        product *= a[i];
 
    // Total product is already
    // negative
    if (product < 0)
        return n;
 
    // Find an index i such the a[i]
    // is negative and compare length
    // of both halfs excluding a[i] to
    // find max length subarray
    for(int i = 0; i < n; i++)
    {
        if (a[i] < 0)
            len = Math.Max(len,
                  Math.Max(n - i - 1, i));
    }
    return len;
}
     
// Driver code
public static void Main(String[] args)
{
     
    // Given array []arr
    int []arr = new int[]{ 1, 2, -3,
                           2, 5, -6 };
    int N = arr.Length;
     
    Console.WriteLine(maxLength(arr, N));
     
    // Given array []arr
    int []arr1 = new int[]{ 1, 2, 3, 4 };
    N = arr1.Length;
 
    Console.WriteLine(maxLength(arr1, N));
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
5
-1

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live