Assignment 01
Assignment 01
Title: Array-Problems
Name: P. Jaswanth
import java.util.*;
Assignment-01 1
}
Dry-Run:
Assignment-01 2
Q2. Longest Subarray with Sum K
Solution:
if (sum == k) {
maxLength = Math.max(maxLength, j - i + 1);
}
j++;
}
return maxLength;
}
Dry-Run:
Assignment-01 3
Q3. Sliding Window Maximum
(Note: The below code seems more related to graph traversal and coloring —
let me know if you'd like a different implementation.)
Solution:
class Solution {
public int largestPathValue(String colors, int[][] edges) {
HashMap<Integer, List<Integer>> graph = new HashMap<>();
int n = colors.length();
int inDegree[] = new int[n];
Assignment-01 4
inDegree[edge[1]]++;
}
while (!q.isEmpty()) {
int node = q.poll();
ans = Math.max(ans, ++count[node][colors.charAt(node) - 'a']);
nodeSeen++;
if (!graph.containsKey(node)) continue;
Dry-Run:
Assignment-01 5
Assignment-01 6
Q4. Product of Array Except Self
Solution:
import java.util.*;
return result;
}
System.out.print("Output: [");
for (int i = 0; i < output.length; i++) {
System.out.print(output[i]);
Assignment-01 7
if (i < output.length - 1) System.out.print(", ");
}
System.out.println("]");
}
}
Dry-Run:
import java.util.*;
Assignment-01 8
for (int[] x : points) {
Map<Double, Integer> map = new HashMap<>();
double slope;
if (x[0] == y[0]) {
slope = Double.POSITIVE_INFINITY;
} else {
slope = (y[1] - x[1]) / (double)(y[0] - x[0]);
}
return max + 1;
}
Dry-Run:
Assignment-01 9
Q6. Subarray Product Less Than K
Solution:
int totalCount = 0;
int product = 1;
Assignment-01 10
totalCount += right - left + 1;
}
return totalCount;
}
Dry-Run:
Assignment-01 11
Q7. Maximum Product Subarray
Solution:
Assignment-01 12
min * nums[i]));
curr_min = Math.min(nums[i], Math.min(prev_max * nums[i], curr_mi
n * nums[i]));
return result;
}
Dry-Run:
Assignment-01 13
Assignment-01 14