Hello World
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

Compile and run:

javac Main.java
java Main

This is the standard Java entry point: the main method runs first, and System.out.println prints to the console.

Variables
String name = "Dan";
int count = 1;
boolean active = true;

These are a few common Java types. Java keeps types explicit, so the declaration tells you exactly what each value holds.

Methods
static String greet(String name) {
    return "Hello, " + name + "!";
}

This is a small static method that takes one input and returns one computed string.

Java Add
public class Solution {
    public static int add(int param1, int param2) {
        return param1 + param2;
    }
}

This just adds the two input numbers with the language’s normal arithmetic and returns the sum.

Java Add Border
public class Solution {
    public static String[] addBorder(String[] picture) {
        int width = picture[0].length() + 2;
        String border = "*".repeat(width);

        String[] result = new String[picture.length + 2];
        result[0] = border;
        for (int i = 0; i < picture.length; i++) {
            result[i + 1] = "*" + picture[i] + "*";
        }
        result[result.length - 1] = border;

        return result;
    }
}

This builds a new grid with a * border around every side. It adds a full top and bottom row, then wraps each existing row from left and right.

Java Adjacent Elements Product
public class Solution {
    public static int adjacentElementsProduct(int[] inputArray) {
        int max = Integer.MIN_VALUE;

        for (int i = 0; i < inputArray.length - 1; i++) {
            max = Math.max(max, inputArray[i] * inputArray[i + 1]);
        }

        return max;
    }
}

This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.

Java Almost Magic Square
public class Solution {
    public static int[] almostMagicSquare(int[] a) {
        int[][] grid = new int[3][3];
        for (int i = 0; i < 9; i++) {
            grid[i / 3][i % 3] = a[i];
        }

        int[] rowSum = new int[3];
        int[] colSum = new int[3];
        int maxSum = 0;

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                rowSum[i] += grid[i][j];
                colSum[i] += grid[j][i];
            }
        }

        for (int k = 0; k < 3; k++) {
            maxSum = Math.max(maxSum, rowSum[k]);
            maxSum = Math.max(maxSum, colSum[k]);
        }

        for (int i = 0, j = 0; i < 3 && j < 3; ) {
            int diff = Math.min(maxSum - rowSum[i], maxSum - colSum[j]);
            grid[i][j] += diff;
            rowSum[i] += diff;
            colSum[j] += diff;

            if (rowSum[i] == maxSum) {
                i++;
            }
            if (colSum[j] == maxSum) {
                j++;
            }
        }

        int[] result = new int[9];
        for (int i = 0; i < 9; i++) {
            result[i] = grid[i / 3][i % 3];
        }

        return result;
    }
}

This adjusts the matrix toward a matching target sum so the rows and columns line up more like a magic square.

Java Are Equally Strong
public class Solution {
    public static boolean areEquallyStrong(int yourLeft, int yourRight, int friendsLeft, int friendsRight) {
        return Math.max(yourLeft, yourRight) == Math.max(friendsLeft, friendsRight)
                && Math.min(yourLeft, yourRight) == Math.min(friendsLeft, friendsRight);
    }
}

This compares each person’s strongest and weakest arm. If both pairs match, the result is true.

Java Array Change
public class Solution {
    public static long arrayChange(int[] a) {
        long min = 0;
        long[] arr = new long[a.length];
        for (int i = 0; i < a.length; i++) {
            arr[i] = a[i];
        }

        for (int k = 0; k < arr.length - 1; k++) {
            if (arr[k] >= arr[k + 1]) {
                long dif = arr[k] - arr[k + 1] + 1;
                arr[k + 1] += dif;
                min += dif;
            }
        }

        return min;
    }
}

This moves left to right and bumps values only when needed so the array becomes strictly increasing.

Java Array Maximal Adjacement Difference
public class Solution {
    public static int arrayMaximalAdjacentDifference(int[] a) {
        int dif = 0;
        for (int i = 1; i < a.length - 1; i++) {
            dif = Math.max(dif, Math.max(Math.abs(a[i] - a[i - 1]), Math.abs(a[i] - a[i + 1])));
        }

        return dif;
    }
}

This checks the gap between each pair of neighbors and returns the largest difference.

Java Binary Gap
public class Solution {
    public static int binaryGap(int n) {
        String bin = Integer.toBinaryString(n).replaceAll("^0+|0+$", "");
        String[] zeroes = bin.split("1", -1);

        int gap = 0;
        for (String zero : zeroes) {
            gap = Math.max(gap, zero.length());
        }

        return gap;
    }
}

This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.

Java Bracket
import java.util.ArrayDeque;
import java.util.Deque;

public class Solution {
    public static int bracket(String s) {
        Deque<Character> stack = new ArrayDeque<>();

        for (char v : s.toCharArray()) {
            switch (v) {
                case ')':
                    if (stack.isEmpty() || stack.pop() != '(') {
                        return 0;
                    }
                    break;
                case ']':
                    if (stack.isEmpty() || stack.pop() != '[') {
                        return 0;
                    }
                    break;
                case '}':
                    if (stack.isEmpty() || stack.pop() != '{') {
                        return 0;
                    }
                    break;
                default:
                    stack.push(v);
                    break;
            }
        }

        return stack.isEmpty() ? 1 : 0;
    }
}

This uses a simple stack approach: open brackets go in, matching closing brackets pop them out.