# C Programming Exercises, Practice, Solution : Array

## C Array [106 exercises with solution]

*[An editor is available at the bottom of the page to write and execute the scripts.]*

**1.** Write a program in C to store elements in an array and print it. Go to the editor

Test Data :

Input 10 elements in the array :

element - 0 : 1

element - 1 : 1

element - 2 : 2

.......

*Expected Output* :

Elements in array are: 1 1 2 3 4 5 6 7 8 9

Click me to see the solution

**2.** Write a program in C to read n number of values in an array and display it in reverse order. Go to the editor

Test Data :

Input the number of elements to store in the array :3

Input 3 number of elements in the array :

element - 0 : 2

element - 1 : 5

element - 2 : 7

*Expected Output* :

The values store into the array are :

2 5 7

The values store into the array in reverse are :

7 5 2

Click me to see the solution

**3.** Write a program in C to find the sum of all elements of the array. Go to the editor

Test Data :

Input the number of elements to be stored in the array :3

Input 3 elements in the array :

element - 0 : 2

element - 1 : 5

element - 2 : 8

*Expected Output* :

Sum of all elements stored in the array is : 15

Click me to see the solution

**4.** Write a program in C to copy the elements of one array into another array. Go to the editor

Test Data :

Input the number of elements to be stored in the array :3

Input 3 elements in the array :

element - 0 : 15

element - 1 : 10

element - 2 : 12

*Expected Output* :

The elements stored in the first array are :

15 10 12

The elements copied into the second array are :

15 10 12

Click me to see the solution

**5.** Write a program in C to count a total number of duplicate elements in an array. Go to the editor

Test Data :

Input the number of elements to be stored in the array :3

Input 3 elements in the array :

element - 0 : 5

element - 1 : 1

element - 2 : 1

*Expected Output* :

Total number of duplicate elements found in the array is : 1

Click me to see the solution

**6.** Write a program in C to print all unique elements in an array. Go to the editor

Test Data :

Print all unique elements of an array:

------------------------------------------

Input the number of elements to be stored in the array: 4

Input 4 elements in the array :

element - 0 : 3

element - 1 : 2

element - 2 : 2

element - 3 : 5

*Expected Output* :

The unique elements found in the array are:

3 5

Click me to see the solution

**7.** Write a program in C to merge two arrays of same size sorted in decending order. Go to the editor

Test Data :

Input the number of elements to be stored in the first array :3

Input 3 elements in the array :

element - 0 : 1

element - 1 : 2

element - 2 : 3

Input the number of elements to be stored in the second array :3

Input 3 elements in the array :

element - 0 : 1

element - 1 : 2

element - 2 : 3

*Expected Output* :

The merged array in decending order is :

3 3 2 2 1 1

Click me to see the solution

**8.** Write a program in C to count the frequency of each element of an array. Go to the editor

Test Data :

Input the number of elements to be stored in the array :3

Input 3 elements in the array :

element - 0 : 25

element - 1 : 12

element - 2 : 43

*Expected Output* :

The frequency of all elements of an array :

25 occurs 1 times

12 occurs 1 times

43 occurs 1 times

Click me to see the solution

**9.** Write a program in C to find the maximum and minimum element in an array. Go to the editor

Test Data :

Input the number of elements to be stored in the array :3

Input 3 elements in the array :

element - 0 : 45

element - 1 : 25

element - 2 : 21

*Expected Output* :

Maximum element is : 45

Minimum element is : 21

Click me to see the solution

**10.** Write a program in C to separate odd and even integers in separate arrays. Go to the editor

Test Data :

Input the number of elements to be stored in the array :5

Input 5 elements in the array :

element - 0 : 25

element - 1 : 47

element - 2 : 42

element - 3 : 56

element - 4 : 32

*Expected Output* :

The Even elements are :

42 56 32

The Odd elements are :

25 47

Click me to see the solution

**11.** Write a program in C to sort elements of array in ascending order. Go to the editor

Test Data :

Input the size of array : 5

Input 5 elements in the array :

element - 0 : 2

element - 1 : 7

element - 2 : 4

element - 3 : 5

element - 4 : 9

*Expected Output* :

Elements of array in sorted ascending order:

2 4 5 7 9

Click me to see the solution

**12.** Write a program in C to sort elements of the array in descending order. Go to the editor

Test Data :

Input the size of array : 3

Input 3 elements in the array :

element - 0 : 5

element - 1 : 9

element - 2 : 1

*Expected Output* :

Elements of the array in sorted descending order:

9 5 1

Click me to see the solution

**13.** Write a program in C to insert New value in the array (sorted list ).. Go to the editor

