C Program To Find First and Second Biggest Element In An Array using Recursion

Lets write a C program to find first and second biggest element/number in an array, without sorting it, and by using recursion.

Related Read:
Basics of Pointers In C Programming Language
Introduction To Arrays: C Programming Language

Important Video Tutorial
C Programming: Arrays, Pointers and Functions

Example: Expected Output

Enter 5 integer numbers
5
2
6
4
3
First Big: 6
Second Big: 5

Visual Representation

First and Second Biggest Element In An Array using Recursion

Video Tutorial: C Program To Find First and Second Biggest Element In An Array using Recursion


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

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

Source Code: C Program To Find First and Second Biggest Element In An Array using Recursion

#include<stdio.h>

#define N 5

void fsBig(int *num, int n, int first, int second)
{
    if(n < 2)
        printf("First Big: %d\nSecond Big: %d\n", first, second);
    else
    {
        if(*num > first)
        {
            second = first;
            first  = *num;
        }
        else if(*num > second && *num != first)
            second = *num;

        fsBig(--num, --n, first, second);
    }
}

int main()
{
    int a[N], i, first, second, count = 0;

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

    for(i = 0; i < N; i++)
    {
        if(a[i] != a[0])
        {
            ( (a[0] > a[i]) ?
              (first = a[0], second = a[i]) :
              (first = a[i], second = a[0]) );
            break;
        }
        else
            count++;
    }

    if(count == N)
    {
        printf("Biggest: %d\nSmallest: %d\n", a[0], a[0]);
        return 0;
    }

    fsBig(&a[N - 1], N - 1, first, second);

    return 0;
}

Output 1:
Enter 5 integer numbers
5
5
4
2
1
First Big: 5
Second Big: 4

Output 2:
Enter 5 integer numbers
5
5
5
5
5
Biggest: 5
Smallest: 5

Logic To Find First and Second Biggest Element In An Array using Recursion

We ask the user to enter N integer numbers and store it inside the address of array variable a[N]. Next we assign the biggest value between a[0] and a[1] to variable first and second biggest value to variable second. Then we pass the last elements address and length of the array and variables first and second to a function fsBig().

Inside function fsBig()

void fsBig(int *num, int n, int first, int second)
{
    if(n < 2)
        printf("First Big: %d\nSecond Big: %d\n", first, second);
    else
    {
        if(*num > first)
        {
            second = first;
            first  = *num;
        }
        else if(*num > second && *num != first)
            second = *num;

        fsBig(--num, --n, first, second);
    }
}

First we write the base condition or the termination condition: Once the length of the array or the variable n value is less than 2, we print the value present in variable fbig and sbig, and the control exits the function fsBig();

Note: We are checking till n < 2, because variables first and second already has values present at index 0 and 1, so need not compare them again against themselves.

Inside else block we write the actual logic to find the first and second biggest element in the array. First we check if the value present in pointer variable *num is greater than value present in variable fbig. If true, then there is a element which is bigger than fbig, that means, now whatever is present in fbig becomes second biggest element – so we transfer value present in fbig to sbig, and then transfer the new found biggest element of the array to variable fbig.

If a number is not greater than fbig, it can still be greater than sbig. So we check for that condition in else if. If its true, then we transfer the value of *num to sbig.

Recursive call
Next we call the same method fsBig() with new values. i.e., we reduce the address of num by 1, we reduce the value of index n by 1, and we pass the new values of fbig and sbig. This keeps on iterating until value of n is less than 2. Once the value of n is less than 2, the base condition is met and the control executes the printf() statement and exits the function fsBig().

Using array variable to receive base address from main method

#include<stdio.h>
#define N 5

void fsBig(int num[], int n, int first, int second)
{
    if(n < 2)
        printf("First Big: %d\nSecond Big: %d\n", first, second);
    else
    {
        if(num[n] > first)
        {
            second = first;
            first  = num[n];
        }
        else if(num[n] > second && num[n] != first)
            second = num[n];

        fsBig(num, --n, first, second);
    }
}

int main()
{
    int a[N], i, first, second, count = 0;

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

    for(i = 0; i < N; i++)
    {
        if(a[i] != a[0])
        {
            ( (a[0] > a[i]) ?
              (first = a[0], second = a[i]) :
              (first = a[i], second = a[0]) );

             break;
        }
        else
            count++;
    }

    if(count == N)
    {
        printf("Biggest: %d\nSmallest: %d\n", a[0], a[0]);
        return 0;
    }

    fsBig(a, N - 1, first, second);

    return 0;
}

