Candies Distribution Problem Statement

Prateek is a kindergarten teacher with a mission to distribute candies to students based on their performance. Each student must get at least one candy, and if two students are adjacent in the line, the one with the higher rating should receive more candies. The goal is to minimize the total candies distributed.

Task:

Given an array 'STUDENTS' of size 'N' representing the ratings of students, determine the minimum number of candies Prateek needs to buy to fulfill the distribution criteria.

Example:

Given students' ratings: [5, 8, 1, 5, 9, 4].
Optimal candy distribution: [1, 2, 1, 2, 3, 1].
Total candies required: 10.

Constraints:

  • 1 <= T <= 10^2
  • 1 <= N <= 10^4
  • 1 <= STUDENTS[i] <= 10^5
  • Time Limit: 1 sec

Input:

The first line contains an integer 'T', the number of test cases.
Each test case begins with an integer 'N', the number of students.
Followed by 'N' space-separated integers, specifying the grades of the students.

Output:

For each test case, output the minimum number of candies required.

Note:

If two students of the same grade are adjacent, they can receive the same number of candies.
Minimal candies necessary for each student is one.
Function implementation is required, no direct printing is needed.
AnswerBot
4mo

The task is to determine the minimum number of candies a teacher needs to distribute to students based on their performance ratings.

  • Iterate through the array of student ratings to determine the minimu...read more

Help your peers!
Select
Add answer anonymously...

LTIMindtree System Engineer interview questions & answers

A System Engineer was asked Q. Explain the concepts of OOPs.
A System Engineer was asked Q. What is DHCP?
A System Engineer was asked Q. What is FSMO?

Popular interview questions of System Engineer

A System Engineer was asked Q1. Explain the concepts of OOPs.
A System Engineer was asked Q2. What is DHCP?
A System Engineer was asked Q3. What is FSMO?
LTIMindtree System Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits