Rotate your phone or change to desktop for better experience

Rotate your phone or change to desktop for better experience

6.Write a program to find minimum and maximum value in an array using divide and conquer. || DAA

#include <stdio.h> 

typedef struct { 

int min; 

int max; 

} MinMax; 

MinMax findMinMax(int arr[], int low, int high)  

{ MinMax result, leftResult, rightResult; 

int mid; 

if (low == high)  

{ result.min = arr[low];  

result.max = arr[low]; 

return result; 

if (high == low + 1) { 

if (arr[low] < arr[high])  

{ result.min = arr[low];  

result.max = arr[high]; 

} else { 

result.min = arr[high]; 

result.max = arr[low]; 

return result; 

mid = (low    + high) / 2; 

leftResult = findMinMax(arr, low, mid);

rightResult = findMinMax(arr, mid + 1, high); 

result.min = (leftResult.min < rightResult.min) ? leftResult.min : rightResult.min; 

result.max = (leftResult.max > rightResult.max) ? leftResult.max : rightResult.max; 

return result; 

int main() { 

int n; 

printf("Enter the number of elements in the array: "); 

scanf("%d", &n); 

int arr[n]; 

printf("Enter the elements of the array:\n"); 

for (int i = 0; i < n; i++) { 

scanf("%d", &arr[i]); 

MinMax result = findMinMax(arr, 0, n - 1); 

printf("Minimum value in the array: %d\n", result.min); 

printf("Maximum value in the array: %d\n", result.max); 

return 0; 

Post a Comment

0 Comments