Output 1:
Enter 5 integer numbers
5
4
5
2
1
First Big: 5
Second Big: 4

Output 2:
Enter 5 integer numbers
5
5
4
4
2
First Big: 5
Second Big: 4

Here we do not alter the value of num while recursively calling the method fsBig(), as we compare with all the elements of the array by changing the value of index variable n.

Whenever we use array variable, compiler converts it to pointer as below

#include<stdio.h>

#define N 5

void fsBig(int num[], int n, int first, int second)
{
    if(n < 2)
        printf("First Big: %d\nSecond Big: %d\n", first, second);
    else
    {
        if(*(num + n) > first)
        {
            second = first;
            first  = *(num + n);
        }
        else if(*(num + n) > second && *(num + n) != first)
            second = *(num + n);

        fsBig(num, --n, first, second);
    }
}

int main()
{
    int a[N], i, first, second, count = 0;

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

    for(i = 0; i < N; i++)
    {
        if(a[i] != a[0])
        {
            ( (a[0] > a[i]) ?
              (first = a[0], second = a[i]) :
              (first = a[i], second = a[0]) );

             break;
        }
        else
            count++;
    }

    if(count == N)
    {
        printf("Biggest: %d\nSmallest: %d\n", a[0], a[0]);
        return 0;
    }

    fsBig(a, N - 1, first, second);

    return 0;
}

Output:
Enter 5 integer numbers
1
2
3
5
5
First Big: 5
Second Big: 3

Whenever compiler encounters array variable like this a[i] or num[n] it converts it into *(a + i) and *(num + n).
i.e., Variable_name[array_index] = *(Base_address + array_index)
Note that, Variable_name holds Base_address. So Variable_name = Base_address.

Its faster to work with address than with variables. This example proves that arrays are pointers in disguise. i.e., arrays use pointers internally.

Explanation With Example

N = 5;
a[N] = {5, 4, 6, 2, 3};
first = 5;
second = 4;
n = N -1 = 5 – 1 = 4;

void fsBig(int num[], int n, int first, int second)
{
    if(n < 2)
        printf("First Big: %d\nSecond Big: %d\n", first, second);
    else
    {
        if(*(num + n) > first)
        {
            second = first;
            first  = *(num + n);
        }
        else if(*(num + n) > second && *(num + n) != first)
            second = *(num + n);

        fsBig(num, --n, first, second);
    }
}
nnum[n]–nfbigsbig
43354
32254
26165

First Big: 6
Second Big: 5

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 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 First and Second Biggest in N Numbers, without using Arrays, using For Loop

Write a C program to find first and second biggest numbers in a list of N numbers entered by the user, without using arrays and without sorting the list and by using for loop.

Related Read:
For Loop In C Programming Language
Find First and Second Biggest in N Numbers, without using Arrays: C Program

Video Tutorial: C Program To Find First and Second Biggest in N Numbers, without using Arrays, using For Loop


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

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


Source Code: C Program To Find First and Second Biggest in N Numbers, without using Arrays, using For Loop

#include<stdio.h>

int main()
{
    int limit, num, count, fbig = 0, sbig = 0;

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

    printf("Enter %d positive numbers\n", limit);

    for(count = 1; count <= limit; count++)
    {
        scanf("%d", &num);

        if(num > fbig)
        {
            sbig = fbig;
            fbig = num;
        }

        if(num > sbig && num < fbig)
        {
            sbig = num;
        }
    }

    printf("First big = %d\nSecond Big = %d\n", fbig, sbig);

    return 0;
}

Output 1:
Enter the limit
5
Enter 5 positive numbers
1
9
5
3
8
First big = 9
Second Big = 8

Output 2:
Enter the limit
8
Enter 8 positive numbers
1
5
9
3
7
8
6
10
First big = 10
Second Big = 9

Logic To Find First and Second Biggest Number in N Numbers, without using Arrays

First we ask the user to enter length of numbers list. If user enters limit value as 5, then we ask the user to enter 5 numbers. Once the user enters limit value, we iterate the for loop until loop counter variable count is less than or equal to limit. For each iteration of for loop we ask the user to enter a positive integer number. We check if the new number entered by the user is greater than fbig. If true, we swap the value of fbig to sbig and value of num to fbig.

