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 Search A Number And Count Its Occurrence In An Array

Twenty-five numbers are entered from the keyboard into an array. The number to be searched is entered through the keyboard by the user. Write a C program to find if the number to be searched is present in the array and if it is present, display the number of times it appears in the array.

Example: Expected Output

Enter 5 integer numbers
1
5
6
3
5
Enter the number to be searched …
5

5 has appeared at position 2 in the array.
5 has appeared at position 5 in the array.

Final Result: 5 has appeared 2 times in the array.

Visual Representation

search key in array

Video Tutorial: C Program To Search A Number And Count Its Occurrence In An Array


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

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

Source Code: C Program To Search A Number And Count Its Occurrence In An Array

#include<stdio.h>

#define N 5

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

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

    printf("Enter the number to be searched ...\n");
    scanf("%d", &key);

    printf("\n");

    for(i = 0; i < N; i++)
    {
        if(a[i] == key)
        {
          printf("%d has appeared at position %d in the array.\n", key, i + 1);
          count++;
        }
    }

  printf("\nFinal Result: %d has appeared %d times in the array.\n", key, count);

    printf("\n");

    return 0;
}

Output 1:
Enter 5 integer numbers
1
5
9
6
4
Enter the number to be searched …
4

4 has appeared at position 5 in the array.
Final Result: 4 has appeared 1 times in the array.

Output 2:
Enter 5 integer numbers
1
2
3
4
5
Enter the number to be searched …
6

Final Result: 6 has appeared 0 times in the array.

Output 3:
Enter 5 integer numbers
1
5
4
5
2
Enter the number to be searched …
5

5 has appeared at position 2 in the array.
5 has appeared at position 4 in the array.

Final Result: 5 has appeared 2 times in the array.

Logic To Search A Number And Count Its Occurrence In An Array

We ask the user to enter N integer numbers(25 integer numbers according to the problem statement) and store it inside array a[N]. Next we ask the user to input the number to be searched – we store the user input inside variable key.

Inside for loop
We make use of for loop to iterate through entire array. For each iteration we check if the value present at a[i] is equal to value present in key. If it’s true, then we display the position(index value + 1) at which “key” appears and also increment the value of variable count by 1.

After all the iterations of for loop, we display the number of occurrences of key inside the array – value of which is present in variable count.

Explanation With Example

If int a[5] = {1, 5, 6, 3, 5};
key = 5;

ia[i]a[i] == keycount
01FALSE0
15TRUE1
26FALSE1
33FALSE1
45TRUE2

5 has appeared at position 2 in the array.
5 has appeared at position 5 in the array.

Final Result: 5 has appeared 2 times in the array.

Note: We increment the position of key in the array by 1, as users are not used to counting from 0. (Array index starts from 0.)

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 Segregate 0s and 1s In An Array using Swapping Method

Write a C program to segregate 0’s to the left and 1’s to the right of an array using Swapping method.

Related Read:
C Program To Segregate 0s and 1s In An Array using Counting Method

Example: Expected Input/Output

Enter 10 elements(0 or 1)
1
0
1
0
1
0
1
0
1
0

Array after sorting 0’s to left and 1’s to right
0
0
0
0
0
1
1
1
1
1

Visual Representation

Segregate 0s and 1s In An Array

Video Tutorial: C Program To Segregate 0’s and 1’s In An Array using Swapping Method


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

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

Source Code: C Program To Segregate 0’s and 1’s In An Array using Swapping Method

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, left = 0, right = N - 1;

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

        if(a[i] != 0 && a[i] != 1)
        {
            printf("Please enter only 0 and 1 as input\n");
            i--;
        }
    }

    while(left < right)
    {
        while(a[left]   == 0)
            left++;

        while(a[right] == 1)
            right--;

        if(left < right)
        {
            a[left++] = 0;
            a[right--] = 1;
        }
    }

    printf("\nArray after sorting 0's to left and 1's to right\n");
    for(i = 0; i < N; i++)
        printf("%d\n", a[i]);

    printf("\n");

    return 0;
}

Output 1:
Enter 5 elements(0 or 1)
1
0
0
1
0

Array after sorting 0’s to left and 1’s to right
0
0
0
1
1

