fork(1) download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace ConsoleApp1
  6. {
  7. class Program
  8. {
  9. static IEnumerable<IEnumerable<int>> SelectNElements(int[] arr, int n)
  10. {
  11. IEnumerable<IEnumerable<int>> result = arr.Select(x => new List<int>() { x });
  12. for (int i = 1; i < n; i++)
  13. result = result.SelectMany(x => arr.Where(y => y > x.Max()), (x, y) => x.Concat(new int[] { y }));
  14. return result;
  15. }
  16. static void Main(string[] args)
  17. {
  18. int[] arr = { 1, 2, 3, 4 };
  19. for (int i = 1; i <= arr.Length; i++)
  20. {
  21. var result = SelectNElements(arr, i);
  22. foreach (var item in result)
  23. {
  24. foreach (int x in item)
  25. Console.Write(x + " ");
  26. Console.WriteLine();
  27. }
  28. }
  29. }
  30. }
  31. }
  32.  
Success #stdin #stdout 0.04s 16184KB
stdin
Standard input is empty
stdout
1 
2 
3 
4 
1 2 
1 3 
1 4 
2 3 
2 4 
3 4 
1 2 3 
1 2 4 
1 3 4 
2 3 4 
1 2 3 4