Test Data :

Insert New value in the sorted array :

-----------------------------------------

Input the size of array : 5

Input 5 elements in the array in ascending order:

element - 0 : 2

element - 1 : 5

element - 2 : 7

element - 3 : 9

element - 4 : 11

Input the value to be inserted : 8

The exist array list is :

2 5 7 9 11

After Insert the list is :

2 5 7 8 9 11

--------------------------------

Process exited after 39.33 seconds with return value 10

Press any key to continue . . .

Click me to see the solution

**14.** Write a program in C to insert New value in the array (unsorted list ). Go to the editor

Test Data :

Input the size of array : 4

Input 4 elements in the array in ascending order:

element - 0 : 1

element - 1 : 8

element - 2 : 7

element - 3 : 10

Input the value to be inserted : 5

Input the Position, where the value to be inserted :2

*Expected Output* :

The current list of the array :

1 8 7 10

After Insert the element the new list is :

1 5 8 7 10

Click me to see the solution

**15.** Write a program in C to delete an element at desired position from an array. Go to the editor

Test Data :

Input the size of array : 5

Input 5 elements in the array in ascending order:

element - 0 : 1

element - 1 : 2

element - 2 : 3

element - 3 : 4

element - 4 : 5

Input the position where to delete: 3

*Expected Output* :

The new list is : 1 2 4 5

Click me to see the solution

**16.** Write a program in C to find the second largest element in an array. Go to the editor

Test Data :

Input the size of array : 5

Input 5 elements in the array :

element - 0 : 2

element - 1 : 9

element - 2 : 1

element - 3 : 4

element - 4 : 6

*Expected Output* :

The Second largest element in the array is : 6

Click me to see the solution

**17.** Write a program in C to find the second smallest element in an array. Go to the editor

Test Data :

Input the size of array : 5

Input 5 elements in the array (value must be <9999) :

element - 0 : 0

element - 1 : 9

element - 2 : 4

element - 3 : 6

element - 4 : 5

*Expected Output* :

The Second smallest element in the array is : 4

Click me to see the solution

**18.** Write a program in C for a 2D array of size 3x3 and print the matrix. Go to the editor

Test Data :

Input elements in the matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [0],[2] : 3

element - [1],[0] : 4

element - [1],[1] : 5

element - [1],[2] : 6

element - [2],[0] : 7

element - [2],[1] : 8

element - [2],[2] : 9

*Expected Output* :

The matrix is :

1 2 3

4 5 6

7 8 9

Click me to see the solution

**19.** Write a program in C for addition of two Matrices of same size. Go to the editor

Test Data :

Input the size of the square matrix (less than 5): 2

Input elements in the first matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [1],[0] : 3

element - [1],[1] : 4

Input elements in the second matrix :

element - [0],[0] : 5

element - [0],[1] : 6

element - [1],[0] : 7

element - [1],[1] : 8

*Expected Output* :

The First matrix is :

1 2

3 4

The Second matrix is :

5 6

7 8

The Addition of two matrix is :

6 8

10 12

Click me to see the solution

**20.** Write a program in C for subtraction of two Matrices. Go to the editor

Test Data :

Input the size of the square matrix (less than 5): 2

Input elements in the first matrix :

element - [0],[0] : 5

element - [0],[1] : 6

element - [1],[0] : 7

element - [1],[1] : 8

Input elements in the second matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [1],[0] : 3

element - [1],[1] : 4

*Expected Output* :

The First matrix is :

5 6

7 8

The Second matrix is :

1 2

3 4

The Subtraction of two matrix is :

4 4

4 4

Click me to see the solution

**21.** Write a program in C for multiplication of two square Matrices. Go to the editor

Test Data :

Input the rows and columns of first matrix : 2 2

Input the rows and columns of second matrix : 2 2

Input elements in the first matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [1],[0] : 3

element - [1],[1] : 4

Input elements in the second matrix :

element - [0],[0] : 5

element - [0],[1] : 6

element - [1],[0] : 7

element - [1],[1] : 8

*Expected Output* :

The First matrix is :

1 2

3 4

The Second matrix is :

5 6

7 8

The multiplication of two matrix is :

19 22

43 50

Click me to see the solution

**22.** Write a program in C to find transpose of a given matrix. Go to the editor

Test Data :

Input the rows and columns of the matrix : 2 2

Input elements in the first matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [1],[0] : 3

element - [1],[1] : 4

*Expected Output* :

The matrix is :

1 2

3 4

The transpose of a matrix is :

1 3

2 4

Click me to see the solution

