Skip to content

Commit 39222b7

Browse files
committed
implemented Min and Max (D&C) in C and C++, updated README.md
1 parent 311cf8e commit 39222b7

File tree

3 files changed

+120
-4
lines changed

3 files changed

+120
-4
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,13 +1285,13 @@ In order to achieve greater coverage and encourage more people to contribute to
12851285
<tr>
12861286
<td>Min and Max (D&C)</td>
12871287
<td> <!-- C -->
1288-
<a href="./CONTRIBUTING.md">
1289-
<img align="center" height="25" src="./logos/github.svg" />
1288+
<a href="./src/c/MinMaxDC.c">
1289+
<img align="center" height="25" src="./logos/c.svg" />
12901290
</a>
12911291
</td>
12921292
<td> <!-- C++ -->
1293-
<a href="./CONTRIBUTING.md">
1294-
<img align="center" height="25" src="./logos/github.svg" />
1293+
<a href="./src/cpp/MinMaxDC.cpp">
1294+
<img align="center" height="25" src="./logos/cplusplus.svg" />
12951295
</a>
12961296
</td>
12971297
<td> <!-- Java -->

src/c/MinMaxDC.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <stdio.h>
2+
3+
/**
4+
* Function to find both the minimum and maximum elements in an array
5+
*
6+
* @param arr The array to search in
7+
* @param left The left index of the sub-array.
8+
* @param right The right index of the sub-array
9+
* @param min Pointer to store the minimum element
10+
* @param max Pointer to store the maximum element
11+
*/
12+
13+
void MinAndMax(int arr[], int left, int right, int* min, int* max) {
14+
// if there is only one element in the sub-array, set it as both min and max
15+
if (left == right) {
16+
*min = *max = arr[left];
17+
return;
18+
}
19+
20+
// if there are two elements in the sub-array, compare and set min and max
21+
if (right - left == 1) {
22+
if (arr[left] < arr[right]) {
23+
*min = arr[left];
24+
*max = arr[right];
25+
} else {
26+
*min = arr[right];
27+
*max = arr[left];
28+
}
29+
return;
30+
}
31+
32+
// calculate the middle index of the sub-array
33+
int mid = (left + right) / 2;
34+
int leftMin, leftMax, rightMin, rightMax;
35+
36+
// recursively find min and max in the left and right sub-arrays
37+
MinAndMax(arr, left, mid, &leftMin, &leftMax);
38+
MinAndMax(arr, mid + 1, right, &rightMin, &rightMax);
39+
40+
// update the minimum and maximum values
41+
*min = (leftMin < rightMin) ? leftMin : rightMin;
42+
*max = (leftMax > rightMax) ? leftMax : rightMax;
43+
}
44+
45+
int main() {
46+
int arr[] = {10, 5, 20, 8, 15, 30, -12, 24};
47+
int arrSize = sizeof(arr) / sizeof(arr[0]);
48+
49+
int min, max;
50+
51+
MinAndMax(arr, 0, arrSize - 1, &min, &max);
52+
53+
printf("Minimum element: %d\n", min);
54+
printf("Maximum element: %d\n", max);
55+
56+
return 0;
57+
}

src/cpp/MinMaxDC.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
/**
5+
* Function to find both the minimum and maximum elements in an array
6+
*
7+
* @param arr The array to search in
8+
* @param left The left index of the sub-array
9+
* @param right The right index of the sub-array
10+
* @param min Reference to store the minimum element
11+
* @param max Reference to store the maximum element
12+
*/
13+
14+
void MinAndMax(const std::vector<int>& arr, int left, int right, int& min, int& max) {
15+
16+
// if there is only one element in the subarray, it is both the minimum and maximum
17+
if (left == right) {
18+
min = max = arr[left];
19+
return;
20+
}
21+
22+
// if there are two elements, compare and set min and max
23+
if (right - left == 1) {
24+
if (arr[left] < arr[right]) {
25+
min = arr[left];
26+
max = arr[right];
27+
}
28+
else {
29+
min = arr[right];
30+
max = arr[left];
31+
}
32+
return;
33+
}
34+
35+
// calculate the middle index of the sub-array
36+
int mid = (left + right) / 2;
37+
int leftMin, leftMax, rightMin, rightMax;
38+
39+
// recursively find min and max in the left and right sub-arrays
40+
MinAndMax(arr, left, mid, leftMin, leftMax);
41+
MinAndMax(arr, mid + 1, right, rightMin, rightMax);
42+
43+
// update the minimum and maximum values
44+
min = std::min(leftMin, rightMin);
45+
max = std::max(leftMax, rightMax);
46+
}
47+
48+
int main() {
49+
50+
std::vector<int> arr = {10, 5, 20, 8, 15, 30, -12, 24};
51+
int min, max;
52+
53+
MinAndMax(arr, 0, arr.size() - 1, min, max);
54+
55+
std::cout << "Minimum element: " << min << std::endl;
56+
std::cout << "Maximum element: " << max << std::endl;
57+
58+
return 0;
59+
}

0 commit comments

Comments
 (0)