Output 2:
Enter 5 elements(0 or 1)
2
Please enter only 0’s and 1’s as input
10
Please enter only 0’s and 1’s as input
1
0
2
Please enter only 0’s and 1’s as input
1
1
0

Array after segregating o’s to left and 1’s to right
0
0
1
1
1

Logic To Segregate 0’s and 1’s In An Array using Swapping Method

We ask the user to input N integer numbers and store it inside a[N]. We assign 0(the first index of any array) to variable left and N – 1(the last index of any array) to variable right.

The outer while loop executes until left < right. Inside we write another while loop to check from left, if a[left] is 0. If its already 0, then its already in sorted order, so we don’t swap it. We simply increment the value of left by one position. Control exits this first inner while loop when a[left] is not equal to 0.

Similarly, we check if a[right] is 1. If its 1, we decrement the value of right by 1. When a[right] is not equal to 1, the control exits.

Now variable “left” will have the index position from left where there is number 1. And variable “right” will have the index position from right where there is number 0. Now we need to swap it.

We check if value of left is still less than the value of right. If true, we store 0 at a[left] and 1 at a[right].

Explanation With Example

If a[5] = {0, 1, 1, 0, 1};

lefta[left]righta[right]Swap
0041NO
1130YES
2121NO

a[5] after sorting 0’s to left and 1’s to right:
a[5] = {0, 0, 1, 1, 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 Segregate 0s and 1s In An Array using Counting Method

Write a C program to segregate 0’s to the left and 1’s to the right of an array using counting method.

Related Read:
C Program To Segregate 0s and 1s In An Array using Swapping Method

Example: Expected Input/Output

Enter 10 elements(0 or 1)
1
0
0
1
1
1
1
0
0
1

Array after segregating o’s to left and 1’s to right
0
0
0
0
1
1
1
1
1
1

Visual Representation

Segregate 0s and 1s In An Array

Video Tutorial: C Program To Segregate 0’s and 1’s In An Array using Counting Method


[youtube https://www.youtube.com/watch?v=gHW6WI-5724]

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

Source Code: C Program To Segregate 0’s and 1’s In An Array using Counting Method

#include<stdio.h>

#define N 5

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

    printf("Enter %d elements(0 or 1)\n", N);
    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);
        if(a[i] != 0 && a[i] != 1)
        {
            printf("Please enter only 0's and 1's as input\n");
            i--;
        }
        else if(a[i] == 0)
            count++;
    }

    for(i = 0; i < N; i++)
    {
        if(i < count)
            a[i] = 0;
        else
            a[i] = 1;
    }

    printf("\nArray after segregating o's to left and 1's to right\n");
    for(i = 0; i < N; i++)
        printf("%d\n", a[i]);

    printf("\n");

    return 0;
}

Output 1:
Enter 5 elements(0 or 1)
0
1
1
0
0

Array after segregating o’s to left and 1’s to right
0
0
0
1
1

Output 2:
Enter 5 elements(0 or 1)
2
Please enter only 0’s and 1’s as input
10
Please enter only 0’s and 1’s as input
1
0
2
Please enter only 0’s and 1’s as input
1
1
0

Array after segregating o’s to left and 1’s to right
0
0
1
1
1

Logic To Segregate 0’s and 1’s In An Array using Counting Method

We ask the user to input N integer numbers and store it inside a[N]. Next we iterate through each element of the array and if the user input is 0, then we increment the value of variable count by 1. After completing the iterations, variable count will have the number of 0’s input by the user.

Modifying the Array
Since user is allowed to either enter 0 or 1. So if we know the number of 0’s input by the user, we can easily calculate the number of 1’s in the array, using the formula:

N – count_0 = count_1
where, N is array size.

Now that we know the exact number of 0’s and 1’s input by the user, we re-enter the 0’s and 1’s as needed by the problem statement. That is, we arrange all the 0’s to left and 1’s to the right.

Number of 0’s plus the number of 1’s should be equal to the array size.
count_0 + count_1 = N;

Explanation With Example

If a[5] = {0, 1, 1, 0, 1};

ia[i]count
001
11
21
302
41

count = 2;
That is, the number of 0’s in user input array is 2. So the number of 1’s is:
array_size – count
5 – 2 = 3
So there are three 1’s and two 0’s.

    for(i = 0; i < N; i++)
    {
        if(i < count)
            a[i] = 0;
        else
            a[i] = 1;
    }