**23.** Write a program in C to find sum of right diagonals of a matrix.Go to the editor

Test Data :

Input the size of the square matrix : 2

Input elements in the first matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [1],[0] : 3

element - [1],[1] : 4

*Expected Output* :

The matrix is :

1 2

3 4

Addition of the right Diagonal elements is :5

Elements in array are:

Click me to see the solution

**24.** Write a program in C to find the sum of left diagonals of a matrix. Go to the editor

Test Data :

Input the size of the square matrix : 2

Input elements in the first matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [1],[0] : 3

element - [1],[1] : 4

*Expected Output* :

The matrix is :

1 2

3 4

Addition of the left Diagonal elements is :5

Click me to see the solution

**25.** Write a program in C to find sum of rows an columns of a Matrix. Go to the editor

Test Data :

Input the size of the square matrix : 2

Input elements in the first matrix :

element - [0],[0] : 5

element - [0],[1] : 6

element - [1],[0] : 7

element - [1],[1] : 8

*Expected Output* :

The First matrix is :

The matrix is :

5 6

7 8

The sum or rows and columns of the matrix is :

5 6 11

7 8 15

12 14

Click me to see the solution

**26.** Write a program in C to print or display the lower triangular of a given matrix. Go to the editor

Test Data :

Input the size of the square matrix : 3

Input elements in the first matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [0],[2] : 3

element - [1],[0] : 4

element - [1],[1] : 5

element - [1],[2] : 6

element - [2],[0] : 7

element - [2],[1] : 8

element - [2],[2] : 9

*Expected Output* :

The matrix is :

1 2 3

4 5 6

7 8 9

Setting zero in lower triangular matrix

1 2 3

0 5 6

0 0 9

Click me to see the solution

**27.** Write a program in C to print or display upper triangular matrix. Go to the editor

Test Data :

Input the size of the square matrix : 3

Input elements in the first matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [0],[2] : 3

element - [1],[0] : 4

element - [1],[1] : 5

element - [1],[2] : 6

element - [2],[0] : 7

element - [2],[1] : 8

element - [2],[2] : 9

*Expected Output* :

The matrix is :

1 2 3

4 5 6

7 8 9

Setting zero in upper triangular matrix

1 0 0

4 5 0

7 8 9

Click me to see the solution

**28.** Write a program in C to calculate determinant of a 3 x 3 matrix. Go to the editor

Test Data :

Input elements in the first matrix :

element - [0],[0] : 1

element - [0],[1] : 0

element - [0],[2] : -1

element - [1],[0] : 0

element - [1],[1] : 0

element - [1],[2] : 1

element - [2],[0] : -1

element - [2],[1] : -1

element - [2],[2] : 0

*Expected Output* :

The matrix is :

1 0 -1

0 0 1

-1 -1 0

The Determinant of the matrix is: 1

Click me to see the solution

**29.** Write a program in C to accept a matrix and determine whether it is a sparse matrix. Go to the editor

Test Data :

Input the number of rows of the matrix : 2

Input the number of columns of the matrix : 2

Input elements in the first matrix :

element - [0],[0] : 0

element - [0],[1] : 0

element - [1],[0] : 1

element - [1],[1] : 0

*Expected Output* :

The given matrix is sparse matrix.

There are 3 number of zeros in the matrix

Click me to see the solution

**30.** Write a program in C to accept two matrices and check whether they are equal. Go to the editor

Test Data :

Input Rows and Columns of the 1st matrix :2 2

Input Rows and Columns of the 2nd matrix :2 2

Input elements in the first matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [1],[0] : 3

element - [1],[1] : 4

Input elements in the second matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [1],[0] : 3

element - [1],[1] : 4

*Expected Output* :

The first matrix is :

1 2

3 4

The second matrix is :

1 2

3 4

The Matrices can be compared :

Two matrices are equal.

Click me to see the solution

**31.** Write a program in C to check whether a given matrix is an identity matrix. Go to the editor

Test Data :

Input number of Rows for the matrix :3

Input number of Columns for the matrix :3

Input elements in the first matrix :

element - [0],[0] : 1

element - [0],[1] : 0

element - [0],[2] : 0

element - [1],[0] : 0

element - [1],[1] : 1

element - [1],[2] : 0

element - [2],[0] : 0

element - [2],[1] : 0

element - [2],[2] : 1

*Expected Output* :

The matrix is :

1 0 0

0 1 0

0 0 1

The matrix is an identity matrix.

Click me to see the solution

**32.** Write a program in C to find a pair with given sum in the array. Go to the editor

*Expected Output* :

The given array : 6 8 4 -5 7 9

