Guest User

Untitled

a guest
Oct 9th, 2020
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.91 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. class HowManyNumbers
  5. {
  6.    public static List<long> FindAll(int sumDigits, int numDigits)
  7.    {
  8.       var list = Enumerable.Range((int)Math.Pow(10, numDigits - 1), (int)Math.Pow(10, numDigits) - 1)
  9.                  .Where(x => GetDigits(x).Sum() == sumDigits
  10.                         && GetDigits(x)
  11.                            .Zip(GetDigits(x).Skip(1), (next, curr) => next >= curr)
  12.                            .All(a => a)
  13.                         );
  14.       if(list.Count() > 0)
  15.           return new List<long>(){list.Count(), list.Take(1).First(), list.OrderByDescending(x => x).Take(1).First()};
  16.       else
  17.           return new List<long>();          
  18.    }
  19.  
  20.   public static IEnumerable<int> GetDigits(int source)
  21.   {
  22.     while (source > 0)
  23.     {
  24.         var digit = source % 10;
  25.         source /= 10;
  26.         yield return digit;
  27.     }
  28.   }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment