Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,943 questions

51,884 answers

573 users

How to count the total pairs whose products exist in an array with C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

internal class Program
{
	internal static int countPairsWhoseProductsExistInArray(int[] arr) {
		int total = 0, size = arr.Length;

		HashSet<int> Hash = new HashSet<int>();

		for (int i = 0; i < size; i++) {
			Hash.Add(arr[i]);
		}

		for (int i = 0; i < size; i++) {
			for (int j = i + 1; j < size; j++) {
				int product = arr[i] * arr[j];

				if (Hash.Contains(product)) {
					total++;
				}
			}
		}

		return total;
	}
	public static void Main(string[] args)
	{
		int[] arr = new int[] {2, 8, 5, 16, 6, 3, 7, 30};

		// 2 * 8 = 16
		// 2 * 3 = 6
		// 5 * 6 = 30

		Console.WriteLine("Total = " + countPairsWhoseProductsExistInArray(arr));
	}
}



/*
run:
  
Total = 3
  
*/

 



answered Jun 16, 2024 by avibootz
...