Next we check if the user entered number is greater than sbig and less than fbig, if true, we assign the value of num to sbig.

Once control exits for loop, inside fbig we will have biggest number from the list of numbers entered by the user and sbig will have the second biggest number from the list of numbers entered by the user.

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 Biggest of N Numbers, without using Arrays, using For Loop

Write a C program to find biggest of N numbers without using Arrays and using for loop.

Related Read:
For Loop In C Programming Language
Find Biggest of N Numbers, without using Arrays: C Program

Video Tutorial: C Program To Find Biggest of N Numbers, without using Arrays, using For Loop


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

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


Logic To Find Biggest Number of N numbers, without using Arrays

We ask the user to enter the limit. i.e., the length of the list of numbers. For Example, if user enters limit value as 5, then we accept 5 numbers from the user and then find the biggest and output that number on to the console window.

First we ask the user to enter the limit. If user enters limit value as 5, we iterate the for loop 5 times i.e., until value of count is less than or equal to limit.

We check if its the first iteration of for loop. If true, we assign the first user entered number to variable big.

Inside the for loop, for each iteration we ask the user to enter a number. Next we check if that user entered number is greater than the value present in variable big. If the new number entered by the user is greater than value present in variable big, then we assign the new number entered by the user to the variable big. We keep doing this for each number entered by the user.

Source Code: C Program To Find Biggest of N Numbers, without using Arrays, using For Loop

#include<stdio.h>

int main()
{
    int limit, num, count, big;

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

    printf("Enter %d numbers\n", limit);
    for(count = 1; count <= limit; count++)
    {
        scanf("%d", &num);

        if(num > big || count == 1)
        {
            big = num;
        }
    }

    printf("Biggest number is %d\n", big);

    return 0;
}

Output 1:
Enter the limit
5
Enter 5 numbers
1
9
3
7
4
Biggest number is 9

Output 2:
Enter the limit
6
Enter 6 numbers
-2
-5
-6
-9
-1
-4
Biggest number is -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

Find First and Second Biggest in N Numbers, without using Arrays: C Program

Write a C program to find first and second biggest numbers in list of N numbers without using arrays and without sorting the list and by using while loop.

Related Read:
while loop in C programming
Find Biggest of N Numbers, without using Arrays: C Program

Source Code: Find First and Second Biggest in N Numbers, without using Arrays: C Program

 
#include<stdio.h>

int main()
{
    int limit, num, fbig = 0, sbig = 0;

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

    printf("Enter %d positive numbers\n", limit);
 

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

        if(num > fbig)
        {
            sbig = fbig;
            fbig = num;
        }
        if(num > sbig && num < fbig)
        {
            sbig = num;
        }

        limit--;
    }

    printf("First Big is %d\n", fbig);
    printf("Second Big is %d\n", sbig);

    return 0;
}

Output 1:
Enter the limit
5
Enter 5 numbers
1
2
3
4
5
First Big is 5
Second Big is 4

Output 2:
Enter the limit
5
Enter 5 numbers
5
4
3
2
1
First Big is 5
Second Big is 4

Output 3:
Enter the limit
5
Enter 5 numbers
1
4
3
5
2
First Big is 5
Second Big is 4

Output 4:
Enter the limit
8
Enter 8 numbers
1
5
9
3
7
4
6
8
First Big is 9
Second Big is 8

Find First and Second Biggest in N Numbers, without using Arrays: C Program


[youtube https://www.youtube.com/watch?v=gs4YT-Qcw6k]

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


Logic To Find First and Second Biggest Number in N Numbers, without using Arrays

First we ask the user to enter length of numbers list. If user enters limit value as 5, then we ask the user to enter 5 numbers. Once the user enters limit value, we iterate the while loop until limit value is 0. For each iteration of while loop we ask the user to enter a integer number. We check if the new number entered by the user is greater than fbig. If true, we swap the value of fbig to sbig and value of num to fbig. If the new number entered by the user is not greater than fbig, then we check if its greater than sbig. If true, we transfer the value of num to sbig.

Once the value of limit is 0, control exits the while loop, and inside fbig we have first biggest number from the list of numbers entered by the user and sbig will have the second biggest number from the list of numbers entered by the user.

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