Sample Programming Questions - SET 2
Sample Programming Questions - SET 2
Input: 5
Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
2. Write a program to traverse an array from left to right and replace each number with
the next greater number present within the remaining elements from its position. For
example, from (4, 9, 23, 7) the next greater number to 4 is 7. If no such number is
found, then replace the remaining array elements with -1.
Examples:
Input: 2,5,7
Output: 5,7,-1
Input: 2,4,8,90,77,54
Output: 4,8,54,-1,-1,-1
Input: 2,-1,0,-1,3
Output: 3,0,3,3,-1
3. From the given unsorted integer array, find the pair(s) of elements that have the
smallest absolute difference between them. If there are multiple such pairs then find
them all.
Examples:
Input: [20, 30, 120, 240]
Output: [20, 30]
Input: [5, 4, 3, 2]
Output: [2, 3], [3, 4], [4, 5]
Input: [11, 15, 24, 9]
Output: [11, 9]