fork download
  1. /*Write a Program to take a positive integer n as input, and find the pivot integer x such that the sum of all
  2. elements between 1 and x inclusively equals the sum of all elements between x and n inclusively. Print the pivot
  3. integer x. If no such integer exists, print -1. Assume that it is guaranteed that there will be at most one
  4. pivot integer for the given input.
  5. */
  6. #include <stdio.h>
  7.  
  8. int main() {
  9. int n;
  10. scanf("%d", &n);
  11.  
  12. long long total = (long long)n * (n + 1) / 2;
  13.  
  14. int x = -1;
  15. for (int i = 1; i <= n; i++) {
  16. long long left = (long long)i * (i + 1) / 2;
  17. long long right = total - (long long)(i - 1) * i / 2;
  18.  
  19. if (left == right) {
  20. x = i;
  21. break;
  22. }
  23. }
  24.  
  25. printf("%d", x);
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0s 5288KB
stdin
8
stdout
6