Once we have this exact data. We can arrange Two 0’s from left of array, i.e., a[0] and a[1]. And three 1’s from a[2], a[3] and a[4].

This is how we segregate 0’s to left and 1’s to the right of an array, using counting method.

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 Count and Display Even and Odd Elements of An Array

Lets write a C program to display even and odd elements of an array, along with their count.

Related Read:
C Program To Count Number of Even, Odd and Zeros In An Array

How To Determine Even and Odd Numbers?

An even number is an integer that is exactly divisible by 2.
Ex: num % 2 == 0

An odd number is an integer that is not exactly divisible by 2.
Ex: num % 2 != 0

Example: Expected Input/Output

Enter 10 integer numbers
1
2
3
4
5
6
7
8
9
10

Even numbers in the array are …
2
4
6
8
10

Odd numbers in the array are …
1
3
5
7
9

Total Even numbers: 5
Total Odd numbers: 5

Visual Representation

count and display even and odd elements of array

Video Tutorial: C program To Count and Display Even and Odd Elements of An Array


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

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

Source Code: C program To Count and Display Even and Odd Elements of An Array

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, even = 0, odd = 0;

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

    printf("\n\nEven numbers in the array are ...\n");
    for(i = 0; i < N; i++)
    {
        if(a[i] % 2 == 0)
        {
            printf("%d\n", a[i]);
            even++;
        }
    }

    printf("\nOdd numbers in the array are ...\n");
    for(i = 0; i < N; i++)
    {
        if(a[i] % 2 != 0)
        {
            printf("%d\n", a[i]);
            odd++;
        }
    }

    printf("\n\nTotal Even numbers: %d", even);
    printf("\nTotal Odd numbers: %d\n", odd);

    printf("\n");

    return 0;
}

Output 1:
Enter 10 integer numbers
10
11
12
13
14
15
16
17
18
19

Even numbers in the array are …
10
12
14
16
18

Odd numbers in the array are …
11
13
15
17
19

Total Even numbers: 5
Total Odd numbers: 5

Output 2:
Enter 10 integer numbers
65
12
4
7
87
2
36
45
78
98

Even numbers in the array are …
12
4
2
36
78
98

Odd numbers in the array are …
65
7
87
45

Total Even numbers: 6
Total Odd numbers: 4

Logic To Count and Display Even and Odd Elements of An Array

We ask the user to enter N integer number and store it in array variable a[N]. We iterate or loop through the user input array elements one by one and check if the selected element is perfectly divisible by 2. If true, then its even number, so we display the number/element and keep count of number of even numbers.

Similarly, we loop through the user input array elements one by one once again, and check if the selected element is not perfectly divisible by 2. If true, then its odd number. So we display the odd number and keep the count of number of odd elements.

Once both the iterations are complete, we display the number of even and odd elements count which we kept track of in above two iterations.

Note: We’re considering 0 as even number, as it has odd elements on either side of it. And 0 is perfectly divisible by 2.
scale

Explanation With Example

If a[5] = {10, 11, 12, 13, 14};
For Even Numbers

    even = 0;
    for(i = 0; i < N; i++)
    {
        if(a[i] % 2 == 0)
        {
            printf("%d\n", a[i]);
            even++;
        }
    }
ia[i]a[i] % 2 == 0ResultDisplayCount
0a[0] = 1010 % 2TRUE101
1a[1] = 1111 % 2FALSE
2a[2] = 1212 % 2TRUE122
3a[3] = 1313 % 2FALSE
4a[4] = 1414 % 2TRUE143

Even numbers displayed:
10
12
14
Count: 3

For Odd Numbers

    odd = 0;
    for(i = 0; i < N; i++)
    {
        if(a[i] % 2 != 0)
        {
            printf("%d\n", a[i]);
            odd++;
        }
    }
ia[i]a[i] % 2 != 0ResultDisplayCount
0a[0] = 1010 % 2FALSE
1a[1] = 1111 % 2TRUE111
2a[2] = 1212 % 2FALSE
3a[3] = 1313 % 2TRUE132
4a[4] = 1414 % 2FALSE

Odd numbers displayed:
11
13
Count: 2

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