Halfway Point
Time limit1sMemory limit512 MB
Given n, find the value printed when the pair-comparison loop reaches its halfway point (the last item index printed there).
- Level
Medium4 of 10
- Topics
- Binary search, Math, Prefix sum, Implementation
- Solved
- No attempts yet
Problem
A friend of yours wrote a program that compares every pair of items exactly once. The program first prints and compares item with items . It then prints and compares item with items . It goes on the same way until every pair has been compared exactly once. If it compares item with item , it never compares item with item later, and it never compares an item with itself.
Your friend wants to know the moment the program is halfway done. If the total number of comparisons is odd, that is the moment it performs the middle comparison. If the total is even, that is the moment it performs the first of the two middle comparisons.
Find the last number printed when the program is halfway done.
Earlier items take part in more comparisons than later ones, so the answer is not .
Input
The first line contains an integer . ()
Output
Print on one line the last number your friend's program prints when it is halfway done.