How to check if product of every adjacent pairs exists in an array with C#

2 Answers

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

public class Program
{
	public static void addToSet(int[] arr, HashSet<int> hset) {
        for (int i = 0; i < arr.Length; i++) {
            hset.Add(arr[i]);
        }
       
    }
	private static bool check_product_of_every_pair(int[] array) {
		HashSet<int> hset = new HashSet<int>();
  
        addToSet(array, hset);

		for (int i = 0; i < array.Length; i += 2) {
			int product = array[i] * array[i + 1];
			Console.WriteLine(array[i] + " * " + array[i + 1] + " = " + product);

			if (!hset.Contains(product)) {
				return false;
			}
		}

		return true;
	}

	public static void Main(string[] args)
	{
		int[] array = new int[] {2, 3, 6, 5, 30, 0};

		Console.Write((check_product_of_every_pair(array) ? "Yes" : "No"));
	}
}






/*
run:
  
2 * 3 = 6
6 * 5 = 30
30 * 0 = 0
Yes
  
*/

 



answered Jul 23, 2023 by avibootz
0 votes
using System;
using System.Linq;

public class Program
{
    private static bool check_product_of_every_pair(int[] array) {
        for (int i = 0; i < array.Length; i += 2) {
            int product = array[i] * array[i + 1];
            Console.WriteLine(array[i] + " * " + array[i + 1] + " = " + product);
 
            if (!array.Contains(product)) {
                return false;
            }
        }
 
        return true;
    }
 
    public static void Main(string[] args)
    {
        int[] array = new int[] {2, 3, 6, 5, 30, 0};
 
        Console.Write((check_product_of_every_pair(array) ? "Yes" : "No"));
    }
}
 
 
 
 
/*
run:
   
2 * 3 = 6
6 * 5 = 30
30 * 0 = 0
Yes
   
*/

 



answered Jul 23, 2023 by avibootz
...