Coding Problems On Arrays & Searching
Coding Problems On Arrays & Searching
Input:
• An integer N representing the size of the array.
• An array of integers nums of size N.
• An integer k representing how many top frequent elements to return.
Output:
• A list of k integers, representing the top k most frequent elements, in order of frequency and value as
per the rules.
Example:
Input:
N=6
nums = [1, 1, 1, 2, 2, 3]
k=2
Output:
12
Explanation:
- Frequency of 1 = 3
- Frequency of 2 = 2
- Frequency of 3 = 1
Constraints:
• 1 ≤ N ≤ 25
• 1 ≤ nums[i] ≤ 100
• 1≤k≤4
Output:
• An integer representing the best partition index i that gives the minimum absolute difference
between the sum of the two parts.
Example:
Input:
arr = [1, 2, 3, 4, 5]
Output:
2
Explanation:
Total sum = 15
Constraints:
• 2 ≤ [Link] ≤ 100,000
• -10⁴ ≤ arr[i] ≤ 10⁴
Input:
• An integer N — the size of the array
• An array A of N integers
• An integer K — the maximum allowed index distance for the swap
Output:
• The modified array after performing at most one valid swap that yields the lexicographically smallest
result.
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Example:
Input:
N=5
A = [5, 4, 3, 2, 1]
K=3
Output:
2
4
3
5
1
Explanation:
- To make the array smallest lexicographically, look for the smallest possible element within a K-range.
- Swapping A[0] = 5 and A[3] = 2 (|0 - 3| = 3 ≤ K) gives [2, 4, 3, 5, 1], which is lexicographically smaller than any
other swap.
Constraints:
• 1 ≤ N ≤ 10⁵
• 1 ≤ A[i] ≤ 10⁹
• 1≤K≤N
Input:
• Four integers:
o H = Height of the blanket
o W = Width of the blanket
o H1 = Height of the box
o W1 = Width of the box
Output:
• An integer representing the minimum number of folds required.
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Example:
Input:
H = 2, W = 3
H1 = 2, W1 = 2
Output:
1
Explanation:
- The blanket is 2 x 3 and the box is 2 x 2.
- Only one fold needed: fold the width 3 to 2 (or less).
Constraints:
• 1 ≤ H, W, H1, W1 ≤ 10⁹
Input:
• An integer array arr of length n
(1 ≤ n ≤ 10⁵, 1 ≤ arr[i] ≤ 10⁴)
Output:
• An integer — the minimum number of largest elements whose total sum is greater than the sum
of the rest.
Example:
Input:
arr = [2, 17, 7, 3]
Output:
1
Explanation:
- Total sum = 29
- Sorting in descending order: [17, 7, 3, 2]
- Take 17 → sum = 17, remaining = 12 → 17 > 12 → Done
- So, minimum elements needed = 1
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Constraints:
• 1 ≤ [Link] ≤ 10⁵
• 1 ≤ arr[i] ≤ 10⁴
Input:
• A list of operations and a list of input values for those operations.
Output:
• A list where each element corresponds to the return value of the respective operation. Use null for
the constructor.
Example:
Input:
operations = ["StockSpanner", "next", "next", "next", "next", "next", "next", "next"]
values = [[], [100], [80], [60], [70], [60], [75], [85]]
Output:
[null, 1, 1, 1, 2, 1, 4, 6]
Explanation:
- Day 1: price = 100 → no previous greater → span = 1
- Day 2: price = 80 → last price > 80 is 100 → span = 1
- Day 3: price = 60 → last price > 60 is 80 → span = 1
- Day 4: price = 70 → last price > 70 is 80 → [60,70] → span = 2
- Day 5: price = 60 → span = 1
- Day 6: price = 75 → [60,70,75] → span = 4
- Day 7: price = 85 → [60,70,75,85] → span = 6
Constraints:
• 1 <= price <= 10⁵
• At most 10⁴ calls to next will be made.
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Input Format:
• First line contains a single integer N — the size of the array.
• Second line contains N space-separated integers — the elements of the array arr.
Output Format:
• Print a single integer — the total number of inversions in the array.
Constraints:
• 1 ≤ N ≤ 5 × 10⁵
• 1 ≤ arr[i] ≤ 10¹⁸
Sample Input:
5
24135
Sample Output:
3
Explanation:
The 3 inversions are:
• (0, 2) → 2 > 1
• (1, 2) → 4 > 1
• (1, 3) → 4 > 3
Input Format:
• First line: Integer n — the range of numbers (from 1 to n)
• Second line: Integer k — the 1-based index of the desired permutation
Output Format:
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Constraints:
• 1≤n≤9
• 1 ≤ k ≤ n!
Sample Input:
3
3
Sample Output:
213
Explanation:
All permutations of [1, 2, 3] in lexicographic order are:
1. 123
2. 132
3. 213 ← this is the 3rd permutation
4. 231
5. 312
6. 321
So, the output is 213.
Input Format:
• First line: An integer E — initial energy.
• Second line: An integer N — number of exercises.
• Next N lines: Each line contains one integer A[i] — energy drain of the i-th exercise.
Output Format:
• A single integer — the minimum number of exercises needed to exhaust the energy, or -1 if it's not
possible.
Constraints:
• 1 < E < 10⁵
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Sample Input:
6
2
1
2
Sample Output:
4
Explanation:
• You can perform:
o Exercise 1 (energy = 1) two times → total = 2
o Exercise 2 (energy = 2) two times → total = 4
o Total energy drained = 1 + 1 + 2 + 2 = 6
• Energy becomes 0 → Done in 4 exercises
Input Format:
• First line: An integer N — the number of elements in the array.
• Next N lines: Each line contains one integer, the i-th element of the array.
Output Format:
• A single integer — the minimum number of changes needed to convert the array into a mountain.
Constraints:
• 1 < N < 10⁵
• 1 < array[i] < 10⁶
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Sample Input:
6
3
3
4
4
5
5
Sample Output:
3
Explanation:
We can convert the array [3, 3, 4, 4, 5, 5] to a mountain like [3, 4, 5, 4, 3, 2] or [1, 2, 3, 4, 3, 2], but in this case,
the fewest changes result in a minimum of 3 modifications.
Input Format:
• First line: Integer N — the number of villages.
• Second line: N space-separated integers — the wealth of the villages.
• Third line: Integer K — the size of the group of contiguous villages to consider.
Output Format:
• A single integer — the smallest possible maximum difference in wealth among all contiguous
subarrays of size K.
Constraints:
• 1 ≤ N ≤ 10⁵
• −10⁶ ≤ arr[i] ≤ 10⁶
• 1≤K≤N
Sample Input:
6
839165
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
3
Sample Output:
5
Explanation:
All contiguous subarrays of size 3 and their max-min differences:
• [8, 3, 9] → max = 9, min = 3 → diff = 6
• [3, 9, 1] → max = 9, min = 1 → diff = 8
• [9, 1, 6] → max = 9, min = 1 → diff = 8
• [1, 6, 5] → max = 6, min = 1 → diff = 5
Minimum of all differences = 5
Input Format:
• First line: Two space-separated integers n and k — the number of days and the streak length.
• Second line: n space-separated integers — the energy values of the smoothies on each day.
Output Format:
• A single integer — the maximum total energy of any contiguous subarray of size k.
Constraints:
• 1 ≤ n ≤ 10⁵
• −10⁶ ≤ energy[i] ≤ 10⁶
Sample Input:
62
-1 -2 -3 4 5 6
Sample Output:
11
Explanation:
All subarrays of size 2:
• [-1, -2] → sum = -3
• [-2, -3] → sum = -5
• [-3, 4] → sum = 1
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
• [4, 5] → sum = 9
• [5, 6] → sum = 11 ← maximum
So, the answer is 11.
Input Format:
• First line: Two integers N and K
• Next N lines: Each line contains N space-separated integers — the matrix elements
Output Format:
• A single integer — the sum of elements from the three distinct regions
Constraints:
• 1 ≤ K ≤ N ≤ 100
• −10³ ≤ mat[i][j] ≤ 1
Sample Input:
53
11111
11111
11111
11111
11111
Sample Output:
39
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Input Format:
• First line: Two space-separated integers n and k
• Second line: n space-separated integers representing the array
Output Format:
• A single integer: the minimum possible maximum subarray sum
Constraints:
• 1 ≤ n ≤ 10⁴
• 1 ≤ array[i] ≤ 10⁵
Sample Input:
52
7 2 5 10 8
Sample Output:
18
Explanation:
Split the array as [7,2,5] and [10,8] → subarray sums: 14 and 18.
The maximum of these is 18, which is minimized.
Input Format:
• First line: Integer n — number of intervals
• Next n lines: Each line contains three integers: start, end, and value
Output Format:
• A single integer: Maximum total value of the selected non-overlapping intervals
Constraints:
• 1 ≤ n ≤ 10⁵
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Sample Input:
4
1 3 50
3 5 20
6 19 100
2 100 200
Sample Output:
200
Explanation:
• Intervals [1,3] and [6,19] are non-overlapping → total = 150
• But interval [2,100] has value 200 → better to pick it alone
Input Format:
• First line: Integer n — number of elements in the array
• Second line: n space-separated integers representing the array
Output Format:
• A single integer: Minimum number of swaps required
Constraints:
• 1 ≤ n ≤ 10
• 0 ≤ arr[i] ≤ 100
Example 1:
Input:
4
4321
Output:
2
Explanation:
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Example 2:
Input:
5
10 19 6 3 5
Output:
2
Explanation:
• Swap 10 with 3 → [3,19,6,10,5]
• Swap 19 with 5 → [3,5,6,10,19]
Input Format:
• First line: Integer n — number of reservoirs
• Second line: n space-separated integers H[0] H[1] ... H[n-1]
Output Format:
• A single integer: minimum total energy required to make all heights equal
Constraints:
• 1 ≤ n ≤ 10⁵
• 1 ≤ H[i] ≤ 10⁹
Examples:
Input:
4
8363
Output:
11
Explanation:
Reduce all to height 3: (8−3) + (6−3) = 5 + 3 = 8
3 and 3 need no change.
Input:
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
3
100 100 100
Output:
0
Input Format:
• First line: Integer n — number of fighters
• Second line: n space-separated integers — power of each fighter
• Third line: Integer q — number of rounds
• Next q lines: Each contains an integer M — Ram’s power in that round
Output Format:
• For each query, print two integers:
number_of_defeated_fighters total_power_of_defeated_fighters
Constraints:
• 1 ≤ n ≤ 10,000
• 1 ≤ fighter power ≤ 100
• 1 ≤ q ≤ 10,000
• 1 ≤ M ≤ 100
Example Input:
7
1234567
3
3
10
2
Example Output:
36
7 28
23
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Input Format:
• First line: Integer n — size of array
• Second line: n space-separated integers — array elements
Output Format:
• A single integer — the maximum subarray sum
Constraints:
• 1 ≤ n ≤ 20
• −100 ≤ nums[i] ≤ 100
Sample Input:
5
5 4 -1 7 8
Sample Output:
23
Input Format:
• First line: Integer n — number of elements
• Second line: n space-separated integers
Output Format:
• A single integer — the maximum sum of a valid alternating subsequence
Constraints:
• 1 ≤ n ≤ 10
• 1 ≤ elements ≤ 100
Sample Input:
6
438538
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Sample Output:
28
Input Format:
• First line: Integer N — size of the array
• Second line: N space-separated integers
Output Format:
• A single integer — minimum number of changes required
Input Format:
• First line: An array of integers prices[]
• Second line: Integer k
Output Format:
• A single integer: the maximum profit
Constraints:
• 1 ≤ [Link] ≤ 1000
• 1 ≤ k ≤ 200
• 1 ≤ prices[i] ≤ 1000
Sample Input:
prices = [10, 22, 5, 80]
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
k=2
Sample Output:
87
Input Format:
• First line: Two integers N and Q
• Second line: N space-separated integers
• Next Q lines: Each contains two integers L and R
Output Format:
• For each query, print the number of distinct elements in the subarray [L, R]
Constraints:
• 1 ≤ N, Q ≤ 2 × 10⁵
• 1 ≤ A[i] ≤ 10⁹
• 1≤L≤R≤N
Sample Input:
53
12132
15
24
33
Sample Output:
3
2
1
Input Format:
• Two lines, each containing space-separated integers of arrays A and B
Output Format:
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Constraints:
• 1 ≤ |A|, |B| ≤ 10⁶
Sample Input:
A = [1 2 3 3 4 5 6]
B = [3 3 5]
Sample Output:
335
Input Format:
• First line: Integer n — number of cubes
• Second line: n space-separated integers — sizes of the cubes
Output Format:
• A single integer — minimum number of towers needed
Constraints:
• 1 ≤ n ≤ 10⁵
Sample Input:
5
38215
Sample Output:
2
Input Format:
• First line: Integer N — number of elements
• Second line: N space-separated integers
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Output Format:
• A single integer — the maximum element
Constraints:
• 1 ≤ N ≤ 1000
• −10⁹ ≤ arr[i] ≤ 10⁹
Sample Input:
5
-2 -19 8 15 4
Sample Output:
15
Input Format:
• First line: Integer N, the number of elements in the array
• Second line: N space-separated integers
Output Format:
• Print three integers separated by a space:
o Number of zeros
o Number of positive integers
o Number of negative integers
Constraints:
• 1 ≤ N ≤ 10⁴
• −10³ ≤ arr[i] ≤ 10³
Sample Input:
10
120 0 -9 89 68 -982 91 -54 -12 -139
Sample Output:
145
Input Format:
• First line: Integer N, number of elements
• Second line: N space-separated integers
Output Format:
• Print the array in reverse order, space-separated
Constraints:
• 1 ≤ N ≤ 100
• 0 ≤ arr[i] ≤ 1000
Sample Input:
5
2 19 8 15 4
Sample Output:
4 15 8 19 2
Input Format:
• First line: Integer N, number of elements
• Second line: N space-separated integers
Output Format:
• Two integers: sum of odd numbers and sum of even numbers
Constraints:
• 1 ≤ N ≤ 10³
• 1 ≤ arr[i] ≤ 10⁶
Sample Input:
5
46925
Sample Output:
14 12
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Input Format:
• First line: Integer N
• Second line: N sorted integers
Output Format:
• Three values on one line:
o Mean (rounded to 2 decimal places)
o Median (rounded to 2 decimal places if needed)
o Mode (as integer)
Constraints:
• 1 ≤ N ≤ 10⁴
• 0 ≤ A[i] ≤ 100
Sample Input:
8
12345566
Sample Output:
4.00 4.50 5
Input Format:
• One line: 99 space-separated integers
Output Format:
• Print the missing number
Constraints:
• Each number is between 1 and 100 (inclusive)
• No duplicates
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Input Format:
• First line: Integer N
• Second line: N space-separated integers
Output Format:
• One integer: the duplicate number
Constraints:
• 2 ≤ N ≤ 100
• 0 ≤ arr[i] ≤ 10⁹
• Only one duplicate exists
Sample Input:
6
5 4 10 9 21 10
Sample Output:
10
Input Format:
• First line: Integer N
• Second line: N space-separated integers
• Third line: Integer X
Output Format:
• Two integers: first and last index of X (0-based)
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Constraints:
• 1 ≤ N ≤ 10³
• 1 ≤ A[i] ≤ 10⁵
• X ∈ A (X is guaranteed to be present at least once)
Sample Input:
10
1 3 5 7 9 11 3 13 15 3
3
Sample Output:
19
Input Format:
• First line: Integer N
• Second line: N space-separated integers
Output Format:
• Space-separated unique elements in the order they appear
Constraints:
• 1 ≤ N ≤ 100
• 0 ≤ arr[i] ≤ 10⁹
Sample Input:
7
5 4 10 9 21 4 10
Sample Output:
5 9 21
Input Format:
• First line: Integer n (size of array A)
• Second line: n integers
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Output Format:
• A single sorted array (space-separated)
Constraints:
• 1 ≤ n, m ≤ 10⁴
• -10⁹ ≤ A[i], B[i] ≤ 10⁹
Sample Input:
5
13579
4
2468
Sample Output:
123456789
Input Format:
• First line: Integer n (size of A)
• Second line: n space-separated integers (A)
• Third line: Integer m (size of B)
• Fourth line: m space-separated integers (B)
Output Format:
• m space-separated integers as described above
Constraints:
• 1 ≤ n, m ≤ 100
• 0 ≤ A[i], B[i] ≤ 1000
Sample Input:
5
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Input Format:
• First line: Integer n, the number of altitude changes
• Second line: n space-separated integers representing the changes
Output Format:
• Single integer: the maximum altitude reached
Constraints:
• 1 ≤ n ≤ 1000
• −10⁴ ≤ change[i] ≤ 10⁴
Sample Input:
5
-5 1 5 0 -7
Sample Output:
1
Input Format:
• First line: Integer n
• Second line: n space-separated binary integers (0 or 1)
Output Format:
• Single integer: the maximum length of contiguous 1's
Constraints:
• 1 ≤ n ≤ 10⁵
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Sample Input:
10
1001011110
Sample Output:
4
Input Format:
• First line: Integer n
• Second line: n space-separated integers
Output Format:
• Print true if the array is bitonic, otherwise print false
Constraints:
• 3 ≤ n ≤ 10⁵
Sample Input:
4
0321
Sample Output:
true
Input Format:
• First line: Integer n
• Second line: n space-separated integers
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Output Format:
• Single integer: the minimum absolute difference abs(max(E) - min(F))
Constraints:
• 2 ≤ n ≤ 10⁵
• −10⁶ ≤ D[i] ≤ 10⁶
Sample Input:
6
7 1 14 16 30 4
Sample Output:
2
Input Format:
• First line: Integer N
• Second line: N space-separated integers
• Third line: Integer target
Output Format:
• One integer: the number of valid triplets
Constraints:
• 3 ≤ N ≤ 1000
• −10⁵ ≤ arr[i], target ≤ 10⁵
Sample Input:
6
123456
10
Sample Output:
3
Input Format:
• Line 1: Integer n — number of elements in the array
• Line 2: n space-separated integers representing the array
• Line 3: Integer limit
Output Format:
• Single integer: length of the longest valid subarray
Constraints:
• 1 ≤ n ≤ 10^5
• 1 ≤ arr[i], limit ≤ 10^9
Example:
Input:
6
824723
4
Output:
3
Explanation: The longest valid subarray is [2, 4, 7] or [4, 7, 2] with max-min ≤ 4.
Output Format:
• Single integer: minimum number of swaps required to sort the array
Constraints:
• 1 ≤ n ≤ 10^5
• All elements are unique
Example:
Input:
4
4321
Output:
2
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Input Format:
• Line 1: Integer n — size of the array
• Line 2: n space-separated integers
Output Format:
• Single integer: the maximum sum of an increasing subsequence
Constraints:
• 1 ≤ n ≤ 10^3
• 1 ≤ arr[i] ≤ 10^4
Example:
Input:
5
1 101 2 3 100
Output:
106
Input Format:
• Line 1: Integer n
• Line 2: n space-separated positive integers
• Line 3: Integer k
Output Format:
• Single integer: count of such subarrays
Constraints:
• 1 ≤ n ≤ 10^5
• 1 ≤ arr[i] ≤ 1000
• 0 < k ≤ 10^6
Example:
Input:
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
4
10 5 2 6
100
Output:
8
Input Format:
• Line 1: Integer n
• Line 2: n space-separated integers
Output Format:
• A single line with n integers, where the ith integer is the next greater element of the ith element in the
array or -1 if none exists.
Constraints:
• 1 ≤ n ≤ 10^5
• −10^9 ≤ arr[i] ≤ 10^9
Sample Input:
4
4 5 2 10
Sample Output:
5 10 10 -1
Input Format:
• Line 1: Integer n (size of array)
• Line 2: n space-separated integers representing the array
Output Format:
• Print the rearranged array such that even indices contain even numbers and odd indices contain odd
numbers.
Constraints:
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
• 1 ≤ n ≤ 10^4
• The array contains equal number of even and odd numbers.
Sample Input:
4
4257
Sample Output:
4527
Input Format:
• Line 1: Integer n (size of array)
• Line 2: n space-separated integers
• Line 3: Integer k
Output Format:
• Single integer representing the count of contiguous subarrays summing to k.
Constraints:
• 1 ≤ n ≤ 10^5
• -10^4 ≤ arr[i] ≤ 10^4
• -10^9 ≤ k ≤ 10^9
Sample Input:
6
111211
3
Sample Output:
4
Input Format:
• Line 1: Integer n
• Line 2: n space-separated integers (sorted array)
• Line 3: Integer target
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Output Format:
• Single integer: count of target in the array.
Constraints:
• 1 ≤ n ≤ 10^5
• -10^9 ≤ arr[i], target ≤ 10^9
Sample Input:
7
1222345
2
Sample Output:
3
Input Format:
• Line 1: Integer n
• Line 2: n space-separated integers
Output Format:
• Single integer representing the smallest missing positive number.
Constraints:
• 1 ≤ n ≤ 10^5
• -10^5 ≤ arr[i] ≤ 10^5
Sample Input:
5
3 4 -1 1 2
Sample Output:
5
Input Format:
• Line 1: Two integers n and k
• Line 2: n space-separated integers
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Output Format:
• Single line of n space-separated integers representing the rotated array.
Constraints:
• 1 ≤ n ≤ 10^5
• 0 ≤ k ≤ 10^9
• -10^4 ≤ arr[i] ≤ 10^4
Sample Input:
52
12345
Sample Output:
45123
Input Format:
• Line 1: Integer n
• Line 2: n space-separated integers
Output Format:
• Single line with all duplicates (order does not matter), space-separated.
Constraints:
• 1 ≤ n ≤ 10^5
• 1 ≤ arr[i] ≤ n
Sample Input:
8
43278231
Sample Output:
23
Input Format:
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Output Format:
• A single line of n - k + 1 floats separated by space, each representing the median of the window. If
median is fractional, print decimal value.
Constraints:
• 1 ≤ k ≤ n ≤ 10^4
• -10^5 ≤ arr[i] ≤ 10^5
Sample Input:
53
1 3 -1 -3 5
Sample Output:
-1.0 -1.0 0.0
Input Format:
• Line 1: Integer n
• Line 2: n space-separated integers (each 0 or 1)
Output Format:
• Single integer: length of longest subarray of 1's after deleting one element.
Constraints:
• 1 ≤ n ≤ 10^5
• arr[i] ∈ {0,1}
Sample Input:
6
110111
Sample Output:
5
Input Format:
• Line 1: Integer n
• Line 2: n space-separated integers
Output Format:
• Single integer: number of good pairs
Constraints:
• 1 ≤ n ≤ 100
• 1 ≤ nums[i] ≤ 100
Sample Input:
6
123113
Sample Output:
4
Input Format:
• Line 1: Integer n
• Line 2: n space-separated integers (rotated sorted array)
• Line 3: Integer target
Output Format:
• Single integer: index of target or -1 if not found.
Constraints:
• 1 ≤ n ≤ 10^4
• -10^4 ≤ nums[i], target ≤ 10^4
Sample Input:
7
4567012
0
Sample Output:
4
Topic: 1. Arrays & Searching
Infosys HackWithInfy Preparatory Material
-- Compiled by Dr. K. V. Ramana
Input Format:
• Line 1: Two integers n and k
• Line 2: n space-separated integers
Output Format:
• A single float rounded to 5 decimal places — the maximum average of any subarray of size k.
Constraints:
• 1 ≤ n ≤ 10^5
• -10^4 ≤ nums[i] ≤ 10^4
• 1≤k≤n
Sample Input:
63
1 12 -5 -6 50 3
Sample Output:
15.66700