Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace NormNumbers
  6. {
  7. class Program
  8. {
  9. private static IEnumerable<int> normalize(int min, IEnumerable<int> arr)
  10. {
  11. if (!arr.Any())
  12. {
  13. yield break;
  14. }
  15.  
  16. var first = arr.First();
  17. var normalized = first - min > 1
  18. ? min + 1
  19. : first;
  20. yield return normalized;
  21.  
  22. foreach (var el in normalize(normalized, arr.Skip(1)))
  23. yield return el;
  24. }
  25.  
  26. private static IEnumerable<int> buildTree(IEnumerable<int> arr)
  27. {
  28. if (!arr.Any())
  29. return Enumerable.Empty<int>();
  30.  
  31. var min = arr.Min();
  32. var left = normalize(min, arr.TakeWhile(x => x != min).Reverse());
  33. var right = normalize(min, arr.SkipWhile(x => x != min).Skip(1));
  34. return buildTree(left.Reverse()).Concat(new[] { min }).Concat(buildTree(right));
  35. }
  36.  
  37. public static void Main()
  38. {
  39. var input = new[] {2, 5, 8, 1, 7, 3, 4, 6, 10, 1, 2, 5};
  40. var output = buildTree(input);
  41. Console.WriteLine("Input: {0}", string.Join(", ", input));
  42. Console.WriteLine("Output: {0}", string.Join(", ", output));
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement