Evaluation IV
Evaluation IV
1) Given a sorted array arr[] of distinct integers. Sort the array into a wave-like array(In Place). In
other words, arrange the elements into a sequence such that arr[1] >= arr[2] <= arr[3] >= arr[4] <=
arr[5]..... If there are multiple solutions, find the lexicographical smallest one.
Input:
n = 5
arr[] = {1,2,3,4,5}
Output: 2 1 4 3 5
Explanation:Array elements after sorting them in wave form are 2 1 4 3
5.
Input:
n = 6
arr[] = {2,4,7,8,9,10}
Output: 4 2 8 7 10 9
Explanation: Array elements after sorting them in wave form are 4 2 8 7
10 9.
2) Given an array arr[] of size n. Find the total count of sub-arrays having their sum equal to 0.
Input:
n = 6
arr[] = {0,0,5,5,0,0}
Output: 6
Explanation: The 6 subarrays are [0], [0], [0], [0], [0,0], and [0,0].
Input:
n = 10
arr[] = {6,-1,-3,4,-2,2,4,6,-12,-7}
Output: 4
Explanation:The 4 subarrays are [-1 -3 4] [-2 2], [2 4 6 -12] & [-1 -3 4
-2 2]
3) Given an array arr[], its starting position l and its ending position r. Sort the array using merge
sort algorithm.
Input:
N = 5
arr[] = {4 1 3 9 7}
Output:
1 3 4 7 9
Input:
N = 10
arr[] = {10 9 8 7 6 5 4 3 2 1}
Time : 2 hrs Evaluation IV 02/03/23
Output:
1 2 3 4 5 6 7 8 9 10
4) Given a string S. The task is to print all unique permutations of the given string in
lexicographical sorted order.
Input: ABC
Output:
ABC ACB BAC BCA CAB CBA
Explanation:
Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB
and CBA
Input: ABSG
Output:
ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB
GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA
Explanation: Given string ABSG has 24 permutations.
5)Given an unsorted array, Arr[] of size N and that contains even number of occurrences for all
numbers except two numbers. Find the two numbers in decreasing order which has odd
occurrences.
Input:
N = 8
Arr = {4, 2, 4, 5, 2, 3, 3, 1}
Output: {5, 1}
Explanation: 5 and 1 have odd occurrences.
Input:
N = 8
Arr = {1 7 5 7 5 4 7 4}
Output: {7, 1}
Explanation: 7 and 1 have odd occurrences.