Saturday, April 20, 2024

Distribute the chocolates among children

    private static List<int> DistributeEqually(List<int> bags ,  int children )

   {


       List<int> equally = new List<int>();


       int totalCount = 0; 


       foreach ( int bag in bags )

       {

           totalCount = totalCount + bag;

       }


       int equalCount = totalCount/children;


       int nonEqualCount = totalCount % children;


       for ( int i = 0;i < children; i++)

       {

           equally.Add(equalCount);

       }


       for (int j = 0; j < nonEqualCount; j++)

       {

           equally[j] = equally[j] + 1;

       }


       return equally;



   }

No comments:

Post a Comment