Statistics

Problem Statement for "OneForYou"

Problem Statement

When trying to divide coins "equally" between two people, an old gag goes something like this...

  • The person dividing up the loot says "One for you, and one for me" giving one coin to the other person and then themselves.
  • Next they say "And that's two for you", while giving the other person a second coin.
  • Then, they say "And that's one, two, for me", giving themselves two coins, additional to the first one they already had.
  • Then, they say "That's three for you, and one, two, three, for me", given the other person a third coin before giving themselves three more coins.
  • ... and so on

The are N coins to be divided. Assuming they are divided in this sneaky fashion, how many coins will the first person end up getting?

Definition

Class:
OneForYou
Method:
coinCount
Parameters:
long
Returns:
long
Method signature:
long coinCount(long N)
(be sure your method is public)

Constraints

  • N will be between 0 and 10^15, inclusive.

Examples

  1. 2

    Returns: 1

    In this case, it's actually fair, each person gets one coin.

  2. 9

    Returns: 6

    This is three full rounds of dividing coins.

  3. 11

    Returns: 7

    Here we run out of coins before the person dividing the coins takes four of them, so he takes as many as he can.

  4. 33

    Returns: 26

  5. 1000000000

    Returns: 999955280

  6. 1000000000000000

    Returns: 999999955278641

  7. 12345678901234

    Returns: 12345673932195

  8. 0

    Returns: 0

  9. 1

    Returns: 0

  10. 99999999999999

    Returns: 99999985857864

  11. 100000000000000

    Returns: 99999985857865

  12. 999999999999999

    Returns: 999999955278640

  13. 10000000000

    Returns: 9999858580

  14. 1000000001

    Returns: 999955281

  15. 234124152345436

    Returns: 234124130706391


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: