Given a very large array of integers, find maximum within the array using multithreading.

**Examples:**

Input : 1, 5, 7, 10, 12, 14, 15, 18, 20, 22, 25, 27, 30, 64, 110, 220 Output :Maximum Element is : 220 Input : 10, 50, 70, 100, 120, 140, 150, 180, 200, 220, 250, 270, 300, 640, 110, 220 Output : Maximum Element is : 640

**Prerequisite :** Multithreading

**Note :** Useful in large files of size MB/GB.

**How to run : **

It can only be run on linux envirenment.

command :

>> gcc -pthread maximum.c >> ./a.out

`// C++ code to find maximum of an array using Multithreading ` `#include <pthread.h> ` `#include <stdio.h> ` `#include <stdlib.h> ` ` ` `// Size of array ` `#define max 16 ` ` ` `// Max number of thread ` `#define Th_max 4 ` ` ` `// Array ` `int` `a[max] = { 1, 5, 7, 10, 12, 14, 15, 18, 20, ` ` ` `22, 25, 27, 300, 64, 110, 220 }; ` ` ` `// Array to store max of threads ` `int` `max_num[Th_max] = { 0 }; ` `int` `thread_no = 0; ` ` ` `// Function to find maximum ` `void` `maximum(` `void` `* arg) ` `{ ` ` ` `int` `i, num = thread_no++; ` ` ` `int` `maxs = 0; ` ` ` ` ` `for` `(i = num * (max / 4); i < (num + 1) * (max / 4); i++) { ` ` ` `if` `(a[i] > maxs) ` ` ` `maxs = a[i]; ` ` ` `} ` ` ` ` ` `max_num[num] = maxs; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `maxs = 0; ` ` ` `int` `i; ` ` ` `pthread_t threads[Th_max]; ` ` ` ` ` `// creating 4 threads ` ` ` `for` `(i = 0; i < Th_max; i++) ` ` ` `pthread_create(&threads[i], NULL, ` ` ` `maximum, (` `void` `*)NULL); ` ` ` ` ` `// joining 4 threads i.e. waiting for ` ` ` `// all 4 threads to complete ` ` ` `for` `(i = 0; i < Th_max; i++) ` ` ` `pthread_join(threads[i], NULL); ` ` ` ` ` `// Finding max element in an array ` ` ` `// by individual threads ` ` ` `for` `(i = 0; i < Th_max; i++) { ` ` ` `if` `(max_num[i] > maxs) ` ` ` `maxs = max_num[i]; ` ` ` `} ` ` ` ` ` `printf` `(` `"Maximum Element is : %d"` `, maxs); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

Maximum Element is : 300

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.

## Recommended Posts:

- Sum of an array using pthreads
- Odd Even Transposition Sort / Brick Sort using pthreads
- Closest greater element for every array element from another array
- Check if minimum element in array is less than or equals half of every other element
- Largest possible Subset from an Array such that no element is K times any other element in the Subset
- Dividing a Large file into Separate Modules in C/C++, Java and Python
- Find the maximum element in an array which is first increasing and then decreasing
- Program to find the minimum (or maximum) element of an array
- Sum and Product of minimum and maximum element of an Array
- Leftmost and rightmost indices of the maximum and the minimum element of an array
- Maximum element in a sorted and rotated array
- Find maximum sum taking every Kth element in the array
- Remove all occurrences of any element for maximum array sum
- Maximum possible middle element of the array after deleting exactly k elements
- C program to find the maximum and minimum element of the array
- Minimum distance between the maximum and minimum element of a given Array
- Maximum Subarray Sum possible by replacing an Array element by its Square
- Longest Subarrays having each Array element as the maximum
- Count subarrays which contains both the maximum and minimum array element
- Count subsequences which contains both the maximum and minimum array element

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.