/*Write a Program to take a positive integer n as input, and find the pivot integer x such that the sum of all
elements between 1 and x inclusively equals the sum of all elements between x and n inclusively. Print the pivot
integer x. If no such integer exists, print -1. Assume that it is guaranteed that there will be at most one
pivot integer for the given input.
*/
#include <stdio.h>
int main() {
int n;
long long total = (long long)n * (n + 1) / 2;
int x = -1;
for (int i = 1; i <= n; i++) {
long long left = (long long)i * (i + 1) / 2;
long long right = total - (long long)(i - 1) * i / 2;
if (left == right) {
x = i;
break;
}
}
return 0;
}
LypXcml0ZSBhIFByb2dyYW0gdG8gdGFrZSBhIHBvc2l0aXZlIGludGVnZXIgbiBhcyBpbnB1dCwgYW5kIGZpbmQgdGhlIHBpdm90IGludGVnZXIgeCBzdWNoIHRoYXQgdGhlIHN1bSBvZiBhbGwKZWxlbWVudHMgYmV0d2VlbiAxIGFuZCB4IGluY2x1c2l2ZWx5IGVxdWFscyB0aGUgc3VtIG9mIGFsbCBlbGVtZW50cyBiZXR3ZWVuIHggYW5kIG4gaW5jbHVzaXZlbHkuIFByaW50IHRoZSBwaXZvdCAKaW50ZWdlciB4LiBJZiBubyBzdWNoIGludGVnZXIgZXhpc3RzLCBwcmludCAtMS4gQXNzdW1lIHRoYXQgaXQgaXMgZ3VhcmFudGVlZCB0aGF0IHRoZXJlIHdpbGwgYmUgYXQgbW9zdCBvbmUgCnBpdm90IGludGVnZXIgZm9yIHRoZSBnaXZlbiBpbnB1dC4KKi8KI2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbigpIHsKICAgIGludCBuOwogICAgc2NhbmYoIiVkIiwgJm4pOwoKICAgIGxvbmcgbG9uZyB0b3RhbCA9IChsb25nIGxvbmcpbiAqIChuICsgMSkgLyAyOwoKICAgIGludCB4ID0gLTE7CiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspIHsKICAgICAgICBsb25nIGxvbmcgbGVmdCA9IChsb25nIGxvbmcpaSAqIChpICsgMSkgLyAyOwogICAgICAgIGxvbmcgbG9uZyByaWdodCA9IHRvdGFsIC0gKGxvbmcgbG9uZykoaSAtIDEpICogaSAvIDI7CgogICAgICAgIGlmIChsZWZ0ID09IHJpZ2h0KSB7CiAgICAgICAgICAgIHggPSBpOwogICAgICAgICAgICBicmVhazsKICAgICAgICB9CiAgICB9CgogICAgcHJpbnRmKCIlZCIsIHgpOwogICAgcmV0dXJuIDA7Cn0K