/**
 * Given a positive integer 'n' and a target sum, find the number 'x' such that sum(range(x)) is equal to the target sum.
 *
 * @param n A positive integer representing the limit of the range.
 * @param targetSum An integer representing the target sum.
 * @return The number 'x' such that sum(range(x)) equals the target sum.
 * Returns -1 if no such number 'x' exists.
 */
fun calculateTargetNumber(n: Int, targetSum: Int): Int {

    var lowerBound = 0
    var upperBound = n

    while (lowerBound <= upperBound) {
        val mid = (lowerBound + upperBound) / 2

        val currentSum = (mid * (mid + 1)) / 2

        if (currentSum == targetSum) {
            return mid
        } else if (currentSum < targetSum) {
            lowerBound = mid + 1
        } else {
            upperBound = mid - 1
        }
    }

    return -1
}
/**
 * Given a positive integer 'n' and a target sum, find the number 'x' such that sum(range(x)) is equal to the target sum.
 *
 * @param n A positive integer representing the limit of the range.
 * @param targetSum An integer representing the target sum.
 * @return The number 'x' such that sum(range(x)) equals the target sum.
 * Returns -1 if no such number 'x' exists.
 */
fun calculateTargetNumber(n: Int, targetSum: Int): Int {
/**
 * Given a positive integer 'n' and a target sum, find the number 'x' such that sum(range(x)) is equal to the target sum.
 *
 * @param n A positive integer representing the limit of the range.
 * @param targetSum An integer representing the target sum.
 * @return The number 'x' such that sum(range(x)) equals the target sum.
 * Returns -1 if no such number 'x' exists.
 */
fun calculateTargetNumber(n: Int, targetSum: Int): Int {

    var lowerBound = 0
    var upperBound = n

    while (lowerBound <= upperBound) {
        val mid = (lowerBound + upperBound) / 2

        val currentSum = (mid * (mid + 1)) / 2

        if (currentSum == targetSum) {
            return mid
        } else if (currentSum < targetSum) {
            lowerBound = mid + 1
        } else {
            upperBound = mid - 1
        }
    }

    return -1
}