
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Scenario 1:
Input: int S = 9; int arr[] = { 1, 3, 2, 5, 8 }; Output: 2
Explanation: In the given array, if we replace all elements greater than 2 with 2, the resultant array becomes {1, 2, 2, 2, 2}. The sum of this array is 9, which is equal to S. So the output is 2.
Scenario 2:
Input: int S = 12; int arr[] = { 1, 3, 2, 5, 8 }; Output: 3
Explanation: In the given array, if we replace all elements greater than 3 with 3, the resultant array becomes {1, 3, 2, 3, 3}. The sum of this array is 12, which is equal to S. So the output is 3.
Algorithm to Solve Array Manipulation and Sum
To solve this problem, we can follow these steps:
- Initialize an array arr of size n and an integer S.
- Sort the array in ascending order.
- Initialize a variable sum to 0.
- Traverse through the sorted array and for each element, check if sum + (arr[i] * (n - i)) = S, where sum consists of the sum of all elements before the current index and i is the current index of iteration.
- If the condition is true, print K and end the program.
- If not, add the current element to sum and continue the next iteration of the loop.
- If you are able to traverse the entire array without finding such an element, print -1.
C/C++ Code to Implement Array Manipulation and Sum
Here is a C/C++ implementation of the algorithm described above:
#include <stdio.h> #include <stdlib.h> // comparator function for qsort int cmpfunc(const void *a, const void *b) { return (*(int*)a - *(int*)b); } int getElement(int arr[], int n, int S) { // Sort the array qsort(arr, n, sizeof(int), cmpfunc); int sum = 0; for (int i = 0; i < n; i++) { // If current element satisfies the condition if (sum + (arr[i] * (n - i)) == S) return arr[i]; sum += arr[i]; } // No element found return -1; } int main() { int S = 9; int arr[] = { 1, 3, 2, 5, 8 }; int n = sizeof(arr) / sizeof(arr[0]); printf("%d\n", getElement(arr, n, S)); return 0; }
The output of the above code will be:
2
#include <bits/stdc++.h> using namespace std; // Function to return the required element int getElement(int a[], int n, int S) { // Sort the array sort(a, a + n); int sum = 0; for (int i = 0; i < n; i++) { // If current element // satisfies the condition if (sum + (a[i] * (n - i)) == S) return a[i]; sum += a[i]; } // No element found return -1; } // Driver code int main() { int S = 12; int a[] = { 1, 3, 2, 5, 8 }; int n = sizeof(a) / sizeof(a[0]); cout << getElement(a, n, S); return 0; }
The output of the above code will be:
3
Time and Space Complexity
Time Complexity: The time complexity of this algorithm is O(n log n). Because we are sorting the array using built-in sort function.
Space Complexity: The space complexity of this algorithm is O(1). We are using a constant amount of extra space.