Write a JAVA Program to find Subarrays with K Different integers
import [Link].*;
class Solution {
public int subarraysWithKDistinct(int[] A, int K) {
return atMostK(A, K) - atMostK(A, K - 1);
}
int atMostK(int[] A, int K) {
int i = 0, res = 0;
Map<Integer, Integer> count = new HashMap<>();
for (int j = 0; j < [Link]; ++j) {
if ([Link](A[j], 0) == 0) K--;
[Link](A[j], [Link](A[j], 0) + 1);
while (K < 0) {
[Link](A[i], [Link](A[i]) - 1);
if ([Link](A[i]) == 0) K++;
i++;
}
res += j - i + 1;
}
return res;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int k = [Link]();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}
Solution obj = new Solution();
[Link]([Link](arr, k));
}
}
Write a JAVA Program to find shortest sub array with sum at least K
Return the length of the shortest, non-empty, contiguous subarray of A with sum at
least K. If
there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
PROGRAM
import [Link].*;
class ShortestSubarray {
public int shortestSubarray(int[] A, int K) {
int N = [Link];
long[] P = new long[N + 1];
// Prefix sums
for (int i = 0; i < N; ++i) {
P[i + 1] = P[i] + (long) A[i];
}
int ans = N + 1; // Initialize with a value that's impossible (max length + 1)
Deque<Integer> monoq = new LinkedList<>(); // Monotonic queue for indices
for (int y = 0; y < [Link]; ++y) {
// Maintain increasing order in the deque
while (![Link]() && P[y] <= P[[Link]()]) {
[Link]();
}
// Check if current prefix sum satisfies condition with oldest index
while (![Link]() && P[y] >= P[[Link]()] + K) {
ans = [Link](ans, y - [Link]());
}
[Link](y);
}
return ans < N + 1 ? ans : -1;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int k = [Link]();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}
ShortestSubarray obj = new ShortestSubarray();
[Link]([Link](arr, k));
}
}
c) Write a JAVA Program to implement Fenwick Tree
Sample Input-1:
8
1 2 13 4 25 16 17 8
26
Sample Output-1:
75
PROGRAM
import [Link].*;
class NumArray {
int[] nums;
int[] BIT;
int n;
public NumArray(int[] nums) {
[Link] = nums;
n = [Link];
BIT = new int[n + 1];
for (int i = 0; i < n; i++) {
init(i, nums[i]);
}
}
public void init(int i, int val) {
i++;
while (i <= n) {
BIT[i] += val;
i += (i & -i);
}
}
public int getSum(int i) {
int sum = 0;
i++;
while (i > 0) {
sum += BIT[i];
i -= (i & -i);
}
return sum;
}
public int sumRange(int i, int j) {
return getSum(j) - getSum(i - 1);
}
public static void main(String args[]) {
Scanner scan = new Scanner([Link]);
int n = [Link]();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = [Link]();
}
NumArray na = new NumArray(nums);
int s1 = [Link]();
int s2 = [Link]();
[Link]([Link](s1, s2));
}
}
d) Write a JAVA Program to implement a segment tree with its operations
Sample Input-1:
4
1 2,2 4,2 3,2 2
Sample Output-1:
4
Sample Input-2:
6
1 5,2 3,2 4,2 2,3 4,3 5
Sample Output-2:
5
PROGRAM
import [Link].*;
public class Main {
static class SegmentTree {
int size;
int[] tree;
public SegmentTree(int n) {
size = n;
tree = new int[4 * n];
build(1, 1, size);
}
void build(int node, int l, int r) {
if (l == r) {
tree[node] = 1; // day l is available
return;
}
int mid = (l + r) / 2;
build(2 * node, l, mid);
build(2 * node + 1, mid + 1, r);
tree[node] = tree[2 * node] + tree[2 * node + 1];
}
int query(int node, int l, int r, int ql, int qr) {
if (qr < l || ql > r || tree[node] == 0) return -1;
if (l == r) return l;
int mid = (l + r) / 2;
int left = query(2 * node, l, mid, ql, qr);
if (left != -1) return left;
return query(2 * node + 1, mid + 1, r, ql, qr);
}
void update(int node, int l, int r, int pos) {
if (l == r) {
tree[node] = 0;
return;
}
int mid = (l + r) / 2;
if (pos <= mid) update(2 * node, l, mid, pos);
else update(2 * node + 1, mid + 1, r, pos);
tree[node] = tree[2 * node] + tree[2 * node + 1];
}
}
static class Program {
int start, end;
Program(int s, int e) {
start = s;
end = e;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
[Link]();
String[] pairs = [Link]().split(",");
List<Program> programs = new ArrayList<>();
int maxDay = 0;
for (String pair : pairs) {
String[] days = [Link]().split(" ");
int start = [Link](days[0]);
int end = [Link](days[1]);
[Link](new Program(start, end));
maxDay = [Link](maxDay, end);
}
// Sort by end day (greedy)
[Link]([Link](p -> [Link]));
SegmentTree st = new SegmentTree(maxDay);
int count = 0;
for (Program p : programs) {
int freeDay = [Link](1, 1, maxDay, [Link], [Link]);
if (freeDay != -1) {
[Link](1, 1, maxDay, freeDay);
count++;
}
}
[Link](count);
}
}
e) Write a JAVA Program to implement TREAP with its operations
Given an integer array nums, return the number of reverse pairs in the array. A
reverse pair is a
pair (i, j) where 0 <= i < j < [Link] and nums[i] > 2 * nums[j].
Example 1:
Input: nums = [1,3,2,3,1]
Output: 2
Example 2:
Input: nums = [2,4,3,5,1]
Output: 3
Constraints:
1 <= [Link] <= 5 * 104
-2^31 <= nums[i] <= 2^31 – 1
PROGRAM
import [Link].*;
public class TreapReversePairs {
static class TreapNode {
long key;
int priority;
int size;
TreapNode left, right;
TreapNode(long key) {
[Link] = key;
[Link] = new Random().nextInt();
[Link] = 1;
}
}
static int size(TreapNode node) {
return node == null ? 0 : [Link];
}
static void updateSize(TreapNode node) {
if (node != null)
[Link] = 1 + size([Link]) + size([Link]);
}
static TreapNode rotateRight(TreapNode y) {
TreapNode x = [Link];
[Link] = [Link];
[Link] = y;
updateSize(y);
updateSize(x);
return x;
}
static TreapNode rotateLeft(TreapNode x) {
TreapNode y = [Link];
[Link] = [Link];
[Link] = x;
updateSize(x);
updateSize(y);
return y;
}
static TreapNode insert(TreapNode root, TreapNode node) {
if (root == null) return node;
if ([Link] < [Link]) {
[Link] = insert([Link], node);
if ([Link] > [Link])
root = rotateRight(root);
} else {
[Link] = insert([Link], node);
if ([Link] > [Link])
root = rotateLeft(root);
}
updateSize(root);
return root;
}
// Count number of nodes with key < target
static int countLessThan(TreapNode root, long target) {
if (root == null) return 0;
if (target <= [Link]) {
return countLessThan([Link], target);
} else {
return 1 + size([Link]) + countLessThan([Link], target);
}
}
public static int reversePairs(int[] nums) {
TreapNode root = null;
int count = 0;
for (int i = [Link] - 1; i >= 0; i--) {
count += countLessThan(root, nums[i]);
root = insert(root, new TreapNode(2L * nums[i]));
}
return count;
}
// 📦 Main to handle input/output
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int[] nums = new int[n];
for(int i=0; i<n; i++) {
nums[i] = [Link]();
}
[Link](reversePairs(nums));
}
}