-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay76
122 lines (98 loc) · 3.4 KB
/
Day76
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//Day76
//A. Do Not Be Distracted!
//
import java.util.HashSet;
import java.util.Scanner;
public class PolycarpTasks {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt(); // Number of test cases
scanner.nextLine(); // Consume newline
for (int i = 0; i < t; i++) {
int n = scanner.nextInt(); // Number of days
String tasks = scanner.next(); // Task sequence
scanner.nextLine(); // Consume newline
if (isSuspicious(tasks)) {
System.out.println("NO");
} else {
System.out.println("YES");
}
}
scanner.close();
}
private static boolean isSuspicious(String tasks) {
HashSet<Character> completedTasks = new HashSet<>();
char lastTask = '\0'; // Initialize with null character
for (char task : tasks.toCharArray()) {
if (task != lastTask) {
if (completedTasks.contains(task)) {
return true; // Found a suspicious task switch
}
completedTasks.add(task);
lastTask = task;
}
}
return false; // No suspicious behavior detected
}
}
//A. Maximum GCD
//https://codeforces.com/problemset/problem/1370/A
import java.util.Scanner;
public class MaximumGCD {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
// The maximum GCD is simply n / 2
System.out.println(n / 2);
}
scanner.close();
}
}
//A. Plus One on the Subset
//https://codeforces.com/problemset/problem/1624/A
import java.util.Scanner;
public class PlusOneOnTheSubset {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
int[] a = new int[n];
for (int j = 0; j < n; j++) {
a[j] = scanner.nextInt();
}
// Find maximum element in the array using a standard for loop
int max_a = a[0];
for (int j = 1; j < n; j++) {
if (a[j] > max_a) {
max_a = a[j];
}
}
// Calculate total operations needed to make all elements equal to max_a
long operations = 0;
for (int j = 0; j < n; j++) {
operations += (max_a - a[j]); // Sum of differences
}
System.out.println(operations);
}
scanner.close();
}
}
// 9. Palindrome Number
// https://leetcode.com/problems/palindrome-number/?envType=study-plan-v2&envId=top-interview-150
class Solution {
public boolean isPalindrome(int x) {
if (x < 0 || (x % 10 == 0 && x != 0)) {
return false;
}
int reversedHalf = 0;
// Step 2: Reverse the second half of the number
while (x > reversedHalf) {
reversedHalf = reversedHalf * 10 + x % 10;
x /= 10;
}
return x == reversedHalf || x == reversedHalf / 10;
}
}