Skip to content

Latest commit

 

History

History
129 lines (90 loc) · 4.33 KB

Data structure questions.md

File metadata and controls

129 lines (90 loc) · 4.33 KB

Given an array of positive integers arr[], return the second largest element from the array. If the second largest element doesn't exist then return -1.

Note: The second largest element should not be equal to the largest element.

Examples:

Input: arr[] = [12, 35, 1, 10, 34, 1] Output: 34 Explanation: The largest element of the array is 35 and the second largest element is 34.

Input: arr[] = [10, 5, 10] Output: 5 Explanation: The largest element of the array is 10 and the second largest element is 5.

Input: arr[] = [10, 10, 10] Output: -1 Explanation: The largest element of the array is 10 and the second largest element does not exist.


Given an array arr[]. Push all the zeros of the given array to the right end of the array while maintaining the order of non-zero elements. Do the mentioned change in the array in place.

Examples:

Input: arr[] = [1, 2, 0, 4, 3, 0, 5, 0] Output: [1, 2, 4, 3, 5, 0, 0, 0] Explanation: There are three 0s that are moved to the end.

Input: arr[] = [10, 20, 30] Output: [10, 20, 30] Explanation: No change in array as there are no 0s.

Input: arr[] = [0, 0] Output: [0, 0] Explanation: No change in array as there are all 0s.

Constraints:
1 ≤ arr.size() ≤ 105
0 ≤ arr[i] ≤ 105


You are given an array of integers arr[]. Your task is to reverse the given array.

**Examples:
**

Input: arr = [1, 4, 3, 2, 6, 5] Output: [5, 6, 2, 3, 4, 1]
Explanation: The elements of the array are 1 4 3 2 6 5. After reversing the array, the first element goes to the last position, the second element goes to the second last position and so on. Hence, the answer is 5 6 2 3 4 1.

Input: arr = [4, 5, 2] Output: [2, 5, 4]
Explanation: The elements of the array are 4 5 2. The reversed array will be 2 5 4.

Input: arr = [1] Output: [1]
Explanation: The array has only single element, hence the reversed array is same as the original.

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


Given an unsorted array arr[]__. Rotate the array to the left (counter-clockwise direction) by d steps, where d is a positive integer. Do the mentioned change in the array in place.

Note: Consider the array as circular.

**Examples :
**

Input: arr[] = [1, 2, 3, 4, 5], d = 2 Output: [3, 4, 5, 1, 2] Explanation: when rotated by 2 elements, it becomes 3 4 5 1 2.

Input: arr[] = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20], d = 3 Output: [8, 10, 12, 14, 16, 18, 20, 2, 4, 6] Explanation: when rotated by 3 elements, it becomes 8 10 12 14 16 18 20 2 4 6.

Input: arr[] = [7, 3, 9, 1], d = 9 Output: [3, 9, 1, 7] Explanation: when we rotate 9 times, we'll get 3 9 1 7 as resultant array.

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


Given an array of integers arr[] representing a permutation, implement the next permutation that rearranges the numbers into the lexicographically next greater permutation. If no such permutation exists, rearrange the numbers into the lowest possible order (i.e., sorted in ascending order). 

Note - A permutation of an array of integers refers to a specific arrangement of its elements in a sequence or linear order.

Examples:

Input: arr = [2, 4, 1, 7, 5, 0] Output: [2, 4, 5, 0, 1, 7] Explanation: The next permutation of the given array is {2, 4, 5, 0, 1, 7}.

Input: arr = [3, 2, 1] Output: [1, 2, 3] Explanation: As arr[] is the last permutation, the next permutation is the lowest one.

Input: arr = [3, 4, 2, 5, 1] Output: [3, 4, 5, 1, 2] Explanation: The next permutation of the given array is {3, 4, 5, 1, 2}.

Constraints:
1 ≤ arr.size() ≤ 105
0 ≤ arr[i] ≤ 105


You are given an array of integer arr[] where each number represents a vote to a candidate. Return the candidates that have votes greater than one-third of the total votes, If there's not a majority vote, return an empty array. 

Note: The answer should be returned in an increasing format.

Examples:

Input: arr[] = [2, 1, 5, 5, 5, 5, 6, 6, 6, 6, 6] Output: [5, 6] Explanation: 5 and 6 occur more n/3 times.

Input: arr[] = [1, 2, 3, 4, 5] Output: []
Explanation: o candidate occur more than n/3 times.

Constraint:
1 <= arr.size() <= 106
-109 <= arr[i] <= 109