Skip to content

Commit 6385463

Browse files
committed
practice session
1 parent f3a8435 commit 6385463

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package contests;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* @author rohithvazhathody
7+
* 15-Aug-2025
8+
*/
9+
public class AdjacentXOR {
10+
11+
/**
12+
* @param args
13+
*/
14+
public static void main(String[] args) {
15+
Scanner sc = new Scanner(System.in);
16+
int test = sc.nextInt();
17+
while (test-- > 0) {
18+
int n = sc.nextInt();
19+
int [] nums1 = new int[n];
20+
int [] nums2 = new int [n];
21+
for (int index = 0; index < n; index++) {
22+
nums1[index] = sc.nextInt();
23+
}
24+
for (int index = 0; index < n; index++) {
25+
nums2[index] = sc.nextInt();
26+
}
27+
System.out.println(isConversionPossible(nums1, nums2, n));
28+
}
29+
sc.close();
30+
}
31+
32+
private static String isConversionPossible(int[] nums1, int[] nums2, int n) {
33+
for (int index = 0; index + 1 < n; index++) {
34+
if ((nums1[index] ^ nums1[index + 1]) == nums2[index]) {
35+
nums1[index] = nums2[index];
36+
}
37+
}
38+
for (int index = n - 2; index >= 0; index--) {
39+
if ((nums1[index] ^ nums1[index + 1]) == nums2[index]) {
40+
nums1[index] = nums2[index];
41+
}
42+
}
43+
for (int index = 0; index < n; index++) {
44+
if (nums1[index] != nums2[index]) {
45+
return "NO";
46+
}
47+
}
48+
return "YES";
49+
}
50+
51+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package contests;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Scanner;
6+
7+
/**
8+
* @author rohithvazhathody 15-Aug-2025
9+
*/
10+
public class ArborisContractio {
11+
12+
/**
13+
* @param args
14+
*/
15+
public static void main(String[] args) {
16+
Scanner sc = new Scanner(System.in);
17+
int test = sc.nextInt();
18+
while (test-- > 0) {
19+
int n = sc.nextInt();
20+
List<List<Integer>> graph = new ArrayList<>();
21+
for (int index = 0; index < n; index++) {
22+
graph.add(new ArrayList<>());
23+
}
24+
for (int index = 0; index < n - 1; index++) {
25+
int u = sc.nextInt();
26+
int v = sc.nextInt();
27+
u--;
28+
v--;
29+
graph.get(u).add(v);
30+
graph.get(v).add(u);
31+
}
32+
System.out.println(findMinOperation(graph, n));
33+
}
34+
sc.close();
35+
}
36+
37+
private static int findMinOperation(List<List<Integer>> graph, int n) {
38+
int totalLeaf = 0;
39+
for (int index = 0; index < n; index++) {
40+
if (graph.get(index).size() == 1) {
41+
totalLeaf++;
42+
}
43+
}
44+
int totalOperation = Integer.MAX_VALUE;
45+
for (int index = 0; index < n; index++) {
46+
// if already leaf, minus 1
47+
int currentOperation = totalLeaf;
48+
if (graph.get(index).size() == 1) {
49+
currentOperation--;
50+
}
51+
List<Integer> children = graph.get(index);
52+
for (Integer child : children) {
53+
if (graph.get(child).size() == 1) {
54+
currentOperation--;
55+
}
56+
}
57+
totalOperation = Math.min(totalOperation, currentOperation);
58+
}
59+
return totalOperation;
60+
}
61+
62+
}

0 commit comments

Comments
 (0)