Mark and Toys Discussions | Algorithms | HackerRank

Mark and Toys

  • + 0 comments

    Using c#

    public static int maximumToys(List<int> prices, int k)
    {
      int n = prices.Count;
      int toyCount = 0;
      int totalSpent = 0;
    
      prices.Sort();
    
      // Buy toys while the budget allows
      for (int i = 0; i < n; i++)
      {
        if (totalSpent + prices[i] <= k)
        {
          totalSpent += prices[i];
          toyCount++;
        }
        else
        {
          // If we can't afford this toy, stop the process
          break;
        }
      }
    }