Related Articles

Related Articles

Count number of ordered pairs with Even and Odd Product
  • Difficulty Level : Basic
  • Last Updated : 19 Nov, 2018

Given an array of n positive numbers, the task is to count number of ordered pairs with even and odd product. Ordered pairs means (a, b) and (b,a) will be considered as different.

Examples:

Input: n = 3, arr[] = {1, 2, 7}
Output: Even product Pairs = 4, Odd product Pairs = 2
The ordered pairs are (1, 2), (1, 7), (2, 1), (7, 1), (2, 7), (7, 2)
Pairs with Odd product: (1, 7), (7, 1)
Pairs with Even product: (1, 2), (2, 7), (2, 1), (7, 2)

Input: n = 6, arr[] = {2, 4, 5, 9, 1, 8}
Output: Even product Pairs = 24, Odd product Pairs = 6

Approach:



The product of two numbers is odd only if both are numbers are odd. Therefore:

Number of odd product pairs = (count of odd numbers) * (count of odd numbers – 1)

And the number of even product pairs will be an inversion of number of odd product pairs. Therefore:

Number of even product pairs = Total Number of pairs – Number of odd product pairs

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
  
// function to count odd product pair
int count_odd_pair(int n, int a[])
{
    int odd = 0, even = 0;
  
    for (int i = 0; i < n; i++) {
  
        // if number is even
        if (a[i] % 2 == 0)
            even++;
  
        // if number is odd
        else
            odd++;
    }
  
    // count of ordered pairs
    int ans = odd * (odd - 1);
  
    return ans;
}
  
// function to count even product pair
int count_even_pair(int odd_product_pairs, int n)
{
    int total_pairs = (n * (n - 1));
    int ans = total_pairs - odd_product_pairs;
    return ans ;
}
  
// Driver code
int main()
{
  
    int n = 6;
    int a[] = { 2, 4, 5, 9, 1, 8 };
  
    int odd_product_pairs = count_odd_pair(n, a);
  
    int even_product_pairs = count_even_pair(
        odd_product_pairs, n);
  
    cout << "Even Product Pairs = "
         << even_product_pairs
         << endl;
    cout << "Odd Product Pairs= "
         << odd_product_pairs
         << endl;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java  implementation of the above approach 
import java.io.*;
  
class GFG {
      
      
// function to count odd product pair 
static int count_odd_pair(int n, int a[]) 
    int odd = 0, even = 0
  
    for (int i = 0; i < n; i++) { 
  
        // if number is even 
        if (a[i] % 2 == 0
            even++; 
  
        // if number is odd 
        else
            odd++; 
    
  
    // count of ordered pairs 
    int ans = odd * (odd - 1); 
  
    return ans; 
  
// function to count even product pair 
static int count_even_pair(int odd_product_pairs, int n) 
    int total_pairs = (n * (n - 1)); 
    int ans = total_pairs - odd_product_pairs; 
    return ans;
  
// Driver code 
    public static void main (String[] args) {
  
        int n = 6
        int []a = { 2, 4, 5, 9, 1, 8 }; 
  
        int odd_product_pairs = count_odd_pair(n, a); 
  
        int even_product_pairs = count_even_pair( 
            odd_product_pairs, n); 
  
        System.out.println( "Even Product Pairs = "+
            even_product_pairs );
           
        System.out.println("Odd Product Pairs= "+
             odd_product_pairs );
      
    }
}
//This Code is Contributed by ajit

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of 
# above approach
  
# function to count odd product pair
def count_odd_pair(n, a):
    odd = 0
    even = 0
    for i in range(0,n):
          
        # if number is even
        if a[i] % 2==0:
            even=even+1
        # if number is odd
        else:
            odd=odd+1
      
    # count of ordered pairs
    ans = odd * (odd - 1)
    return ans
  
# function to count even product pair
def count_even_pair(odd_product_pairs, n):
    total_pairs = (n * (n - 1))
    ans = total_pairs - odd_product_pairs
    return ans
  
#Driver code
if __name__=='__main__':
    n = 6
    a = [2, 4, 5, 9, 1 ,8]
  
    odd_product_pairs = count_odd_pair(n, a)
    even_product_pairs = (count_even_pair
                       (odd_product_pairs, n))
  
    print("Even Product Pairs = "
          ,even_product_pairs)
    print("Odd Product Pairs= "
          ,odd_product_pairs)
  
# This code is contributed by 
# Shashank_Sharma

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C#  implementation of the above approach
using System;
  
public class GFG{
      
          
// function to count odd product pair 
static int count_odd_pair(int n, int []a) 
    int odd = 0, even = 0; 
  
    for (int i = 0; i < n; i++) { 
  
        // if number is even 
        if (a[i] % 2 == 0) 
            even++; 
  
        // if number is odd 
        else
            odd++; 
    
  
    // count of ordered pairs 
    int ans = odd * (odd - 1); 
  
    return ans; 
  
// function to count even product pair 
static int count_even_pair(int odd_product_pairs, int n) 
    int total_pairs = (n * (n - 1)); 
    int ans = total_pairs - odd_product_pairs; 
    return ans;
  
// Driver code 
      
static public void Main (){
        int n = 6; 
        int []a = { 2, 4, 5, 9, 1, 8 }; 
  
        int odd_product_pairs = count_odd_pair(n, a); 
  
        int even_product_pairs = count_even_pair( 
            odd_product_pairs, n); 
  
        Console.WriteLine( "Even Product Pairs = "+
            even_product_pairs );
          
        Console.WriteLine("Odd Product Pairs= "+
            odd_product_pairs );
    }
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// function to count odd product pair
function count_odd_pair($n, $a)
{
    $odd = 0 ;
    $even = 0 ;
  
    for ($i = 0; $i < $n; $i++) 
    {
  
        // if number is even
        if ($a[$i] % 2 == 0)
            $even++;
  
        // if number is odd
        else
            $odd++;
    }
  
    // count of ordered pairs
    $ans = $odd * ($odd - 1);
  
    return $ans;
}
  
// function to count even product pair
function count_even_pair($odd_product_pairs, $n)
{
    $total_pairs = ($n * ($n - 1));
    $ans = $total_pairs - $odd_product_pairs;
      
    return $ans ;
}
  
// Driver code
$n = 6;
$a = array( 2, 4, 5, 9, 1, 8 );
  
$odd_product_pairs = count_odd_pair($n, $a);
  
$even_product_pairs
      count_even_pair($odd_product_pairs, $n);
  
echo "Even Product Pairs = ",
      $even_product_pairs, "\n";
echo "Odd Product Pairs = "
      $odd_product_pairs, "\n";
  
// This code is contributed
// by ANKITRAI1
?>

chevron_right


Output:

Even Product Pairs = 24
Odd Product Pairs= 6

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :