Count of index pairs in array whose range product is a positive integer | C ++ Program to find the count of index pairs in the array positive range product ; Condition if number of negative elements is even then increase even_count ; Otherwise increase odd_count ; Condition if current element is negative ; Condition if number of negative elements is even then add even_count in answer ; Otherwise add odd_count in answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ++ ) { if ( total_count % 2 == 0 ) even_count ++ ; else odd_count ++ ; if ( arr [ i ] < 0 ) total_count ++ ; if ( total_count % 2 == 0 ) ans += even_count ; else ans += odd_count ; } cout << ans << " STRNEWLINE " ; } int main ( ) { int A [ ] = { 5 , -3 , 3 , -1 , 1 } ; int size = sizeof ( A ) / sizeof ( A [ 0 ] ) ; positiveProduct ( A , size ) ; return 0 ; }