

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Find if array has an element whose value is half of array sum in C++
In this problem, we are given an array arr of sorted unique values. Our task is to find if array has an element whose value is half of array sum.
Problem Description: For the array arr[], we need to find element x in the array such that the sum of all elements of array is equal to 2*X.
Let’s take an example to understand the problem,
Input: arr[] = {2, 4, 5, 6, 7}
Output: No
Explanation:
Sum = 2 + 4 + 5 + 6 + 7 = 24
No element found.
Solution Approach:
To solve the problem, we simply need to find the elements which is half of the sum of all elements of the array.
Algorithm:
Step 1: Find the sum of all elements of the array.
Step 2: if the sum value is odd, return -1.
Step 3: if sum value is even, find element x such that x*2 = sum.
Step 4: if element found, return 1.
Step 5: if element not found return -1.
For searching the element, we can use the binary search algorithm as it is sorted.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; int checkForElement(int array[], int n) { int arrSum = 0; for (int i = 0; i < n; i++) arrSum += array[i]; if (arrSum % 2) return -1; int start = 0; int end = n - 1; while (start <= end) { int mid = start + (end - start) / 2; if ( ( 2 * array[mid] ) == arrSum) return array[mid]; else if (( 2 * array[mid] ) > arrSum) end = mid - 1; else start = mid + 1; } return -1; } int main() { int array[] = { 4, 5, 6, 7, 9 }; int n = sizeof(array) / sizeof(array[0]); int x = checkForElement(array, n); if(x != -1) cout<<"Element found, value is "<<x; else cout<<"Element not found!"; return 0; }
Output −
Element not found!
- Related Questions & Answers
- Write a Java program to find the first array element whose value is repeated an integer array?
- Find an element in array such that sum of left array is equal to sum of right array using c++
- Check if the array has an element which is equal to sum of all the remaining elements in Python
- Pair of (adjacent) elements of an array whose sum is lowest JavaScript
- Check if the array has an element which is equal to product of remaining elements in Python
- Count of pairs in an array whose sum is a perfect square in C++
- Increment value of an array element with array object in MongoDB
- Check if an array can be divided into pairs whose sum is divisible by k in Python
- How to find a group of three elements in an array whose sum equals some target sum JavaScript
- JavaScript: Check if array has an almost increasing sequence
- Adjacent elements of array whose sum is closest to 0 - JavaScript
- Find numbers whose sum of digits equals a value
- Best way to find two numbers in an array whose sum is a specific number with JavaScript?
- Find the Number Whose Sum of XOR with Given Array Range is Maximum using C++
- How to validate if an element in an array is repeated? - JavaScript