The given sum : 15

Pair of elements can make the given sum by the value of index 0 and 5

Click me to see the solution

**33.** Write a program in C to find the majority element of an array. Go to the editor

A majority element in an array A[] of size n is an element that appears more than n/2 times (and hence there is at most one such element).

*Expected Output* :

The given array is : 4 8 4 6 7 4 4 8

There are no Majority Elements in the given array.

Click me to see the solution

**34.** Write a program in C to find the number occurring odd number of times in an array. Go to the editor

All numbers occur even number of times except one number which occurs odd number of times.

*Expected Output* :

The given array is : 8 3 8 5 4 3 4 3 5

The element odd number of times is : 3

Click me to see the solution

**35.** Write a program in C to find the largest sum of contiguous subarray of an array. Go to the editor

*Expected Output* :

The given array is : 8 3 8 -5 4 3 -4 3 5

The largest sum of contiguous subarray is : 21

Click me to see the solution

**36.** Write a program in C to find the missing number from a given array. There are no duplicates in list. Go to the editor

*Expected Output* :

The given array is : 1 3 4 2 5 6 9 8

The missing number is : 7

Click me to see the solution

**37.** Write a program in C to find the pivot element of a sorted and rotated array using binary search. Go to the editor

Pivot element is the only element in input array which is smaller than it's previous element.

A pivot element divided a sorted rotated array into two monotonically increasing array.

*Expected Output* :

The given array is : 14 23 7 9 3 6 18 22 16 36

The Pivot Element in the array is : 3

Click me to see the solution

**38.** Write a program in C to merge one sorted array into another sorted array. Go to the editor

Pivot element is the only element in input array which is smaller than it's previous element.

A pivot element divided a sorted rotated array into two monotonically increasing array.

*Expected Output* :

The given Large Array is : 10 12 14 16 18 20 22

The given Small Array is : 11 13 15 17 19 21

After merged the new Array is :

10 11 12 13 14 15 16 17 18 19 20 21 22

Click me to see the solution

**39.** Write a program in C to rotate an array by N positions. Go to the editor

*Expected Output* :

The given array is : 0 3 6 9 12 14 18 20 22 25 27

From 4th position the values of the array are : 12 14 18 20 22 25 27

Before 4th position the values of the array are : 0 3 6 9

After rotating from 4th position the array is:

12 14 18 20 22 25 27 0 3 6 9

Click me to see the solution

**40.** Write a program in C to find the ceiling in a sorted array. Go to the editor

N.B.: Given a sorted array in ascending order and a value x, the ceiling of x is the smallest element in array greater than or equal to x,
and the floor is the greatest element smaller than or equal to x.

*Expected Output* :

The given array is : 1 3 4 7 8 9 9 10

The ceiling of 5 is: 7

Click me to see the solution

**41.** Write a program in C to find the Floor and Ceil of the number 0 to 10 from a sroted array. Go to the editor

*Expected Output* :

The given array is : 1 3 5 7 8 9

Number: 0 ceiling is: 1 floor is: -1

Number: 1 ceiling is: 1 floor is: 1

Number: 2 ceiling is: 3 floor is: 1

Number: 3 ceiling is: 3 floor is: 3

Number: 4 ceiling is: 5 floor is: 3

Number: 5 ceiling is: 5 floor is: 5

Number: 6 ceiling is: 7 floor is: 5

Number: 7 ceiling is: 7 floor is: 7

Number: 8 ceiling is: 8 floor is: 8

Number: 9 ceiling is: 9 floor is: 9

Number: 10 ceiling is: -1 floor is: 9

Click me to see the solution

**42.** Write a program in C to find the smallest missing element from a sorted array. Go to the editor

*Expected Output* :

The given array is : 0 1 3 4 5 6 7 9

The missing smallest element is: 2

Click me to see the solution

**43.** Write a program in C to to print next greater elements in a given unsorted array. Elements for which no greater element exist, consider next greater element as -1. Go to the editor

*Expected Output* :

The given array is : 5 3 10 9 6 13

Next Bigger Elements are:

Next bigger element of 5 in the array is: 10

Next bigger element of 3 in the array is: 10

Next bigger element of 10 in the array is: 13

Next bigger element of 9 in the array is: 13

Next bigger element of 6 in the array is: 13

Next bigger element of 13 in the array is: -1

Next Bigger Elements Array:

10 10 13 13 13 -1

Click me to see the solution

**44.** Write a program in C to find the two repeating elements in a given array. Go to the editor

*Expected Output* :

The given array is : 2 7 4 7 8 3 4

The repeating elements are: 7 4

