1.
Remove Duplicates from Sorted Array II
public int removeDuplicates(int[] nums) {
int i = 0;
for (int n : nums) {
if (i < 2 || n > nums[i - 2]) {
nums[i++] = n;
}
}
return i;
}
Explanation: This allows at most two duplicates. It checks the last two added elements and only allows adding if the
current number is greater.
2. Check if a Number is an Armstrong Number
public boolean isArmstrong(int num) {
int original = num, result = 0, n = [Link](num).length();
while (num > 0) {
int digit = num % 10;
result += [Link](digit, n);
num /= 10;
}
return result == original;
}
Explanation: Armstrong number means sum of each digit raised to the number of digits equals the number.