Programming Exercises - April 17, 2026
Quick Jump
| Program | File | Link |
|---|---|---|
| Exercise 1 : Minimum And Maximum Using Reccursion | MinMaxReccursion.cpp | Open on GitHub |
Exercise 1 : Minimum And Maximum Using Reccursion
Algorithm
Start with the array range defined by
lowandhighIf the segment has only one element (
low == high):- Set both
minandmaxto that element
- Set both
If the segment has more than two elements:
- Calculate
mid = low + (high - low) / 2 - Recursively find
min1, max1in the left half (low → mid) - Recursively find
min2, max2in the right half (mid+1 → high) - Set
min = smaller(min1, min2) - Set
max = larger(max1, max2)
- Calculate
Return the final
minandmaxvalues
Code
cpp
#include <stdio.h>
// Global Minimum And Maximum
int min, max;
void findMinMax(int arr[], int low, int high) {
// Case 1 : Only One Element
if (low == high) {
min = arr[low];
max = arr[low];
}
// Case 2 : Divide And Conquer
if (high > low + 1) {
// Split The Array in Two Parts
int mid = low + (high - low) / 2;
// LocalMin And LocaLMax
int leftMin, leftMax, rightMin, rightMax;
// For Left Side ~ Low To Mid
findMinMax(arr, low, mid);
leftMin = min;
leftMax = max;
// For Right Side ~ Mid To High
findMinMax(arr, mid, high);
rightMin = min;
rightMax = max;
// Final Pass
min = (leftMin < rightMin) ? leftMin : rightMin;
max = (leftMax > rightMax) ? leftMax : rightMax;
}
}
int main() {
int size;
printf("Enter The Size Of Array : ");
scanf("%d", &size);
int arr[size];
for (int i = 0; i < size; i++) {
printf("Enter The Value Of Element %d : ", i);
scanf("%d", &arr[i]);
}
printf("\nThe Array : ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
findMinMax(arr, 0, size - 1);
printf("\n\nThe Maximum Element : %d", max);
printf("\nThe Minimum Element : %d", min);
return 0;
}Compile command
bash
g++ MinMaxReccursion.cpp -o MinMaxReccursion && ./MinMaxReccursionOutput
bash
Enter The Size Of Array : 5
Enter The Value Of Element 0 : 9
Enter The Value Of Element 1 : 78
Enter The Value Of Element 2 : 23
Enter The Value Of Element 3 : 67
Enter The Value Of Element 4 : 1
The Array : 9 78 23 67 1
The Maximum Element : 78
The Minimum Element : 1Compile All Files
bash
g++ MinMaxReccursion.cpp -o MinMaxReccursion && ./MinMaxReccursionTurbo C++ compatibility notes
c
// Change function signature
int main() -> void main()
// Change the final line
return 0; -> getch();