0% found this document useful (0 votes)
3 views4 pages

Day 01

The document presents five algorithmic problems involving arrays. The tasks include finding the kth smallest element, rotating an array, reversing an array, minimizing the height difference of towers after adjustments, and identifying a repeated number in an array without modifying it. Each problem is accompanied by examples and constraints.

Uploaded by

dominionofallies
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Day 01

The document presents five algorithmic problems involving arrays. The tasks include finding the kth smallest element, rotating an array, reversing an array, minimizing the height difference of towers after adjustments, and identifying a repeated number in an array without modifying it. Each problem is accompanied by examples and constraints.

Uploaded by

dominionofallies
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Question 01:-​

Given an array arr[] and an integer k where k is smaller than the size of the array, your task is to
find the kth smallest element in the given array.

Follow up: Don't solve it using the inbuilt sort function.

Examples :

Input: arr[] = [7, 10, 4, 3, 20, 15], k = 3​


Output: 7​
Explanation: 3rd smallest element in the given array is 7.​

Input: arr[] = [2, 3, 1, 20, 15], k = 4​
Output: 15​
Explanation: 4th smallest element in the given array is 15.

Constraints:​
1 <= arr.size <= 106​
1<= arr[i] <= 106​
1 <= k <= n

Question 02:-

Given an array arr, rotate the array by one position in clockwise direction.

Examples:

Input: arr[] = [1, 2, 3, 4, 5]

Output: [5, 1, 2, 3, 4]​


Explanation: If we rotate arr by one position in clockwise 5 come to the front and remaining
those are shifted to the end.

Input: arr[] = [9, 8, 7, 6, 4, 2, 1, 3]

Output: [3, 9, 8, 7, 6, 4, 2, 1]​


Explanation: After rotating clock-wise 3 comes in first position.

Constraints:​
1<=arr.size()<=105​
0<=arr[i]<=105


Question 03:-​
Given an array arr[], the task is to reverse the array. Reversing an array means rearranging the
elements such that the first element becomes the last, the second element becomes second
last and so on.

Examples:

Input: arr[] = {1, 4, 3, 2, 6, 5} ​


Output: {5, 6, 2, 3, 4, 1}​
Explanation: The first element 1 moves to last position, the second element 4 moves to
second-last and so on.

Input: arr[] = {4, 5, 1, 2} ​


Output: {2, 1, 5, 4}​
Explanation: The first element 4 moves to last position, the second element 5 moves to second
last and so on.


Question 04:-​
Given an array arr[] denoting heights of N towers and a positive integer K.

For each tower, you must perform exactly one of the following operations exactly once.

●​ Increase the height of the tower by K

●​ Decrease the height of the tower by K

Find out the minimum possible difference between the height of the shortest and tallest towers
after you have modified each tower.

You can find a slight modification of the problem here.​


Note: It is compulsory to increase or decrease the height by K for each tower. After the
operation, the resultant array should not contain any negative integers.

Examples :

Input: k = 2, arr[] = {1, 5, 8, 10}

Output: 5

Explanation: The array can be modified as {1+k, 5-k, 8-k, 10-k} = {3, 3, 6, 8}.The difference
between the largest and the smallest is 8-3 = 5.

Input: k = 3, arr[] = {3, 9, 12, 16, 20}

Output: 11
Explanation: The array can be modified as {3+k, 9+k, 12-k, 16-k, 20-k} -> {6, 12, 9, 13, 17}.The
difference between the largest and the smallest is 17-6 = 11.

Constraints​
1 ≤ k ≤ 107​
1 ≤ n ≤ 105​
1 ≤ arr[i] ≤ 107

Question 05:-​
Given an array of integers nums containing n + 1 integers where each integer is in the range [1,
n] inclusive.

There is only one repeated number in nums, return this repeated number.

You must solve the problem without modifying the array nums and using only constant extra
space.

Example 1:

Input: nums = [1,3,4,2,2]

Output: 2

Example 2:

Input: nums = [3,1,3,4,2]

Output: 3

Example 3:

Input: nums = [3,3,3,3,3]

Output: 3

Constraints:

●​ 1 <= n <= 105

●​ nums.length == n + 1

●​ 1 <= nums[i] <= n


●​ All the integers in nums appear only once except for precisely one integer which
appears two or more times.

You might also like