Skip to main content

Command Palette

Search for a command to run...

Distribute Candies to People

Published
2 min read
Distribute Candies to People
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

We distribute some number of candies, to a row of n = num_people people in the following way:

We then give 1 candy to the first person, 2 candies to the second person, and so on until we give n candies to the last person.

Then, we go back to the start of the row, giving n + 1 candies to the first person, n + 2 candies to the second person, and so on until we give 2 * n candies to the last person.

This process repeats (with us giving one more candy each time, and moving to the start of the row after we reach the end) until we run out of candies. The last person will receive all of our remaining candies (not necessarily one more than the previous gift).

Return an array (of length num_people and sum candies) that represents the final distribution of candies.

LeetCode Problem - 1103

class Solution {
    public int[] distributeCandies(int candies, int num_people) {

        int idx = 0; // Initialize the index to track the current person
        int candyDistribute = 1; // Initialize the number of candies to distribute
        int[] resultArray = new int[num_people]; // Initialize the result array to store candies for each person

        // Loop until all candies are distributed
        while (candies > 0) {
            // Reset the index to 0 if it reaches the number of people
            idx = (idx == num_people) ? 0 : idx;

            // If there are enough candies to distribute the current amount
            if (candies - candyDistribute >= 0) {
                // Distribute candies to the current person and move to the next person
                resultArray[idx++] += candyDistribute;
                candies -= candyDistribute; // Decrease the remaining candies
                candyDistribute++; // Increment the number of candies for the next distribution
            } else {
                // If not enough candies to distribute the current amount, give all remaining candies to the current person
                resultArray[idx] += candies;
                break; // Exit the loop as all candies are distributed
            }
        }

        return resultArray; // Return the result array with the distribution of candies
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.