OOPs Using Java DPP 01
OOPs Using Java DPP 01
GATE
Q1 Problem statement: You are given an array nums array can hold any values, as they don't affect
containing n elements, where each the result.
element represents a color: Examples:
Input: arr[] = [2, 2, 2, 2, 2]
0 for red
Output: 1 (array becomes [2])
1 for white
Explanation: Only one unique element (2) is
2 for blue
present.
Your task is to sort the array in-place so that Input: arr[] = [1, 2, 2, 3, 4, 4, 4, 5, 5]
objects of the same color are grouped together, Output: 5 (array becomes [1, 2, 3, 4, 5])
in the order red (0), white (1), then blue (2). Input: arr[] = [1, 2, 3]
Important: Output: 3 (array remains [1, 2, 3])
You cannot use any built-in sort functions. Explanation: All elements are already distinct.
The solution should ideally be a one-pass Constraints:
algorithm that uses only constant extra space. 1 <= n <= 10^4
Example 1: Array is sorted in non-decreasing order.
Input: nums = [2,0,2,1,1,0]
Q3 Problem statement: Given an array arr[], your
Output: [0,0,1,1,2,2]
task is to identify all the leaders in the array.
Example 2:
An element is called a leader if it is greater than
Input: nums = [2,0,1]
or equal to all the elements to its right in the
Output: [0,1,2]
array. By default, the last element in the array is
Constraints:
always considered a leader since there are no
1 <= nums.length <= 300 elements after it.
Each nums[i] is either 0, 1, or 2. Examples:
Input: arr[] = [16, 17, 4, 3, 5, 2]
Follow-up:
Output: [17, 5, 2]
Can you design a one-pass solution that sorts the
Input: arr[] = [1, 2, 3, 4, 5, 2]
array using only constant extra space?
Output: [5, 2]
Q2 Problem statement: You are given a sorted array
Q4 Problem statement: You are given an unsorted
arr[] of size n.
array of size n, containing numbers ranging from
The task is to rearrange the array in-place so that
1 to n.
all distinct elements appear at the start of the
In this array:
array in sorted order.
After moving the distinct elements to the front, One number is missing from the range {1, 2, …,
return the number of distinct elements found. n}
The elements beyond the distinct portion of the One number appears twice
https://qbg-admin.penpencil.co/finalize-question-paper/print-preview 1/4
4/23/25, 12:26 PM GATE_Computer Science & Information Technology_DPP 3
GATE
Your task is to find the repeating number and the You must sell the stock before you can buy it
missing number. again (no multiple holdings allowed at
Examples: the same time).
Input: arr[] = {3, 1, 3} You can buy and sell on the same day if it’s
Output: 3, 2 profitable.
Explanation: Number 2 is missing, and 3 occurs
Examples:
twice.
Input: prices[] = {100, 180, 260, 310, 40, 535, 695}
Input: arr[] = {4, 3, 6, 2, 1, 1}
Output: 865
Output: 1, 5
Explanation:
Explanation: Number 5 is missing, and 1 repeats.
Buy on day 0, sell on day 3 → Profit = 310 -
Q5 Problem statement: You are given an array 100 = 210
prices[] of length N, where each element Buy on day 4, sell on day 6 → Profit = 695 - 40
represents the price of a stock on a particular = 655
day. Total Profit = 210 + 655 = 865
Your task is to find the maximum profit that can
be made by performing exactly one
transaction — meaning buying one stock and
selling it later.
Note: You must buy the stock before you sell it.
Examples:
Input: prices[] = {7, 10, 1, 3, 6, 9, 2}
Output: 8
Explanation: Buy at 1 and sell at 9 for a profit of
8.
Input: prices[] = {7, 6, 4, 3, 1} Input: prices[] = {4, 2, 2, 2, 4}
Output: 0 Output: 2
Explanation: No profit can be made as prices Explanation:
keep decreasing.
Buy on day 3, sell on day 4 → Profit = 4 - 2 = 2
Input: prices[] = {1, 3, 6, 9, 11}
Total Profit = 2
Output: 10
Explanation: Buy at 1 and sell at 11 for a profit of Q7 Problem statement: Given an array arr[] of n
10. integers, your task is to create a new array res[]
of the same size such that each element res[i] is
Q6 Problem statement: You are given an array
equal to the product of all the elements in arr[]
prices[] of size n, where each element
except arr[i].
represents the price of a stock on a particular
Examples:
day.
Input: arr[] = [10, 3, 5, 6, 2]
The task is to calculate the maximum profit
Output: [180, 600, 360, 300, 900]
possible if you are allowed to buy and sell the
Explanation:
stock any number of times.
Note: res[0] = 3 × 5 × 6 × 2 = 180
res[1] = 10 × 5 × 6 × 2 = 600
res[2] = 10 × 3 × 6 × 2 = 360
https://qbg-admin.penpencil.co/finalize-question-paper/print-preview 2/4
4/23/25, 12:26 PM GATE_Computer Science & Information Technology_DPP 3
GATE
Input: arr[] = [12, 0] Q10 Problem statement: You are given an array of n
Output: [0, 12] distinct integers. Your task is to find the third
Explanation: largest element in the array.
res[0] = 0 Examples:
res[1] = 12 Input: arr[] = {1, 14, 2, 16, 10, 20}
Output: 14
Q8 Problem statement: Given an array arr[], the task Explanation: The largest element is 20, the
is to find the contiguous subarray with the second largest is 16, and the third largest is 14.
maximum possible sum and return that sum. Input: arr[] = {19, -10, 20, 14, 2, 16, 10}
Examples: Output: 16
Input: arr[] = {2, 3, -8, 7, -1, 2, 3} Explanation: The largest element is 20, the
Output: 11 second largest is 19, and the third largest is 16.
Explanation: The subarray {7, -1, 2, 3} has the
highest sum of 11. Q11 Problem Statement: Given an integer array nums
Input: arr[] = {-2, -4} and an integer target, return indices of the two
Output: -2 numbers such that they add up to target. You
Explanation: The subarray {-2} gives the must achieve an average O(n) runtime by using a
maximum sum -2. Map<Integer,Integer> to store seen values.
Input: arr[] = {5, 4, 1, 7, 8} Example 1
Output: 25 Input: nums = [2,7,11,15], target = 9
Explanation: The entire array {5, 4, 1, 7, 8} has the Output: [0,1]
largest sum 25. Explanation: nums[0] + nums[1] == 9
Example 2
Q9 Problem statement: You are given an unsorted Input: nums = [3,2,4], target = 6
array arr[] that contains both positive and Output: [1,2]
negative numbers. Your task is to find the
smallest positive number missing from the array. Q12 Problem Statement: Given a string s, find the
Note: You are allowed to modify the original index of the first non-repeating character in it. If
array. it doesn’t exist, return - 1.
Examples: Example 1
Input: arr[] = {2, -3, 4, 1, 1, 7} Input: s = "adityajainsir"
Output: 3 Output: 0
Explanation: The smallest positive number Explanation: 'd' is the first character that appears
missing from the array is 3. only once, at index 0.
Input: arr[] = {5, 3, 2, 5, 1} Example 2
Output: 4 Input: s = "aadiyajainsir"
Explanation: The smallest positive number Output: 6
missing from the array is 4. Explanation: 'j' is the first non-repeating
Input: arr[] = {-8, 0, -1, -4, -3} character, at index 6.
Output: 1
Q13 Problem Statement: Given two integer arrays
nums1 and nums2, return their intersection,
https://qbg-admin.penpencil.co/finalize-question-paper/print-preview 3/4
4/23/25, 12:26 PM GATE_Computer Science & Information Technology_DPP 3
GATE
https://qbg-admin.penpencil.co/finalize-question-paper/print-preview 4/4