Write a C program to find largest / maximum difference between two elements of an array, such that larger element or number appears after the smaller number in the array.
Note: I’m not considering time complexity in this video tutorial intentionally. We’ll have a completely dedicated video teaching about time complexity from basic. My intention in this video is to make the logic as simple and understandable as possible.
Related Read:
C Program To Find Biggest Element of An Array
C Program To Find Smallest Element In An Array
Page Contents
Enter 6 integer numbers
7
9
5
6
13
2
The largest difference is 8, and its between 13 and 5.
#include<stdio.h> #define N 6 int main() { int num[N], i, big, small; printf("Enter %d integer numbers\n", N); for(i = 0; i < N; i++) scanf("%d", &num[i]); big = small = num[0]; for(i = 1; i < N; i++) { if(num[i] > big) big = num[i]; if(num[i] < small) small = num[i]; } printf("The largest difference is %d, ", (big - small)); printf("and its between %d and %d.\n", big, small); return 0; }
Output:
Enter 6 integer numbers
7
9
5
6
13
2
The largest difference is 11, and its between 13 and 2.
Here we find biggest element in the array and smallest element in the array. Next we subtract smallest element from biggest element to get the largest different between two array elements.
#include<stdio.h> #define N 6 int main() { int num[N], i, big, small, pos = 0; printf("Enter %d integer numbers\n", N); for(i = 0; i < N; i++) scanf("%d", &num[i]); big = small = num[0]; for(i = 1; i < N; i++) { if(num[i] > big) { big = num[i]; pos = i; } } for(i = 1; i < pos; i++) { if(num[i] < small) small = num[i]; } printf("The largest difference is %d, ", (big - small)); printf("and its between %d and %d.\n", big, small); return 0; }
Output:
Enter 6 integer numbers
7
9
5
6
13
2
The largest difference is 8, and its between 13 and 5.
As per the problem statement, the smallest number in the array must be chosen from index 0 to the position where the biggest number of the array is present.
For Example: Assume that we’ve an array {1, 2, 5, 3, 0}. Here biggest element in the array is 5 and its position in the array is 2. Now we need to select smallest number in the array between the index range 0 to 2. So the smallest number will be 1.
Step 1: First we iterate through the array using a for loop and find the biggest element in the array and we also determine the index position at which this biggest number is present.
Step 2: We write another for loop and iterate from index 1 to the position where the biggest number is present. And inside for loop we select the smallest element between the range.
Step 3: Now we subtract the smallest element from step 2 with the biggest element from step 1, and we get the largest difference between two elements of an array, where biggest number appears after the smaller number in the array.
Note:
1. We initialize variables big and small to the first element of the array, because we need to have some value to compare it with other elements of the array.
2. In both the for loops we initialize i to 1, as both variables big and small is assigned with value present at index 0. So there is no point in comparing with itself, so we start the comparison from index 1. That’s the reason we initialize i to 1 in both the for loops.
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert