Given an array arr[], the task is to find the elements from the array which are equal to the sum of any sub-array of size greater than 1.
Examples:
Input:_ arr[] = {1, 2, 3, 4, 5, 6}_
Output:_ 3, 5, 6_
Explanation:
The elements 3, 5, 6 are equal to sum of subarrays {1, 2},{2, 3} and {1, 2, 3} respectively.
Input:_ arr[] = {5, 6, 10, 1, 3, 4, 8, 16}_
Output:_ 4, 8, 16_
Explanation:
The elements 4, 8, 16 are equal to the sum of subarrays {1, 3}, {1, 3, 4}, {1, 3, 4, 8} respectively
Approach: The idea is to use prefix sum array to solve the given problem. Create a prefix[] array that stores the prefix sum of all its preceding elements for every index. Store the sum of all subarrays in a map and search if any array element is present in the map or not.
#arrays #hash #prefix-sum #subarray #subarray-sum