C Exercises: Find the maximum n – m
88. Maximum Difference with Order Condition
Write a program in C to find the maximum difference n – m such that array[n] > array[m] from a given array[].
Given an array arr[], find the maximum difference n – m such that arr[n] > arr[m]
Expected Output:
The given array is:
7 5 8 2 3 2 4 2 1 0
m = 0, n = 2, arr1[m] = 7 arr1[n] = 8 difference = 2
m = 3, n = 6, arr1[m] = 2 arr1[n] = 4 difference = 3
The maximum differcences between two position of array index is: 3
The problem involves writing a C program to find the maximum difference n - m such that arr[n] > arr[m] for a given array. The program should iterate through the array, comparing elements to identify pairs where the condition is met, and calculate the differences between their indices. The output will display the array, the pairs of indices that satisfy the condition, and the maximum difference between these indices.
Sample Solution:
C Code:
#include <stdio.h>
// Function to find the maximum difference between indices
int findMaxDifference(int arr1[], int ar_size)
{
int max_diff = -1; // Initialize the maximum difference
// Loop through the array to find the maximum difference between two indices
for (int m = 0; m < ar_size; ++m)
{
for (int n = ar_size - 1; n > m; --n)
{
// Check if the element at index n is greater than element at index m
// If so, update the max_diff if the current difference (n - m) is greater
if(arr1[n] > arr1[m] && max_diff < (n - m))
{
// Print the indices, values, and the difference between them
printf("m = %d, n = %d, arr1[m] = %d arr1[n] = %d difference = %d\n",
m, n, arr1[m], arr1[n], n - m);
max_diff = n - m; // Update the maximum difference
}
}
}
return max_diff; // Return the maximum difference between indices
}
int main()
{
int arr1[] = {7, 5, 8, 2, 3, 2, 4, 2, 1, 0};
int n = sizeof(arr1) / sizeof(arr1[0]); // Calculate the size of the array
int i;
// Print the original array
printf("The given array is: \n");
for(i = 0; i < n; i++)
{
printf("%d ", arr1[i]);
}
printf("\n");
// Find the maximum difference between indices and print it
int max_diff = findMaxDifference(arr1, n);
printf("\nThe maximum difference between two positions of array indices is: %d", max_diff);
getchar();
return 0;
}
Output:
The given array is: 7 5 8 2 3 2 4 2 1 0 m = 0, n = 2, arr1[m] = 7 arr1[n] = 8 difference = 2 m = 3, n = 6, arr1[m] = 2 arr1[n] = 4 difference = 3 The maximum differcences between two position of array index is: 3
Flowchart:/p>
For more Practice: Solve these Related Problems:
- Write a C program to find the maximum difference where the larger element appears after the smaller using a single loop.
- Write a C program to compute the maximum difference using dynamic programming and then output the corresponding indices.
- Write a C program to determine the maximum difference using recursion by tracking minimum value so far.
- Write a C program to find the maximum difference and then verify by sorting the array and comparing extremes.
C Programming Code Editor:
Previous: Write a program in C to find the maximum element in an array which is first increasing and then decreasing.
Next: Write a program in C to find maximum size square sub-matrix with all 1s.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.