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,936 questions

51,873 answers

573 users

How to find all the possible rows which are permutations of a given row in a matrix with C#

2 Answers

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

public class Program
{
	public static void printPermutationRows(int[][] matrix, int givenrow) {
		int rows = matrix.Length;
		int cols = matrix[0].Length;

		HashSet<int> Set = new HashSet<int>();
		for (int j = 0; j < cols; j++) {
			Set.Add(matrix[givenrow][j]);
		}

		for (int i = 0; i < rows; i++) {
			if (i == givenrow) {
				continue;
			}
			int j = 0;
			for (; j < cols; j++) {
				if (!Set.Contains(matrix[i][j])) {
					break;
				}
			}
			if (j != cols) {
				continue;
			}

			Console.Write(i + ", ");
		}
	}
	
	public static void Main(string[] args)
	{
		int givenrow = 1;

		int[][] matrix = new int[][]
		{
			new int[] {7, 9, 4, 3, 1},
			new int[] {4, 7, 9, 1, 3},
			new int[] {4, 7, 8, 1, 2},
			new int[] {1, 6, 9, 7, 4},
			new int[] {9, 1, 3, 7, 4}
		};

		printPermutationRows(matrix, givenrow);
	}
}



/*
run:
 
0, 4,  
   
*/

 



answered Feb 21, 2024 by avibootz
0 votes
using System;
using System.Collections.Generic;

public class Program
{
	public static void printPermutationRows(int[,] matrix, int givenrow) {
		int rows = matrix.GetLength(0);
		int cols = matrix.GetLength(1);

		HashSet<int> Set = new HashSet<int>();
		for (int j = 0; j < cols; j++) {
			Set.Add(matrix[givenrow, j]);
		}

		for (int i = 0; i < rows; i++) {
			if (i == givenrow) {
				continue;
			}
			int j = 0;
			for (; j < cols; j++) {
				if (!Set.Contains(matrix[i, j])) {
					break;
				}
			}
			if (j != cols) {
				continue;
			}

			Console.Write(i + ", ");
		}
	}
	
	public static void Main(string[] args)
	{
		int givenrow = 1;

		int[,] matrix = new int[,]
		{
			{7, 9, 4, 3, 1},
			{4, 7, 9, 1, 3},
			{4, 7, 8, 1, 2},
			{1, 6, 9, 7, 4},
			{9, 1, 3, 7, 4}
		};

		printPermutationRows(matrix, givenrow);
	}
}



/*
run:
 
0, 4,  
   
*/

 



answered Feb 21, 2024 by avibootz
...