Java Number Of Disc Intersections
public class Solution {
public static int numberOfDiscIntersections(int[] a) {
int c = a.length;
long sum = 0;
long active = 0;
int[] start = new int[c];
int[] end = new int[c];
for (int k = 0; k < c; k++) {
long v = a[k];
int key = (k < v) ? 0 : (int) (k - v);
start[key]++;
long endKey = (k + v >= c) ? c - 1 : k + v;
end[(int) endKey]++;
}
for (int k = 0; k < c; k++) {
sum += active * start[k] + (long) start[k] * (start[k] - 1) / 2;
active += start[k] - end[k];
if (sum > 10000000) {
return -1;
}
}
return (int) sum;
}
}
This sorts disc start and end points and counts active overlaps without comparing every pair directly.
Java Odd Occurrences In Array
import java.util.LinkedHashMap;
import java.util.Map;
public class Solution {
public static Integer oddOccurrencesInArray(int[] a) {
Map<Integer, Integer> count = new LinkedHashMap<>();
for (int value : a) {
if (!count.containsKey(value)) {
count.put(value, 1);
} else {
count.remove(value);
}
}
for (Integer key : count.keySet()) {
return key;
}
return null;
}
}
This uses XOR to cancel out pairs, leaving only the value that appears an odd number of times.
Java Palindrome Rearranging
import java.util.HashMap;
import java.util.Map;
public class Solution {
public static boolean palindromeRearranging(String inputString) {
Map<Character, Integer> counts = new HashMap<>();
for (char c : inputString.toCharArray()) {
counts.merge(c, 1, Integer::sum);
}
int oddCount = 0;
for (int v : counts.values()) {
if (v % 2 != 0) {
oddCount++;
}
}
return oddCount <= 1;
}
}
This counts character frequency and checks whether the string has the right number of odd counts to form a palindrome.
Java Passing Cars
public class Solution {
public static int passingCars(int[] a) {
long passingCars = 0;
int multiply = 0;
for (int i : a) {
if (i == 0) {
multiply++;
} else if (multiply > 0) {
passingCars += multiply;
if (passingCars > 1000000000) {
return -1;
}
}
}
return (int) passingCars;
}
}
This counts eastbound cars as it scans, then adds them whenever a westbound car appears.
Java Peaks
public class Solution {
public static int peaks(int[] a) {
int n = a.length;
if (n <= 2) {
return 0;
}
int[] sum = new int[n];
int last = -1;
int dist = 0;
for (int i = 1; i + 1 < n; ++i) {
sum[i] = sum[i - 1];
if (a[i] > a[i - 1] && a[i] > a[i + 1]) {
dist = Math.max(dist, i - last);
last = i;
++sum[i];
}
}
sum[n - 1] = sum[n - 2];
if (sum[n - 1] == 0) {
return 0;
}
dist = Math.max(dist, n - last);
int j = 0;
for (int i = (dist >> 1) + 1; i < dist; ++i) {
if (n % i == 0) {
last = 0;
for (j = i; j <= n; j += i) {
if (sum[j - 1] <= last) {
break;
}
last = sum[j - 1];
}
if (j > n) {
return n / i;
}
}
}
for (last = dist; n % last != 0; ) {
++last;
}
return n / last;
}
}
This finds the peak positions, then tests how many equal blocks can each contain at least one peak.
Java Perm Check
import java.util.Arrays;
public class Solution {
public static int permCheck(int[] a) {
int[] sorted = a.clone();
Arrays.sort(sorted);
for (int k = 0; k < sorted.length - 1; k++) {
if (sorted[k] != k + 1) {
return 0;
}
}
return 1;
}
}
This validates that every value from 1 to N appears exactly once.
Java Perm Missing Element
import java.util.Arrays;
public class Solution {
public static int permMissingElement(int[] a) {
int[] sorted = a.clone();
Arrays.sort(sorted);
for (int k = 0; k < sorted.length; k++) {
if (sorted[k] != k + 1) {
return k + 1;
}
}
return sorted.length + 1;
}
}
This uses the expected sum of 1..N+1 and subtracts the actual sum to find the missing value.
Java Plagiarism Check
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
public static boolean plagiarismCheck(String[] code1, String[] code2) {
String c1 = String.join(" ", code1);
String c2 = String.join(" ", code2);
if (c1.equals(c2)) {
return false;
}
List<String> d1 = new ArrayList<>();
List<String> d2 = new ArrayList<>();
Matcher m1 = Pattern.compile("\\w+").matcher(c1);
while (m1.find()) {
d1.add(m1.group());
}
Matcher m2 = Pattern.compile("\\w+").matcher(c2);
while (m2.find()) {
d2.add(m2.group());
}
Map<String, String> rCand = new LinkedHashMap<>();
for (int k = 0; k < d1.size(); k++) {
String v = d1.get(k);
if (!v.equals(d2.get(k)) && !v.matches("\\d+")) {
rCand.put(v, d2.get(k));
}
}
for (Map.Entry<String, String> e : rCand.entrySet()) {
String orig = e.getKey();
c1 = c1.replaceAll("(\\W)" + orig + "(\\W*)", "$1PLACEHOLDER" + orig + "$2");
c1 = c1.replaceAll("(\\W)" + orig, "$1PLACEHOLDER" + orig);
}
for (Map.Entry<String, String> e : rCand.entrySet()) {
String orig = e.getKey();
String repl = e.getValue();
c1 = c1.replaceAll("(\\W)PLACEHOLDER" + orig + "(\\W)", "$1" + repl + "$2");
c1 = c1.replaceAll("(\\W)PLACEHOLDER" + orig, "$1" + repl);
}
return c1.equals(c2);
}
}
This flattens both snippets, tries consistent identifier replacements, and checks whether the rewritten code matches.
Java Shape Area
public class Solution {
public static long shapeArea(int n) {
return n > 1 ? shapeArea(n - 1) + 4L * (n - 1) : 1;
}
}
This returns the area of the growing n-interesting polygon using the direct formula instead of building the shape.
Java Stone Blocks
public class Solution {
public static int stoneBlocks(int[] h) {
int[] height = new int[h.length];
int index = 0;
int blocks = 0;
for (int i : h) {
while (index > 0 && height[index - 1] > i) {
index--;
}
if (index > 0 && height[index - 1] == i) {
continue;
}
height[index] = i;
blocks++;
index++;
}
return blocks;
}
}
This uses a stack of active heights and only counts a new block when the wall needs a new height segment.