01
Variables and types
Java requires a declared type, and choosing int or long affects overflow in algorithms.
public class Main {
public static void main(String[] args) {
int count = 3;
String label = "items";
System.out.println(label + " " + count);
}
}
Output: items 3
02
Conditionals and loops
Scanning arrays and graphs depends on predictable branches and iteration.
public class Main {
public static void main(String[] args) {
int total = 0;
for (int value : new int[]{2, 4, 5})
if (value % 2 == 0) total += value;
System.out.println(total);
}
}
Output: 6
03
Functions
Static methods give each algorithm explicit parameters and a return type.
public class Main {
static int doubleValue(int value) { return value * 2; }
public static void main(String[] args) {
System.out.println(doubleValue(6));
}
}
Output: 12
04
Recursion
Tree traversal and divide-and-conquer rely on a method calling itself with a smaller case.
public class Main {
static void countdown(int n) {
if (n == 0) return;
System.out.println(n);
countdown(n - 1);
}
public static void main(String[] args) { countdown(3); }
}
Output: 3
2
1
05
Arrays and lists
Arrays are fixed-size indexed storage; ArrayList is the resizable version used in many solutions.
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>(List.of(3, 1, 2));
numbers.add(4);
System.out.println(numbers.get(1) + " " + numbers.size());
}
}
Output: 1 4
06
Strings
String values are immutable, so comparisons and efficient building need deliberate APIs.
public class Main {
public static void main(String[] args) {
String word = "level";
System.out.println(word.equals(new StringBuilder(word).reverse().toString()));
}
}
Output: true
07
Hash maps and sets
HashMap and HashSet provide average constant-time lookup for counts and membership.
import java.util.*;
public class Main {
public static void main(String[] args) {
Set<Integer> seen = new HashSet<>(Set.of(2, 4));
Map<String, Integer> counts = Map.of("a", 2);
System.out.println(seen.contains(4) + " " + counts.get("a"));
}
}
Output: true 2
08
References and copying
Two array variables can refer to one object; copy when a separate mutable array is required.
import java.util.*;
public class Main {
public static void main(String[] args) {
int[] first = {1, 2};
int[] second = first.clone();
second[0] = 9;
System.out.println(Arrays.toString(first) + " " + Arrays.toString(second));
}
}
Output: [1, 2] [9, 2]
09
Classes or structs
A node class models the fields that connect linked lists and trees.
public class Main {
static class Node { int value; Node(int value) { this.value = value; } }
public static void main(String[] args) {
System.out.println(new Node(7).value);
}
}
Output: 7
10
Sorting with a custom comparator
Comparator.comparing and lambdas express ordering for records and intervals.
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> words = new ArrayList<>(List.of("bbb", "a"));
words.sort(Comparator.comparingInt(String::length));
System.out.println(words);
}
}
Output: [a, bbb]
11
Reading input
BufferedReader or a token reader handles judge input more efficiently than Scanner for large input.
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(reader.readLine());
int b = Integer.parseInt(reader.readLine());
System.out.println(a + b);
}
}
Input: 2
4
Output: 6