-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay75
126 lines (98 loc) · 3.47 KB
/
Day75
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
123
124
125
126
//B. Even Array
//https://codeforces.com/problemset/problem/1367/B
import java.util.Scanner;
public class EvenArray {
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();
}
int evenMismatch = 0;
int oddMismatch = 0;
// Count mismatches
for (int j = 0; j < n; j++) {
if (j % 2 == 0 && a[j] % 2 != 0) {
evenMismatch++;
} else if (j % 2 != 0 && a[j] % 2 == 0) {
oddMismatch++;
}
}
if (evenMismatch != oddMismatch) {
System.out.println(-1);
} else {
System.out.println(evenMismatch);
}
}
scanner.close();
}
}
//
//A. Panoramix's Prediction
//https://codeforces.com/problemset/problem/80/A
import java.util.Scanner;
public class PanoramixPrediction {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// List of all prime numbers <= 50
int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};
int n = scanner.nextInt();
int m = scanner.nextInt();
// Find if m is the next prime after n
boolean isNextPrime = false;
for (int i = 0; i < primes.length - 1; i++) {
if (primes[i] == n && primes[i + 1] == m) {
isNextPrime = true;
break;
}
}
System.out.println(isNextPrime ? "YES" : "NO");
scanner.close();
}
}
//A. Boring Apartments
//https://codeforces.com/problemset/problem/1433/A
import java.util.Scanner;
public class BoringApartments {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt(); // Number of test cases
for (int i = 0; i < t; i++) {
int x = scanner.nextInt(); // Apartment number that answered
int digit = x / 1111; // Extracting the digit (1-9)
int totalKeypresses = 0;
// Count keypresses for digits 1 to (digit - 1)
for (int d = 1; d < digit; d++) {
totalKeypresses += d * 4; // Each digit contributes d keypresses for 4 apartments (d, dd, ddd, dddd)
}
// Count keypresses for the current digit
totalKeypresses += digit * (x % 1111 + 1); // Count apartments from d, dd, ..., up to x
System.out.println(totalKeypresses);
}
scanner.close();
}
}
//202. Happy Number
//https://leetcode.com/problems/happy-number/description/?envType=study-plan-v2&envId=top-interview-150
class Solution {
public boolean isHappy(int n) {
HashSet<Integer> seen = new HashSet<>();
while (n != 1 && !seen.contains(n)) {
seen.add(n);
n = getNext(n);
}
return n == 1;
}
private int getNext(int n) {
int totalSum = 0;
while (n > 0) {
int digit = n % 10;
totalSum += digit * digit;
n /= 10;
}
return totalSum;
}
}