Click me to see the solution

**45.** Write a program in C to find two elements whose sum is closest to zero. Go to the editor

*Expected Output* :

The given array is : 38 44 63 -51 -35 19 84 -69 4 -46

The Pair of elements whose sum is minimum are:

[44, -46]

Click me to see the solution

**46.** Write a program in C to find the smallest positive number missing from an unsorted array. Go to the editor

*Expected Output* :

The given array is : 3 1 4 10 -5 15 2 -10 -20

The smallest positive number missed is: 5

Click me to see the solution

**47.** Write a program in C to find a subarray with given sum from the given array. Go to the editor

*Expected Output* :

The given array is : 3 4 -7 1 3 3 1 -4

[0..1] -- { 3 4 }

[0..5] -- { 3 4 -7 1 3 3 }

[3..5] -- { 1 3 3 }

[4..6] -- { 3 3 1 }

Click me to see the solution

**48.** Write a program in C to find if a given integer x appears more than n/2 times in a sorted array of n integers. Go to the editor

*Expected Output* :

The given array is : 1 3 3 5 4 3 2 3 3

The given value is : 3

3 appears more than 4 times in the given array[]

Click me to see the solution

**49.** Write a program in C to find majority element of an array. Go to the editor

*Expected Output* :

The given array is : 1 3 3 7 4 3 2 3 3

The majority of the Element : 3

Click me to see the solution

**50.** Write a program in C to print a matrix in spiral form. Go to the editor

*Expected Output* :

The given array in matrix form is :

1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

The spiral form of above matrix is:

1 2 3 4 5 10 15 20 19 18 17 16 11 6 7 8 9 14 13 12

Click me to see the solution

**51.** Write a program in C to find the maximum circular subarray sum of a given array. Go to the editor

*Expected Output* :

The given array is : 10 8 -20 5 -3 -5 10 -13 11

The maximum circular sum in the above array is: 29

Click me to see the solution

**52.** Write a program in C to count the number of triangles can be fromed from a given array. Go to the editor

*Expected Output* :

The given array is : 6 18 9 7 10

Number of possible triangles can be formed from the array is: 5

Click me to see the solution

**53.** Write a program in C to find the number of times (frequency) occurs a given number in an array. Go to the editor

*Expected Output* :

The given array is : 2 3 4 4 4 4 5 5 5 6 7 7

The number of times the number 4 occurs in the given array is: 4

Click me to see the solution

**54.** Write a program in C to sort an array of 0s, 1s and 2s. Go to the editor

*Expected Output* :

The given array is : 0 1 2 2 1 0 0 2 0 1 1 0

After sortig the elements in the array are:

0 0 0 0 0 1 1 1 1 2 2 2

Click me to see the solution

**55.** Write a program in C to check whether an array is subset of another array. Go to the editor

*Expected Output* :

The given first array is : 4 8 7 11 6 9 5 0 2

The given second array is : 5 4 2 0 6

The second array is the subset of first array.

Click me to see the solution

**56.** Write a program in C to return the minimum number of jumps to reach the end of the array. Go to the editor

*Expected Output* :

The given array is : 1 3 5 8 9 2 6 7 6 8 9 1 1 1

The minimum of number of jumps is required to reach the end is: 3

Click me to see the solution

**57.** Write a program in C to find minimum element in a sorted and rotated array. Go to the editor

*Expected Output* :

The given array is : 3 4 5 6 7 9 2

The minimum element in the above array is: 2

Click me to see the solution

**58.** Write a program in C to move all zeroes to the end of a given array. Go to the editor

*Expected Output* :

The given array is : 2 5 7 0 4 0 7 -5 8 0

The new array is:

2 5 7 8 4 -5 7 0 0 0

Click me to see the solution

**59.** Write a program in C to return the counting sort on an array. Go to the editor

*Expected Output* :

The given array is : 4 14 8 0 2 5 2 1 0 17 9 0 5

After sorting the elements in the array are: 0 0 0 1 2 2 4 5 5 8 9 14 17

Click me to see the solution

**60.** Write a program in C to find the row with maximum number of 1s. Go to the editor

*Expected Output* :

The given 2D array is :

0 1 0 1 1

1 1 1 1 1

1 0 0 1 0

0 0 0 0 0

1 0 0 0 1

The index of row with maximum 1s is: 1

Click me to see the solution

**61.** Write a program in C to find maximum product subarray in a given array. Go to the editor

*Expected Output* :

The given array is : -4 9 -7 0 -15 6 2 -3

The maximum product of a sub-array in the given array is: 540

Click me to see the solution

