Java Largest String
public class Solution {
    public static String largestString(String s) {
        char[] chars = s.toCharArray();
        int len = chars.length;
        StringBuilder cur = new StringBuilder();

        for (int i = len - 1; i >= 0; i--) {
            cur.insert(0, chars[i]);

            if (cur.length() == 3) {
                if (cur.toString().equals("abb")) {
                    chars[i] = 'b';
                    chars[i + 1] = 'a';
                    chars[i + 2] = 'a';
                    if (i + 4 < len && chars[i + 4] == 'b') {
                        i += 4 + 1;
                    } else if (i + 3 < len && chars[i + 3] == 'b') {
                        i += 3 + 1;
                    } else if (chars[i + 2] == 'b') {
                        i += 2 + 1;
                    }
                }
                if (chars[i + 1] == 'b') {
                    i += 1 + 1;
                } else {
                    ++i;
                }
                cur.setLength(0);
            }
        }

        return new String(chars);
    }
}

This builds the biggest valid string it can under the challenge rules by always choosing the best next character it is allowed to use.

Java Max Counters
public class Solution {
    public static int[] maxCounters(int n, int[] a) {
        int[] counters = new int[n];
        int maxCounter = 0;
        int lastUpdate = 0;
        int condition = n + 1;

        for (int v : a) {
            if (v <= n) {
                int index = v - 1;
                if (counters[index] < lastUpdate) {
                    counters[index] = lastUpdate;
                }
                counters[index]++;
                maxCounter = Math.max(counters[index], maxCounter);
            }
            if (v == condition) {
                lastUpdate = maxCounter;
            }
        }

        for (int k = 0; k < counters.length; k++) {
            if (counters[k] < lastUpdate) {
                counters[k] = lastUpdate;
            }
        }

        return counters;
    }
}

This delays the expensive “set all counters to max” work until it is really needed, which keeps the solution fast.

Java Max Double Slice Sum
public class Solution {
    public static int maxDoubleSliceSum(int[] a) {
        int size = a.length;
        if (size < 3) {
            return 0;
        }

        int[] p1 = new int[size];
        int[] p2 = new int[size];
        p1[1] = 0;
        p2[size - 2] = 0;

        for (int i = 2; i < size - 1; i++) {
            p1[i] = Math.max(0, p1[i - 1] + a[i - 1]);
            p2[size - i - 1] = Math.max(0, p2[size - i] + a[size - i]);
        }

        int sum = p1[1] + p2[1];
        for (int i = 1; i < size - 1; i++) {
            sum = Math.max(sum, p1[i] + p2[i]);
        }

        return sum;
    }
}

This keeps the best sum ending on the left and starting on the right, then combines them around each middle position.

Java Max Product Of Three
import java.util.Arrays;

public class Solution {
    public static long maxProductOfThree(int[] a) {
        int[] sorted = a.clone();
        Arrays.sort(sorted);
        int c = sorted.length;

        long p1 = (long) sorted[c - 1] * sorted[c - 2] * sorted[c - 3];
        long p2 = (long) sorted[0] * sorted[1] * sorted[c - 1];

        return Math.max(p1, p2);
    }
}

This checks the useful extremes, because the best product can come from either the three largest numbers or two negatives plus one large positive.

Java Max Profit
public class Solution {
    public static int maxProfit(int[] a) {
        int price = a[0];
        int profit = 0;

        for (int v : a) {
            price = Math.min(price, v);
            profit = Math.max(profit, v - price);
        }

        return profit;
    }
}

This tracks the lowest buy price seen so far and updates the best profit as it scans the prices once.

Java Max Slice Sum
public class Solution {
    public static int maxSliceSum(int[] a) {
        long tmp = Long.MIN_VALUE;
        long max = Long.MIN_VALUE;

        for (int v : a) {
            tmp = Math.max(tmp + v, v);
            max = Math.max(max, tmp);
        }

        return (int) max;
    }
}

This is a Kadane-style scan: keep the best running sum and the best overall sum while moving once through the array.

Java Min Avg Two Slice
public class Solution {
    public static int minAvgTwoSlice(int[] a) {
        int idx = 0;
        double min = (a[0] + a[1]) / 2.0;

        for (int i = 0; i < a.length - 1; i++) {
            double cur = (a[i] + a[i + 1]) / 2.0;
            if (i + 2 < a.length) {
                double three = (a[i] + a[i + 1] + a[i + 2]) / 3.0;
                cur = Math.min(cur, three);
            }
            if (cur < min) {
                min = cur;
                idx = i;
            }
        }

        return idx;
    }
}

This leans on the key trick for this problem: the minimum average slice is always length 2 or 3.

Java Min Perimeter Rectangle
public class Solution {
    public static int minPerimeterRectangle(int n) {
        long i = 1;
        long min = Long.MAX_VALUE;

        while (i * i < n) {
            if (n % i == 0) {
                min = Math.min(min, 2 * (i + n / i));
            }
            i++;
        }

        return (int) min;
    }
}

This searches factor pairs up to the square root and picks the pair with the smallest perimeter.

Java Missing Integer
import java.util.Arrays;

public class Solution {
    public static int missingInteger(int[] a) {
        int min = 1;
        int[] sorted = Arrays.stream(a).distinct().sorted().toArray();

        for (int v : sorted) {
            if (v > 0) {
                if (min != v) {
                    break;
                }
                min++;
            }
        }

        return min;
    }
}

This records the positive numbers that exist, then returns the smallest positive value that is still missing.

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

public class Solution {
    public static int nesting(String s) {
        if (s.isEmpty()) {
            return 1;
        }

        Deque<Character> stack = new ArrayDeque<>();
        for (char v : s.toCharArray()) {
            if (v == ')') {
                if (stack.isEmpty() || stack.pop() != '(') {
                    return 0;
                }
            } else {
                stack.push(v);
            }
        }

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

This treats the string like a balance counter: open parentheses add one, closing ones remove one.