aboutsummaryrefslogtreecommitdiff
path: root/pass_the_pillow.c
blob: 2ffe2d304b984b43f9c1aeba4e283b9586f5c024 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 2582. Pass the Pillow
#include "leetcode.h"

/*
 * 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 passe it to the next person standing in the line. once the
 * pillow reaching the end of the line, the direction changes and people
 * continue passing the pillow in the opposite direction. given the two positive
 * integers 'n' and 'time', return the index of the peson holding the pillow
 * after 'time' seconds.
 */

int passThePillow(int n, int time) {
  int rounds = time / (n - 1), ans = 0;
  ans = !(rounds % 2) ? 1 + time % (n - 1) : n - time % (n - 1);
  return ans;
}

int main() {
  printf("%d\n", passThePillow(4, 5)); // expect: 2
  printf("%d\n", passThePillow(3, 2)); // expect: 3
}