Leetcode 755 Solution

This article provides solution to leetcode question 755 (reach-a-number)

https://leetcode.com/problems/reach-a-number

Solution

class Solution {
public:
    int reachNumber(int target) {
        target = abs(target);

        int k = 0;
        while (target > 0)
            target -= ++k;

        return target % 2 ? k + 1 + k % 2 : k;
    }
};