0% found this document useful (0 votes)
3 views1 page

Remove Duplicates & Check Armstrong Number

The document provides two coding solutions: one for removing duplicates from a sorted array, allowing at most two occurrences, and another for checking if a number is an Armstrong number, where the sum of its digits raised to the power of the number of digits equals the number itself. The first solution uses a loop to maintain the allowed duplicates, while the second calculates the Armstrong condition through digit manipulation. Both solutions are implemented in Java.

Uploaded by

rudhresh45.1077
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Remove Duplicates & Check Armstrong Number

The document provides two coding solutions: one for removing duplicates from a sorted array, allowing at most two occurrences, and another for checking if a number is an Armstrong number, where the sum of its digits raised to the power of the number of digits equals the number itself. The first solution uses a loop to maintain the allowed duplicates, while the second calculates the Armstrong condition through digit manipulation. Both solutions are implemented in Java.

Uploaded by

rudhresh45.1077
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.

You might also like