
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- 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
- Increment value of an array element with array object in MongoDB
- 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++
- 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
- Compute the truth value of an array XOR another array element-wise in Numpy
- Best way to find two numbers in an array whose sum is a specific number with JavaScript?
- Find sum of factorials in an array in C++
- How to validate if an element in an array is repeated? - JavaScript
- Find the Middle Element of an array in JAVA
- Compute the truth value of an array AND to another array element-wise in Numpy