**62.** Write a program in C to find the largest subarray with equal number of 0s and 1s. Go to the editor

*Expected Output* :

The given array is : 0 1 0 0 1 1 0 1 1 1

Subarray found from the index 0 to 7

Click me to see the solution

**63.** Write a program in C to replace every element with the greatest element on its right side. Go to the editor

*Expected Output* :

The given array is : 7 5 8 9 6 8 5 7 4 6

After replace the modified array is: 9 9 9 8 8 7 7 6 6 0

Click me to see the solution

**64.** Write a program in C to find the median of two sorted arrays of same size. Go to the editor

*Expected Output* :

The given array - 1 is : 1 5 13 24 35

The given array - 2 is : 3 8 15 17 32

The Median of the 2 sorted arrays is: 14

Click me to see the solution

**65.** Write a program in C to find the product of an array such that product is equal to the product of all the elements of arr[] except arr[i]. Go to the editor

*Expected Output* :

The given array is : 1 2 3 4 5 6

The product array is: 720 360 240 180 144 120

Click me to see the solution

**66.** Write a program in C to count the number of inversion in a given array. Go to the editor

*Expected Output* :

The given array is : 1 9 6 4 5

The inversions are: (9, 6) (9, 4) (9, 5) (6, 4) (6, 5)

The number of inversion can be formed from the array is: 5

Click me to see the solution

**67.** Write a program in C to search an element in a row wise and column wise sorted matrix. Go to the editor

*Expected Output* :

The given array in matrix form is :

15 23 31 39

18 26 36 43

25 28 37 48

30 34 39 50

The given value for searching is: 37

The element Found at the position in the matrix is: 2, 2

Click me to see the solution

**68.** Write a program in C to return maximum sum such that no two elements are adjacent. Go to the editor

*Expected Output* :

The given array is : 1 3 5 9 7 10 1 10 100

The maximum sum from the array such that no two elements are adjacent is: 122

Click me to see the solution

**69.** Write a program in C to find out the maximum difference between any two elements such that larger element appears after the smaller number. Go to the editor

*Expected Output* :

The given array is : 7 9 5 6 13 2

The elements which provide maximum difference is: 5, 13

The Maximum difference between two elements in the array is: 8

Click me to see the solution

**70.** Write a program in C to find two numbers that occur odd number of times in an array. Go to the editor

*Expected Output*:

The given array is: 6 7 3 6 8 7 6 8 3 3

The two numbers occuring odd number of times are: 3 & 6

Click me to see the solution

**71.** Write a program in C to find the median of two sorted arrays of different size. Go to the editor

*Expected Output*:

The given first array is : 90 240 300

The given second array is : 10 13 14 20 25

The median of two different size arrays are : 22.500000

Click me to see the solution

**72.** Write a program in C to return only the unique rows from a given binary matrix. Go to the editor

*Expected Output*:

The given array is :

0 1 0 0 1

1 0 1 1 0

0 1 0 0 1

1 0 1 0 0

The unique rows of the given array are :

0 1 0 0 1

1 0 1 1 0

1 0 1 0 0

Click me to see the solution

**73.** Write a program in C to print all unique elements of an unsorted array. Go to the editor

*Expected Output*:

The given array is : 1 5 8 5 7 3 2 4 1 6 2

Unique Elements in the given array are:

1 5 8 7 3 2 4 6

Click me to see the solution

**74.** Write a program in C to find the sum of upper triangular elements of a matrix. Go to the editor

*Expected Output*:

The given array is :

1 2 3

4 5 6

7 8 9

The elements being summed of the upper triangular matrix are: 2 3 6

The Sum of the upper triangular Matrix Elements are: 11

Click me to see the solution

**75.** Write a program in C to find the sum of lower triangular elements of a matrix. Go to the editor

*Expected Output*:

The given array is :

1 2 3

4 5 6

7 8 9

The elements being summed of the lower triangular matrix are: 4 7 8

The Sum of the lower triangular Matrix Elements are: 19

Click me to see the solution

**76.** Write a program in C to find largest number possible from the set of given numbers. Go to the editor

*Expected Output*:

The given numbers are :

15 628 971 9 2143 12

The largest possible number by the given numbers are: 997162821431512

Click me to see the solution

**77.** Write a program in C to generate a random permutation of array elements. Go to the editor

*Expected Output*:

The given array is:

1 2 3 4 5 6 7 8

The shuffled elements in the array are:

2 8 7 3 4 5 1 6

Click me to see the solution

**78.** Write a program in C to find four array elements whose sum is equal to given number. Go to the editor

*Expected Output*:

The given array is:

