22BCS10271 Harsh Thakur Assignment
22BCS10271 Harsh Thakur Assignment
Assignment-1
Student Name: Harsh Thakur UID:22BCS10271 Branch: CSE Date of
Performance:31/05/25Subject Name:In-house Training Semester : 6
2. Objective:
Given an array and a list of operations (values you can add), applyexactlykoperations
(one per value) to maximize the sumof any subarray. Eachoperation can be added
to only one element of the array.
3. Algorithm:
∙ For each operation, try adding it to every unused index and checkwhichposition
gives the maximum Kadane’s subarray sum.
∙ Apply the operation at the best index, and repeat for the next operation.
4. Implementation/Code:
import java.util.*;
public class Main {
static int kadane(int[] arr) {
int maxEndingHere = arr[0];
int maxSoFar = arr[0];
for (int i = 1; i < arr.length; i++) {
maxEndingHere = Math.max(arr[i], maxEndingHere + arr[i]); maxSoFar =
Math.max(maxSoFar, maxEndingHere);
}
return maxSoFar;
Harsh Thakur 22BCS10271
DEPARTMENT OF
DEPARTMENT OF
5. Output:
Harsh Thakur 22BCS10271
DEPARTMENT OF
∙ Use two pointers: one for arrival and one for departure. ∙ If a train arrives before the
last one departs, increase platformcount. ∙ If a train departs before the next arrives,
decrease platformcount. ∙ Track the maximum platforms used at any time —that’s
the answer.
3. Implementation/Code:
import java.util.*;
public class Main {
public static int findMinimumPlatforms(int[] arr, int[] dep) {
Arrays.sort(arr);
Arrays.sort(dep);
int n = arr.length;
int i = 1, j = 0;
int platforms = 1, maxPlatforms = 1; while (i < n && j < n)
{
if (arr[i] <= dep[j])
{ platforms++;
DEPARTMENT OF
4.Output:
DEPARTMENT OF
2. Objective:
Find the next greater element for each element in a circular array; if
noneexists,return -1.
3. Algorithm:
∙ Initialize a stack to store indices and a result array filled with-1. ∙ Traverse the
n]:
∙ While stack is not empty and current element > element at stack’stopindex,
Pop from stack and update result at that index with current element. ∙ If in the
first pass (i < n), push current index onto the stack.
∙ After traversal, result array contains the next greater elements or -1ifnone exists.
4. Implementation/Code:
class Solution {
public int[] nextGreaterElements(int[] nums)
{ int n = nums.length;
int[] result = new int[n];
Stack<Integer> stack = new Stack<>();
Arrays.fill(result, -1);
Harsh Thakur 22BCS10271
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
if (i < n)
{ stack.push(i);
}
}
return result;
}
}
5. Output:
Harsh Thakur 22BCS10271
DEPARTMENT OF
2. Objective:
Decode a string encoded with the pattern k[encoded_string] where thesubstringinside
brackets is repeated k times. Return the fully decoded string.
3. Algorithm:
1. Use two stacks — one for counts and one for partial decoded strings. 2. Traverse the
input string character by character:
If digit, build the repeat count k.
If [, push the current count and current decoded string ontotheir stacks,reset
current and k.
If ], pop count and previous decoded string; append the current
decodedsubstring repeated count times to it.
Else, append characters to current decoded substring.
3. At the end, the current string contains the fully decoded string.
DEPARTMENT OF
return current.toString();
}
}
5. Output:
Harsh
Thakur 22BCS10271
DEPARTMENT OF
3. Algorithm:
. Find the middle of the linked list using slow and fast pointers. . Reverse the
class Solution {
public void reorderList(ListNode head) { if (head == null ||
head.next == null) return;
DEPARTMENT OF
7. Learning Outcome:
1. Learned how to strategically apply limited operations to optimize subarraysumusing
greedy logic + prefix sum + priority decisions.
2. Understood how to handle overlapping intervals using sorting and twopointers, acommon
technique in scheduling and time-based event problems.
3. Mastered use of monotonic stack and how to simulate circular arrays efficientlyusing
modulo and two-pass traversal.
4. Learned how to use two stacks (one for numbers, one for strings) to decodenestedpatterns
– useful in parsing and expression evaluation problems.