Candy in the box
Practice
4.4 (12 votes)
Observation
Linear search
Math
Algorithms
Problem
34% Success 4241 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You have N boxes numbered 1 through N and K candies numbered 1 through K. You put the candies in the boxes in the following order:

  • first candy in the first box,
  • second candy in the second box,
  • ....... 
  • ........
  • so up to N-th candy in the Nth box,
  • the next candy in (N - 1)-th box,
  • the next candy in (N - 2)-th box
  • ........
  • .......
  • and so on up to the first box, 
  • then the next candy in the second box 
  •  ......    and so on until there is no candy left.

So you put the candies in the boxes in the following order: \(1, 2, 3, ...., N, N - 1, N - 2, ...., 2, 1, 2, 3, ...., N, N - 1, ....\)

Find the index of the box where you put the K-th candy.

Input format

  • The first line contains denoting the number of test cases. The description of T test cases is as follows:
  • Each test case consists of a single line containing two integers N, and K.

Output format

For each test case, print the index of the box where you put the K-th candy.

Constraints

\(1 \leq T \leq 10^5 \\ 2 \leq N\leq 10^9\\ 1 \leq K\leq 10^9\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
12 votes
Tags:
Linear SearchImplementationAlgorithmsGreedy algorithm
Points:20
175 votes
Tags:
AlgorithmsEasySearching
Points:20
125 votes
Tags:
AlgorithmsEasySearching