3 7 1 9 15 14 6 2 5 7

The elements are:

3, 15, 14, 5

Click me to see the solution

**79.** Write a program in C to sort n numbers in range from 0 to n^2. Go to the editor

*Expected Output*:

The given array is: 37 62 52 7 48 3 15 61

Sorted array is: 3 7 15 37 48 52 61 62

Click me to see the solution

**80.** Write a program in C to count all distinct pairs for a specific difference. Go to the editor

*Expected Output*:

The given array is:

5 2 3 7 6 4 9 8

The distinct pairs for difference 5 are: [7, 2] [8, 3] [9, 4]

Number of distinct pairs for difference 5 are: 3

Click me to see the solution

**81.** Write a program in C to find the maximum repeating number in a given array. Go to the editor

The array range is [0..n-1] and the elements are in the range [0..k-1] and k<=n..

*Expected Output*:

The given array is:

2 3 3 5 3 4 1 7 7 7 7

The maximum repeating number is: 7

Click me to see the solution

**82.** Write a program in C to print all possible combinations of r elements in a given array. Go to the editor

*Expected Output*:

The given array is:

1 5 4 6 8
The combination from by the number of elements are: 4

The combinations are:

1 5 4 6

1 5 4 8

1 5 6 8

1 4 6 8

5 4 6 8

Click me to see the solution

**83.** Write a program in C to find a pair with the given difference. Go to the editor

*Expected Output*:

The given array is:

1 15 39 75 92

The given difference is: 53

The pair are: (39, 92)

Click me to see the solution

**84.** Write a program in C to find the minimum distance between two numbers in a given array. Go to the editor

*Expected Output*:

The given array is:

7 9 5 11 7 4 12 6 2 11

The minimum distance between 7 and 11 is: 1

Click me to see the solution

**85.** Write a program in C to Count all possible paths from top left to bottom right of a m X n matrix.Go to the editor

*Expected Output*:

The size of matrix is : 4 x 4

The all possible paths from top left to bottom right is: 20

Click me to see the solution

**86.** Write a program in C find the equilibrium index of an array. Go to the editor

*Expected Output*:

The given array is:

0 -4 7 -4 -2 6 -3 0

The equilibrium index found at : 7 5 0

Click me to see the solution

**87.** Write a program in C to find the maximum element in an array which is first increasing and then decreasing. Go to the editor

*Expected Output*:

The given array is:

2 7 12 25 4 57 27 44

The maximum element which is increasing then decreasing is: 57

Click me to see the solution

**88.** Write a program in C to find the maximum n – m such that array[n] > array[m] from a given array[]. Go to the editor

Given an array arr[], find the maximum j – i such that arr[j] > arr[i]

*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

Click me to see the solution

**89.** Write a program in C to find maximum size square sub-matrix with all 1s. Go to the editor

*Expected Output*:

The given array in matrix form is :

0 1 0 1 1

1 1 1 1 0

1 1 1 1 0

1 1 1 1 0

1 1 1 1 1

0 1 0 1 0

The maximum size sub-matrix is:

1 1 1 1

1 1 1 1

1 1 1 1

1 1 1 1

Click me to see the solution

**90.** Given an array of size n such that every element is in the range from 0 to n-1. Write a program in C to rearrange the given array so that arr[i] becomes arr[arr[i]]. Go to the editor

*Expected Output*:

The Original array is

2 1 4 3 0

4 1 0 3 2

Click me to see the solution

**91.**Given an unsorted array of specific size. Write a program in C to find the minimum length of subarray such that,

sorting this subarray makes the whole array sorted.Go to the editor

*Expected Output*:

The given array is:

10 12 15 17 28 32 42 18 56 59 67

The minimum length of unsorted subarray which makes the given array sorted

lies between the indeces 4 and 7

Click me to see the solution

**92.** Write a program in C that checks whether the elements in an unsorted array appears consecutively or not. Go to the editor

*Expected Output*:

The given array is:

7 4 3 5 6 2

The appearence of elements in the array are consecutive.

The given array is:

7 4 4 5 6 2

The appearence of elements in the array are not consecutive.

The given array is:

7 4 9 5 6 3

The appearence of elements in the array are not consecutive.

Click me to see the solution

**93.** Write a program in C to rearrange positive and negative numbers alternatively in a given array.Go to the editor

N.B.: If positive numbers are more they appear at the end and for also negative numbers, they too appear in the end of the array.

*Expected Output*:

The given array is:

-4 8 -5 -6 5 -9 7 1 -21 -11 19

The rearranged array is:

-4 7 -5 1 -21 5 -11 8 -9 19 -6

