Advanced Coding-II
Assignment-2
HU21CSEN0101366
M.H.KARTHIK
Question 1.
Given a circular integer array nums of length n, return the maximum possible sum of a non-
empty subarray of nums.
A circular array means the end of the array connects to the beginning of the array. Formally, the next
element of nums[i] is nums[(i + 1) % n] and the previous element of nums[i] is nums[(i - 1 +
n) % n].
A subarray may only include each element of the fixed buffer nums at most once. Formally, for a
subarray nums[i], nums[i + 1], ..., nums[j], there does not exist i <= k1, k2 <= j with k1 % n ==
k2 % n.
Example 1:
Input: nums = [1,-2,3,-2]
Output: 3
Explanation: Subarray [3] has maximum sum 3.
Example 2:
Input: nums = [5,-3,5]
Output: 10
Explanation: Subarray [5,5] has maximum sum 5 + 5 = 10.
Example 3:
Input: nums = [-3,-2,-3]
Output: -2
Explanation: Subarray [-2] has maximum sum -2
Constraints:
• n == nums.length
• 1 <= n <= 3 * 104
• -3 * 104 <= nums[i] <= 3 * 104
Solution
public class Main {
// Kadane's Algorithm for finding the maximum subarray sum
public static int kadaneMax(int[] arr) {
int maxCurrent = arr[0];
int maxGlobal = arr[0];
for (int i = 1; i < arr.length; i++) {
maxCurrent = Math.max(arr[i], maxCurrent + arr[i]);
maxGlobal = Math.max(maxGlobal, maxCurrent);
}
return maxGlobal;
}
// Function to find the maximum circular subarray sum
public static int maxSubarraySumCircular(int[] nums) {
int totalSum = 0;
int maxKadane = kadaneMax(nums);
// Calculate total sum and invert array to find the minimum sum
subarray
for (int i = 0; i < nums.length; i++) {
totalSum += nums[i];
nums[i] = -nums[i];
}
// Find minimum sum subarray using Kadane on the inverted array
int minKadane = kadaneMax(nums);
// If all elements are negative, the circular sum is not valid
if (totalSum + minKadane == 0) {
return maxKadane;
}
// Return the maximum of non-circular and circular sums
return Math.max(maxKadane, totalSum + minKadane);
}
public static void main(String[] args) {
// Input from user
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.print("Enter the number of elements in the array: ");
int n = sc.nextInt();
int[] nums = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
// Compute and print the result
int result = maxSubarraySumCircular(nums);
System.out.println("Maximum Circular Subarray Sum: " + result);
sc.close();
}
}
Output:
Stamping The Sequence
You are given two strings stamp and target. Initially, there is a string s of length target.length with all
s[i] == '?'. In one turn, you can place stamp over s and replace every letter in the s with the
corresponding letter from stamp.
For example, if stamp = "abc" and target = "abcba", then s is "?????" initially. In one turn you can:
place stamp at index 0 of s to obtain "abc??",
place stamp at index 1 of s to obtain "?abc?", or
place stamp at index 2 of s to obtain "??abc".
Note that stamp must be fully contained in the boundaries of s in order to stamp (i.e., you cannot
place stamp at index 3 of s).
We want to convert s to target using at most 10 * target.length turns.
Return an array of the index of the left-most letter being stamped at each turn. If we cannot obtain
target from s within 10 * target.length turns, return an empty array.
Example 1:
Input: stamp = "abc", target = "ababc"
Output: [0,2]
Explanation: Initially s = "?????".
- Place stamp at index 0 to get "abc??".
- Place stamp at index 2 to get "ababc".
[1,0,2] would also be accepted as an answer, as well as some other answers.
Example 2:
Input: stamp = "abca", target = "aabcaca"
Output: [3,0,1]
Explanation: Initially s = "???????".
- Place stamp at index 3 to get "???abca".
- Place stamp at index 0 to get "abcabca".
- Place stamp at index 1 to get "aabcaca".
Constraints:
1 <= stamp.length <= target.length <= 1000
stamp and target consist of lowercase English letters.
Solution
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static int[] movesToStamp(String stamp, String target) {
char[] stampArr = stamp.toCharArray();
char[] targetArr = target.toCharArray();
boolean[] visited = new boolean[target.length()];
List<Integer> result = new ArrayList<>();
int stars = 0;
while (stars < target.length()) {
boolean replaced = false;
for (int i = 0; i <= target.length() - stamp.length(); i++) {
if (!visited[i] && canReplace(targetArr, i, stampArr)) {
stars += doReplace(targetArr, i, stampArr.length);
visited[i] = true;
replaced = true;
result.add(i);
if (stars == target.length()) break;
}
}
if (!replaced) return new int[0];
}
int[] res = new int[result.size()];
for (int i = 0; i < result.size(); i++) {
res[i] = result.get(result.size() - 1 - i);
}
return res;
}
private static boolean canReplace(char[] target, int start, char[] stamp)
{
for (int i = 0; i < stamp.length; i++) {
if (target[start + i] != '?' && target[start + i] != stamp[i]) {
return false;
}
}
return true;
}
private static int doReplace(char[] target, int start, int length) {
int count = 0;
for (int i = 0; i < length; i++) {
if (target[start + i] != '?') {
target[start + i] = '?';
count++;
}
}
return count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the stamp string: ");
String stamp = sc.nextLine();
System.out.print("Enter the target string: ");
String target = sc.nextLine();
int[] result = movesToStamp(stamp, target);
if (result.length == 0) {
System.out.println("It's not possible to stamp the target
string.");
} else {
System.out.print("Stamping order: ");
for (int idx : result) {
System.out.print(idx + " ");
}
System.out.println();
}
sc.close();
}
}
Output: