Arrays PSDL
Arrays PSDL
Arrays PSDL
ARRAYS:
1. Write a program which takes size of array and elements of array as input, then find
sum of elements, average of elements, maximum and minimum element of a given
array.
Sample Input:
6
10 19 7 25 12 32
Output: 105 17.5 32 7
2. Write a program to find sum of elements of a given array excluding maximum and
minimum number.
Sample Input 1:
10
1234567899
Output:
35
Sample Input 2:
5
22222
Output:
0
3. Write a program to find the sum of even numbers from a given array.
Sample Input:
10
7 2 9 8 4 6 10 13 12 11
Output:
42
4. Write a program to search an element from a given array and print the index of the
element as output. If the element is found more than once print the index of last
instance of the element. The program takes input as size of array, elements of array
and the element to be searched.
Sample input 1:
8
41639852
9
Output:
4
Sample input 2:
8
41639892
9
Output:
6
Sample input 3:
8
41639852
7
Output:
-1
5. Write a program to find first 2 maximum and first 2 minimum elements of a given
array of size 9. Read the elements of the array using command line arguments
Note:
Do not sort the given array.
The array size should always be 9.
Sample Input 1:
12 100 65 97 13 124 9 234 76
Output:
234 124
9 12
Sample Input 2:
12 7 18
Output:
9 elements are required
Sample Input:
4 9 1 10 8 7 2 5
Output:
5 2 7 8 10 1 9 4
7. Write a program to remove duplicate elements from a given array and print the same.
Sample Input:
12 4 6 12 8 9 4 5
Output:
12 4 6 8 9 5
8. Write a program to print unique elements of a given array.
Sample Input:
12 4 6 12 8 9 4 5
Output:
6895
9. Write a program to rotate the elements of a given array by n bits. Input to the program
is size of array, no of bits to be rotated and elements of the array.
Sample Input 1:
51
12345
Output:
23451
Sample Input 2:
54
12345
Output:
51234
Sample Input 1:
8
12345678
Output:
21436587
Sample Input 2:
9
123456789
Output:
214365879
11. Write a program which takes comma separated array elements as input, then sort the
elements in descending order and delete the middle element of the array and print
the output array.
Sample Input 1:
6,1,4,10,3,12,15,8,12
Output:
15,12,12,10,6,4,3,1
Sample Input 2:
7,5,9,2,18,10,3,21
Output:
21,18,10,5,3,2
Sample Input 3:
10
Output:
No elements to display.
Sample Input 4:
12,10
Output:
No elements to display.
12. Write a program to print the sum of the elements of an array following the given
below condition. If the array has 6 and 7 in succeeding orders, ignore the numbers
between 6 and 7 and consider the other numbers for calculation of sum.
Sample Input 1:
7
10 3 6 1 2 7 9
Output: [10+3+9]
22
Sample Input 2:
5
71236
Output:
19
Sample Input 1:
5
16479
Output:
10
13. Write a program which takes input as size of array and elements of array. Then print
the maximum number which is present to the right of given element for every
element. As the last number do not have any elements to right print -1.
Sample input 1:
5
12 5 7 6 2
Output:
7 7 6 2 -1
Sample input 2:
5
89622
Output:
9 6 2 2 -1
14. Write a program to read size and elements of array, then if the elements are in
increasing order like 1,2,3,4…….. till a number n (size of the array) then print “YES”
else print “NO”.
Note: Try this program with and without sorting the given array.
Sample Input 1:
6
341652
Output:
YES
Sample Input 2:
6
341672
Output:
NO
15. Write a program to read size and elements of array as input and print the maximum
possible sum of continuous subset of given array elements.
Sample Input 1:
5
2 -4 3 7 5
Output: [3+7+5]
15
Sample Input 2:
7
1 4 -1 2 5 6 -3
Output: [1+4-1+2+5+6]
17