📌  相关文章
📜  范围乘积为正整数的数组中索引对的计数

📅  最后修改于: 2021-06-26 23:01:46             🧑  作者: Mango

给定非零整数数组A ,任务是找到对数(l,r) ,其中(l <= r)使得A [l] * A [l + 1] * A [l + 2 ]….A [r]为正。
例子:

方法:
这个想法是检查每个数组元素可能的数字对。

  • 遍历数组,对数组中的每个元素执行以下步骤。
  • 跟踪在其前面具有偶数个负元素的元素数量(作为even_count)以及在其之前具有奇数个负元素的元素数量(作为odd_count)
  • 到目前为止存储否定元素的总数(作为total_count)
  • 如果total_count是偶数,则将even_count添加到答案中。否则,添加odd_count

下面是上述方法的实现:

C++
// C++ Program to find the
// count of index pairs
// in the array positive
// range product
 
#include 
using namespace std;
 
void positiveProduct(int arr[], int n)
{
    int even_count = 0;
    int odd_count = 0;
    int total_count = 0;
    int ans = 0;
 
    for (int i = 0; i < n; i++) {
 
        // Condition if number of
        // negative elements is even
        // then increase even_count
        if (total_count % 2 == 0)
            even_count++;
 
        // Otherwise increase odd_count
        else
            odd_count++;
 
        // Condition if current element
        // is negative
        if (arr[i] < 0)
            total_count++;
 
        // Condition if number of
        // negative elements is even
        // then add even_count
        // in answer
        if (total_count % 2 == 0)
            ans += even_count;
 
        // Otherwise add odd_count
        // in answer
        else
            ans += odd_count;
    }
 
    cout << ans << "\n";
}
 
// Driver Code
int main()
{
    int A[] = { 5, -3, 3, -1, 1 };
 
    int size = sizeof(A) / sizeof(A[0]);
 
    positiveProduct(A, size);
 
    return 0;
}


Java
// Java program to find the count of
// index pairs in the array positive
// range product
class GFG{
     
public static void positiveProduct(int arr[],
                                   int n)
{
    int even_count = 0;
    int odd_count = 0;
    int total_count = 0;
    int ans = 0;
     
    for(int i = 0; i < n; i++)
    {
         
       // Condition if number of
       // negative elements is even
       // then increase even_count
       if (total_count % 2 == 0)
       {
           even_count++;
       }
        
       // Otherwise increase odd_count
       else
       {
           odd_count++;
       }
        
       // Condition if current element
       // is negative
       if (arr[i] < 0)
       {
           total_count++;
       }
        
       // Condition if number of
       // negative elements is even
       // then add even_count
       // in answer
       if (total_count % 2 == 0)
           ans += even_count;
            
       // Otherwise add odd_count
       // in answer
       else
           ans += odd_count;
    }
    System.out.println(ans);
     
}
 
// Driver Code   
public static void main(String[] args)
{
    int A[] = { 5, -3, 3, -1, 1 };
    int size = A.length;
     
    positiveProduct(A, size);
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program to find the count
# of index pairs in the array
# positive range product
def positiveProduct(arr, n):
 
    even_count = 0
    odd_count = 0
    total_count = 0
    ans = 0
 
    for i in range(n):
 
        # Condition if number of
        # negative elements is even
        # then increase even_count
        if(total_count % 2 == 0):
            even_count += 1
 
        # Otherwise increase odd_count
        else:
            odd_count += 1
 
        # Condition if current element
        # is negative
        if(arr[i] < 0):
            total_count += 1
 
        # Condition if number of
        # negative elements is even
        # then add even_count
        # in answer
        if(total_count % 2 == 0):
            ans += even_count
 
        # Otherwise add odd_count
        # in answer
        else:
            ans += odd_count
 
    print(ans)
 
# Driver Code
if __name__ == '__main__':
     
    A = [ 5, -3, 3, -1, 1 ]
    size = len(A)
 
    positiveProduct(A, size)
 
# This code is contributed by Shivam Singh


C#
// C# program to find the count of
// index pairs in the array positive
// range product
using System;
 
class GFG{
     
public static void positiveProduct(int []arr,
                                   int n)
{
    int even_count = 0;
    int odd_count = 0;
    int total_count = 0;
    int ans = 0;
     
    for(int i = 0; i < n; i++)
    {
         
       // Condition if number of
       // negative elements is even
       // then increase even_count
       if (total_count % 2 == 0)
       {
           even_count++;
       }
        
       // Otherwise increase odd_count
       else
       {
           odd_count++;
       }
        
       // Condition if current element
       // is negative
       if (arr[i] < 0)
       {
           total_count++;
       }
        
       // Condition if number of
       // negative elements is even
       // then add even_count
       // in answer
       if (total_count % 2 == 0)
           ans += even_count;
        
       // Otherwise add odd_count
       // in answer
       else
           ans += odd_count;
    }
    Console.WriteLine(ans);
}
 
// Driver Code
public static void Main(String[] args)
{
    int []A = { 5, -3, 3, -1, 1 };
    int size = A.Length;
     
    positiveProduct(A, size);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
7

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

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。