Array Exercises
Array Exercises
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
10. Write a program in C to separate odd and even integers into separate
arrays.
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
13. Write a program in C to insert the values in the array (sorted list).
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 . . .
18. Write a program in C for a 2D array of size 3x3 and print the matrix.
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 :
123
456
789
19. Write a program in C for adding two matrices of the same size.
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 :
12
34
The Second matrix is :
56
78
The Addition of two matrix is :
68
10 12
56
78
The Second matrix is :
12
34
The Subtraction of two matrix is :
44
44
12
34
The Second matrix is :
56
78
The multiplication of two matrix is :
19 22
43 50
12
34
12 14
123
056
009
100
450
789
30. Write a program in C to accept two matrices and check whether they
are equal.
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 :
12
34
The second matrix is :
12
34
The Matrices can be compared :
Two matrices are equal.
32. Write a program in C to find a pair with given sum in the array.
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
37. Write a program in C to find the pivot element of a sorted and rotated
array using binary search.
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
38. Write a program in C to merge one sorted array into another sorted
array.
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
41. Write a program in C to find the Floor and Ceiling of the number 0 to
10 from a sroted array.
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
47. Write a program in C to find a subarray with a given sum from the
given array.
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 }
58. Write a program in C to move all zeroes to the end of a given array.
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
60. Write a program in C to find the row with the maximum number of
1s.
Expected Output :
The given 2D array is :
01011
11111
10010
00000
10001
The index of row with maximum 1s is: 1
64. Write a program in C to find the median of two sorted arrays of the
same size.
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
68. Write a program in C to return the maximum sum such that no two
elements are adjacent.
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
70. Write a program in C to find two numbers that occur an odd number
of times in an array.
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
72. Write a program in C to return only the unique rows from a given
binary matrix.
Expected Output:
The given array is :
01001
10110
01001
10100
The unique rows of the given array are :
01001
10110
10100
75. Write a program in C to find the sum of the lower triangular elements
of a matrix.
Expected Output:
The given array is :
123
456
789
The elements being summed of the lower triangular matrix are: 4 7 8
The Sum of the lower triangular Matrix Elements are: 19
76. Write a program in C to find the largest number possible from the set
of given numbers.
Expected Output:
The given numbers are :
15 628 971 9 2143 12
The largest possible number by the given numbers are:
997162821431512
78. Write a program in C to find four array elements whose sum is equal
to a given number.
Expected Output:
The given array is:
3 7 1 9 15 14 6 2 5 7
The elements are:
3, 15, 14, 5
85. Write a program in C to count all possible paths from top left to
bottom right of a m X n matrix.
Expected Output:
The size of matrix is : 4 x 4
The all possible paths from top left to bottom right is: 20
89. Write a program in C to find the largest square sub-matrix with all
1s.
Expected Output:
The given array in matrix form is :
01011
11110
11110
11110
11111
01010
The maximum size sub-matrix is:
1111
1111
1111
1111
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]].
Expected Output:
The Original array is
2 1 4 3 0 The modified array is:
41032
94. Write a program in C to find the largest element of each and every
contiguous subarray of size k from a given array.
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
97. Write a program in C to find the index of the first peak element in a
given array.
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
98. Write a program in C to return the largest span found in the leftmost
and rightmost appearances of the same value (values are inclusive) in a
given array.
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
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