Array Manipulation and Sum using C/C++

In this problem, you are given an integer array arr of size n and an integer S. Your task is to find an element k in the array such that if all the elements greater than k in the array are replaced with k, then the sum of all the elements of the resultant array becomes equal to S. If such an element exists, print it; otherwise, print -1.

Syntax

int getElement(int arr[], int n, int S);

Algorithm

To solve this problem efficiently, we follow these steps −

  • Sort the array in ascending order.
  • Initialize a variable sum to 0.
  • For each element at index i, check if sum + (arr[i] * (n - i)) = S.
  • If condition is true, return arr[i] as our answer.
  • Otherwise, add current element to sum and continue.
  • If no element satisfies the condition, return -1.

Example 1

Let's find an element k where replacing all elements greater than k makes the sum equal to 9 −

#include <stdio.h>
#include <stdlib.h>

int cmpfunc(const void *a, const void *b) {
    return (*(int*)a - *(int*)b);
}

int getElement(int arr[], int n, int S) {
    qsort(arr, n, sizeof(int), cmpfunc);
    
    int sum = 0;
    
    for (int i = 0; i < n; i++) {
        if (sum + (arr[i] * (n - i)) == S)
            return arr[i];
        sum += arr[i];
    }
    
    return -1;
}

int main() {
    int arr[] = {1, 3, 2, 5, 8};
    int n = sizeof(arr) / sizeof(arr[0]);
    int S = 9;
    
    printf("Target sum: %d\n", S);
    printf("Element k: %d\n", getElement(arr, n, S));
    
    return 0;
}
Target sum: 9
Element k: 2

Explanation: When k=2, we replace all elements greater than 2 with 2, giving us {1, 2, 2, 2, 2}. Sum = 1 + 2 + 2 + 2 + 2 = 9.

Example 2

Let's find k for a different target sum −

#include <stdio.h>
#include <stdlib.h>

int cmpfunc(const void *a, const void *b) {
    return (*(int*)a - *(int*)b);
}

int getElement(int arr[], int n, int S) {
    qsort(arr, n, sizeof(int), cmpfunc);
    
    int sum = 0;
    
    for (int i = 0; i < n; i++) {
        if (sum + (arr[i] * (n - i)) == S)
            return arr[i];
        sum += arr[i];
    }
    
    return -1;
}

int main() {
    int arr[] = {1, 3, 2, 5, 8};
    int n = sizeof(arr) / sizeof(arr[0]);
    int S = 12;
    
    printf("Target sum: %d\n", S);
    printf("Element k: %d\n", getElement(arr, n, S));
    
    return 0;
}
Target sum: 12
Element k: 3

Explanation: When k=3, we replace all elements greater than 3 with 3, giving us {1, 3, 2, 3, 3}. Sum = 1 + 3 + 2 + 3 + 3 = 12.

Time and Space Complexity

Complexity Value Reason
Time Complexity O(n log n) Due to sorting the array
Space Complexity O(1) Constant extra space used

Conclusion

This array manipulation problem is efficiently solved by sorting the array first and then checking each element as a potential threshold value. The key insight is that we only need to check array elements as candidates for k.

Farhan Muhamed
Farhan Muhamed

No Code Developer, Vibe Coder

Updated on: 2026-03-15T11:40:33+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements