fork download
  1. #include <stdio.h>
  2.  
  3. int natural_num_sum(int n)
  4. {
  5. return (n + 1) * n / 2;
  6. }
  7.  
  8. int sum_lower_equal(const int *arr, int arr_size, int lower_equal)
  9. {
  10. int sum = 0;
  11.  
  12. for (int i = 0; i < arr_size; i++) {
  13. if (arr[i] <= lower_equal) {
  14. sum += arr[i];
  15. }
  16. }
  17.  
  18. return sum;
  19. }
  20.  
  21. int sum(const int *arr, int arr_size) {
  22. int sum = 0;
  23.  
  24. for (int i = 0; i < arr_size; i++) {
  25. sum += arr[i];
  26. }
  27.  
  28. return sum;
  29. }
  30.  
  31. void solve_2(const int *arr, int arr_size)
  32. {
  33. int n = arr_size + 2;
  34. int sum_x_y = natural_num_sum(n) - sum(arr, arr_size);
  35. int x_is_lower_equal = sum_x_y / 2;
  36. int x = natural_num_sum(x_is_lower_equal) - sum_lower_equal(arr, arr_size, x_is_lower_equal);
  37. int y = sum_x_y - x;
  38.  
  39. printf("x = %d, y = %d\n", x, y);
  40. }
  41.  
  42. int main(void) {
  43. const int test[] = {1, 4, 3, 2};
  44. solve_2(test, sizeof(test)/sizeof(test[0]));
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0s 10320KB
stdin
Standard input is empty
stdout
x = 5, y = 6