Sum of array is a small problem where we have to add each element in the array by traversing through the entire array. But when the number of elements are too large, it could take a lot of time. But this could solved by dividing the array into parts and finding sum of each part simultaneously i.e. by finding sum of each portion in parallel.
This could be done by using multi-threading where each core of the processor is used. In our case, each core will evaluate sum of one portion and finally we will add the sum of all the portion to get the final sum. In this way we could improve the performance of a program as well as utilize the cores of processor.
It is better to use one thread for each core. Although you can create as many thread as you want for better understanding of multi-threading.
Input : 1, 5, 7, 10, 12, 14, 15, 18, 20, 22, 25, 27, 30, 64, 110, 220 Output : sum is 600 Input : 10, 50, 70, 100, 120, 140, 150, 180, 200, 220, 250, 270, 300, 640, 110, 220 Output : sum is 3030
Note – It is advised to execute the program in Linux based system.
Compile in linux using following code:
g++ -pthread program_name.cpp
sum is 600
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum element in a very large array using pthreads
- Addition and Subtraction of Matrix using pthreads
- Odd Even Transposition Sort / Brick Sort using pthreads
- Find an element in array such that sum of left array is equal to sum of right array
- Maximum sum subarray having sum less than or equal to given sum using Set
- Check if there exist two elements in an array whose sum is equal to the sum of rest of the array
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Find Sum of all unique sub-array sum for a given array.
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Maximize product of array by replacing array elements with its sum or product with element from another array
- Maximum sum subarray having sum less than or equal to given sum
- Count of numbers satisfying m + sum(m) + sum(sum(m)) = N
- Maximum subarray sum in O(n) using prefix sum
- Construct sum-array with sum of elements in given range
- Minimum array size after repeated replacement of even sum pair with sum
- Sum of XOR of sum of all pairs in an array
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Minimize the sum calculated by repeatedly removing any two elements and inserting their sum to the Array
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.