Pass the Pillow

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.
There are n people standing in a line labeled from 1 to n. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the pillow in the opposite direction.
- For example, once the pillow reaches the
n<sup>th</sup>person they pass it to then - 1<sup>th</sup>person, then to then - 2<sup>th</sup>person and so on.
Given the two positive integers n and time, return the index of the person holding the pillow after time seconds.
LeetCode Problem - 2582
class Solution {
public int passThePillow(int n, int time) {
int idx = 1; // Starting index of the pillow
int direction = 1; // Direction of movement (1 for forward, -1 for backward)
// Iterate through each time step
for(int i=1; i<=time; i++){
idx += direction; // Move the pillow in the current direction
// Check if the pillow reaches the end points (1 or n)
if(idx == n || idx == 1){
direction = -direction; // Change direction when reaching an end point
}
}
return idx; // Return the final position of the pillow after 'time' steps
}
}




