using System; using System.Linq; class VariationsWithoutRepetition { const int k = 2; const int n = 4; static int[] arr = new int[k]; static int[] free = Enumerable.Range(1, 4).ToArray(); static void Main() { GenerateVariationsNoRepetitions(0); } static void GenerateVariationsNoRepetitions(int index) { if (index >= k) { PrintVariations(); } else { for (int i = index; i < n; i++) { arr[index] = free[i]; Swap(ref free[i], ref free[index]); GenerateVariationsNoRepetitions(index + 1); Swap(ref free[i], ref free[index]); } } } private static void Swap<T>(ref T v1, ref T v2) { T old = v1; v1 = v2; v2 = old; } static void PrintVariations() { Console.WriteLine("(" + string.Join(", ", arr) + ")"); } } // result: // (1, 2) // (1, 3) // (1, 4) // (2, 1) // (2, 3) // (2, 4) // (3, 2) // (3, 1) // (3, 4) // (4, 2) // (4, 3) // (4, 1)