C Program To Find Largest Difference Between Two Elements of Array

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

Example: Expected Output

Enter 6 integer numbers
7
9
5
6
13
2
The largest difference is 8, and its between 13 and 5.

Visual Representation

Largest Difference Between Two Elements of Array

Video Tutorial: C Program To Find Largest Difference Between Two Elements of Array


[youtube https://www.youtube.com/watch?v=XLHcpajCZyQ]

YouTube Link: https://www.youtube.com/watch?v=XLHcpajCZyQ [Watch the Video In Full Screen.]

Source Code: C Program To Find Largest Difference Between Two Elements of Array

#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.

Find Largest Difference, where Largest Element Appears After Smallest Number in Array

#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.

Logic To Find Largest Difference b/w Two Elements of Array, where biggest number appears after the smallest number

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

C Program To Find Smallest Element In An Array

Lets write a C program to find smallest element in an array, without sorting the elements. And also print the position at which the smallest number is present in the array.

Related Read:
C Program To Find Biggest Element of An Array

Example: Expected Output

Enter 5 integer numbers
5
2
6
4
3

Biggest of 5 numbers is 2, at position 2.
array with size 5

Video Tutorial: C Program To Find Smallest Element In An Array


[youtube https://www.youtube.com/watch?v=0lnvlxKPD3A]

YouTube Link: https://www.youtube.com/watch?v=0lnvlxKPD3A [Watch the Video In Full Screen.]

Source Code: C Program To Find Smallest Element of An Array

Method 1

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, small, pos;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);

        if(i == 0 || small > a[i])
        {
            small = a[i];
            pos   = i + 1;
        }
    }

    printf("Smallest Number: %d, at position %d.\n", small, pos);

    return 0;
}

Output 1:
Enter 5 integer numbers
9
8
6
2
5
Smallest Number: 2, at position 4.

Output 2:
Enter 5 integer numbers
5
6
1
3
2
Smallest Number: 1, at position 3.

Logic To Find Smallest Element In An Array

We ask the user to input N integer numbers. N being Macro, used to assign array size. In above source code N is assigned a value of 5. So user enters 5 integer numbers. While the user inputs numbers we check if it’s the first number input by the user. If its the first number/element, then we assign that first number/element entered by the user to variable small and assign 1 to variable pos, indicating that its the first element in the array.

Next, for each consecutive iteration of the for loop we check if the new value entered by the user is smaller than the value present in variable small. If it’s true, then we assign the new value entered by the user to variable small and also update the value of pos accordingly.

Once i < N condition is false, control exits for loop and we print the value present inside variable small and pos which will have smallest of N numbers or the smallest element of the array and position of that element in the array.

Explanation With Example

If int a[5] = {5, 2, 6, 4, 3};

    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);

        if(i == 0 || small > a[i])
        {
            small = a[i];
            pos   = i + 1;
        }
    }
indexa[index]smallpos = index + 1
0a[0] = 551
1a[1] = 222
2a[2] = 6
3a[3] = 4
4a[4] = 3
5

a[5] = {5, 2, 6, 4, 3}
small = 2;
pos = 2;

Source Code: C Program To Find Smallest Element of An Array

Method 2

 #include<stdio.h>

#define N 5

int main()
{
    int a[N], i, small, pos;

    printf("Enter %d integer numbers\n", N);

    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    small = a[0];
    pos   = 1;

    for(i = 1; i < N ; i++)
    {
        if(small > a[i])
        {
            small = a[i];
            pos   = i + 1;
        }
    }

    printf("Smallest Number: %d, at position %d.\n", small, pos);

    return 0;
}

Output 1:
Enter 5 integer numbers
9
8
6
2
5
Smallest Number: 2, at position 4.

Output 2:
Enter 5 integer numbers
5
2
6
4
3
Smallest Number: 2, at position 2.

Method 2: LOGIC

Here we accept N integer numbers from the user. Next we assign the first element of the array to variable small and 1 to pos. Now we use another for loop to loop through the array.

Inside for loop
Inside for loop we check if the value present inside variable small is greater or bigger than the value present at a[i]. If it’s true, then we assign the value of a[i] to small and value of (i+1) to variable pos.

At the end of for loop, variable small and pos will have smallest element of the array and the position at which this number is present in the array.

Note: Since we assign the value of first element of the array(which is present at location a[0]) to variable small, while comparing with other elements of the array, we start comparing from a[1] till a[N-1]. That’s the reason we’ve assigned initial value of i to 1 in the second for loop.

Since a[0] is assigned to variable small, there is no point in comparing big with a[0]. So we start comparison from a[1].

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

C Program To Find Range of Set of Numbers

Write a C program to find the range of a set of numbers entered through the keyboard. Range is the difference between the smallest and biggest number in the list.

Example: If biggest number in the list is 5 and smallest number in the list is 1. The difference between them is the range. i.e., 5 – 1 = 4. So range = 4.

Related Read:
while loop in C programming
if else statement in C
Relational Operators In C
C Program To Find Absolute Value of a Number

Expected Output for the Input

User Input:
Enter the limit
5
Enter 5 numbers
1
2
3
4
5

Output:
Small Number = 1
Big Number = 5
Range is 4

Logic To Find Range of Set of Numbers

First we ask the user to enter the length or the size of the list, and store it inside the variable limit. If the user enters the list size as 5, then we ask the user to enter 5 numbers.

Next we ask the user to enter the first number. We assign the first number entered by the user to variables small and big. Since user already entered 1 number into the list, we decrement the value of variable limit by 1.

Next we take remaining inputs inside the while loop. For each iteration of the while loop we decrement the value of variable limit by 1, until the value of limit is 0. Once value of limit is zero, control exits while loop.

For each input(inside the while loop), we check if the value entered is bigger than the value present in variable big. If its true, we assign the bigger number to variable big. We also check if the value entered is smaller than the value present in variable small. If its true, we assign the smaller number to variable small.

Once the control exits the while loop, variable big will have the biggest number in the list and variable small will have the smallest number in the list.

Finally, we use below formula to calculate range of the list:
range = big – small;

Video Tutorial: C Program To Find Range of Set of Numbers


[youtube https://www.youtube.com/watch?v=-yN6KsA8d-k]

YouTube Link: https://www.youtube.com/watch?v=-yN6KsA8d-k [Watch the Video In Full Screen.]

Source Code: C Program To Find Range of Set of Numbers

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int small, big, range, num, limit;

    printf("Enter the limit\n");
    scanf("%d", &limit);

    printf("Enter %d numbers\n", limit);
    scanf("%d", &num);

    small = big = num;

    limit = limit - 1;

    while(limit)
    {
        scanf("%d", &num);

        if(num > big)
        {
            big = num;
        }

        if(num < small)
        {
            small = num;
        }

        limit--;
    }

    range = big - small;

    printf("Small Number = %d\nBig Number = %d\n", small, big);
    printf("Range is %d\n", abs(range));

    return 0;
}

Output 1:
Enter the limit
5
Enter 5 numbers
0
-1
2
3
4
Small Number = -1
Big Number = 4
Range is 5

Output 2:
Enter the limit
6
Enter 6 numbers
10
9
8
7
6
5
Small Number = 5
Big Number = 10
Range is 5

Note: abs() returns the absolute value of a number. Absolute value is always positive.

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