How to count the total pairs in an array in all permutations with even and odd products in C#

1 Answer

0 votes
using System;

internal class Program
{
	internal static int countOddProductPairs(int[] arr) {
		int odd = 0;

		for (int i = 0; i < arr.Length; i++) {
			if (arr[i] % 2 != 0) {
				odd++;
			}
		}

		return odd * (odd - 1);
	}

	internal static int countEvenProductPairs(int total_odd_product_pairs, int[] arr) {
		int total_pairs = arr.Length * (arr.Length - 1);

		return total_pairs - total_odd_product_pairs;
	}

	public static void Main(string[] args)
	{
		int[] arr = new int[] {1, 8, 5};

		// The pairs are: (1, 8), (1, 5), (8, 1), (8, 5), (5, 1), (5, 8) 
		// Pairs with Even product: (1, 8), (8, 1), (8, 5), (5, 8) = 4
		// Total pairs with Odd product: (1, 5), (1, 5) = 2

		int total_odd_product_pairs = countOddProductPairs(arr);
		int total_even_product_pairs = countEvenProductPairs(total_odd_product_pairs, arr);

		Console.WriteLine("Total Even Product Pairs = " + total_even_product_pairs);

		Console.WriteLine("Total Odd Product Pairs= " + total_odd_product_pairs);
	}
}



/*
run:
  
Total Even Product Pairs = 4
Total Odd Product Pairs= 2
  
*/

 



answered Jun 18, 2024 by avibootz
...