Click me to see the solution

**94.** Write a program in C to find the maximum for each and every contigious subarray of size k from a given array.Go to the editor

*Expected Output*:

The given array is:

1 3 6 21 4 9 12 3 16 10

The length of each subarray is: 4

The contigious subarray of length 4 and their maximum value are:

1 3 6 21 ----> 21

3 6 21 4 ----> 21

6 21 4 9 ----> 21

21 4 9 12 ----> 21

4 9 12 3 ----> 12

9 12 3 16 ----> 16

12 3 16 10 ----> 16

Click me to see the solution

**95.** Write a program in C to segregate 0s and 1s in an array. Go to the editor

*Expected Output*:

The given array is:

1 0 1 0 0 1 0 1 1

The array after segregation is: 0 0 0 0 1 1 1 1 1

Click me to see the solution

**96.** Write a program in C to segregate even and odd elements on an array. Go to the editor

*Expected Output*:

The given array is:

17 42 19 7 27 24 30 54 73

The array after segregation is: 54 42 30 24 27 7 19 17 73

Click me to see the solution

**97.** Write a program in C to find the index of first peak element in a given array. Go to the editor

*Expected Output*:

The given array is:

5 12 13 20 16 19 11 7 25

The index of first peak element in the array is: 3

Click me to see the solution

**98.** Write a program in C to return the largest span found in the leftmost and rightmost appearances of same value(values are inclusive) in a given array. Go to the editor

*Expected Output*:

The given array is:

17 42 19 7 27 24 17 54 73

The span between the same values in the array is: 7

Click me to see the solution

**99.** Write a program in C to check if an array can be splitted in such a position that, the sum of left side of the splitting is equal to the sum of the right side. Go to the editor

*Expected Output*:

The given array is : 1 3 3 8 4 3 2 3 3

The array can be split in a position where the sum of both side are equal.

Click me to see the solution

**100.** Write a program in C to return the number of clumps(a series of 2 or more adjacent elements of the same value) in a given array. Go to the editor

*Expected Output*:

The given array is:

17 42 42 7 24 24 17 54 17

The number of clumps in the array is: 2

Click me to see the solution

**101.** Write a program in C to rearrange an array such that arr[i]=i. Go to the editor

N.B.: Given array contains N elements, from 0 to N – 1. All elements within the range may not be present in the array. There will be -1 if an element within the range is not present in the array.

*Expected Output*:

The given array is:

2 5 -1 6 -1 8 7 -1 9 1

The new array is: -1 1 2 -1 -1 5 6 7 8 9

Click me to see the solution

**102.** Write a program in C to rearrange an array in such an order that– smallest, largest, 2nd smallest, 2nd largest and on. Go to the editor

*Expected Output*:

The given array is:

5 8 1 4 2 9 3 7 6

The new array is:

1 9 2 8 3 7 4 6 5

Click me to see the solution

**103.** Write a program in C to update every array element with multiplication of previous and next numbers in array. Go to the editor

*Expected Output*:

The given array is:

1 2 3 4 5 6

The new array is:

2 3 8 15 24 30

Click me to see the solution

**104.** Write a program in C to rearrange an array such that even index elements are smaller and odd index elements are greater than their next. Go to the editor

*Expected Output*:

The array given is:

6 4 2 1 8 3

The new array after rearranging:

4 6 1 8 2 3

Click me to see the solution

**105.** Write a program in C to find minimum number of swaps required to gather all elements less than or equals to k. Go to the editor

*Expected Output*:

The given array is:

2 7 9 5 8 7 4

The minimum swap required is: 2

Click me to see the solution

**106.** Write a program in C to convert the array in such a way that double its value and replace the next number with 0 if current and next element are same and rearrange the array such that all 0's shifted to the end. Go to the editor

*Expected Output*:

The given array is: 0 3 3 3 0 0 7 7 0 9

The new array is: 6 3 14 9 0 0 0 0 0 0

Click me to see the solution

**C Programming Code Editor:**

**More to Come !**

**Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.**

## C Programming: Tips of the Day

**How to compile and run C/C++ in a Unix console/Mac terminal?**

If it is a simple single source program:

make foo

where the source file is foo.c or foo.cpp, etc.

You dont even need a makefile. Make has enough built-in rules to build your source file into an executable of the same name, minus extension.

Running the executable just built is the same as running any program - but you will most often need to specify the path to the executable as the shell will only search what is in $PATH to find executables, and most often that does not include the current directory (.).

So to run the built executable foo:

./foo

Ref : https://bit.ly/3ebOLB7

**New Content published on w3resource:**- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework