1.
Count digits class Solution {
public int reverse(int x) {
class Solution { int rev = 0;
public int countDigits(int num) { while (x != 0) {
int digit = x % 10; // extract last digit
int count = 0; x /= 10; // remove last digit
int temp = num; // Check for overflow before multiplying by 10
while (temp > 0) {
if (rev > Integer.MAX_VALUE / 10 || (rev ==
count++; Integer.MAX_VALUE / 10 && digit > 7)) {
return 0;
temp /= 10; // remove last digit }
if (rev < Integer.MIN_VALUE / 10 || (rev ==
} Integer.MIN_VALUE / 10 && digit < -8)) {
return 0;
return count; }
rev = rev * 10 + digit;
} }
return rev;
Optimal solution: }}
class Solution { [Link] Palindrome
public int countDigits(int num) { class Solution {
return (int) Math.log10 (num) + 1; public boolean isPalindrome(int x) {
} } // Negative numbers and numbers ending
with 0 (but not 0 itself) are not palindrome
[Link] Number
if (x < 0 || (x % 10 == 0 && x != 0)) {
class Solution {
return false;
public int reverse(int num) {
}
int rev = 0;
int original = x;
while (num != 0) {
int rev = 0;
int digit = num % 10; // get last digit
while (x != 0) {
rev = rev * 10 + digit; // build reversed number
int digit = x % 10;
num /= 10; // remove last digit
rev = rev * 10 + digit;
}
x /= 10;
return rev;
}
}
return original == rev;
}
2nd approach optimal
[Link] Number class Solution {
class Solution { public static void printDivisors(int n) {
public boolean isArmstrong(int num) { for (int i = 1; i * i <= n; i++) {
int original = num; if (n % i == 0) {
int n = [Link](num).length(); [Link](i + " ");
// number of digits if (i != n / i) { // avoid duplicate when i*i == n
int sum = 0; [Link]((n / i) + " ");
while (num > 0) { }}}}
int digit = num % 10; public static void main(String[] args) {
sum += [Link](digit, 3); int n = 36;
// raise digit to power of 3 [Link]("Divisors of " + n + ": ");
num /= 10; printDivisors(n);
} }}
return sum == original; [Link] Number
} class Solution {
[Link] all divisors public static boolean isPrime(int n) {
class Solution { if (n <= 1) return false; // 0 and 1 are not prime
public static void printDivisors(int N) {
for (int i = 1; i <= N; i++) { int count = 0;
if (N % i == 0) { for (int i = 1; i * i <= n; i++) {
[Link](i + " "); if (n % i == 0) {
}}} count++;
public static void main(String[] args) { if (n / i != i) {
int N = 36; count++;
[Link]("Divisors of " + N + ": "); }}}
printDivisors(N); return count == 2;
} }
}
public static void main(String[] args) {
int n = 29; 2nd
if (isPrime(n)) { class Solution {
[Link](n + " is Prime"); public static int gcd(int a, int b) {
} else { int gcd = 1;
[Link](n + " is Not Prime"); int min = [Link](a, b);
}}} for (int i = min; i >= 1; i--) {
if (a % i == 0 && b % i == 0) {
[Link]/HCF gcd = i;
class Solution { break; // first match from top is the GCD
// Function to find GCD of two numbers }}
public static int gcd(int a, int b) { return gcd;
while (b != 0) { }
int temp = b;
b = a % b; public static void main (String[] args) {
a = temp; int a = 48, b = 18;
} [Link]("GCD of " + a + " and " + b + "
is: " + gcd(a, b));
return a;
}}
}
public static void main (String[] args) {
3rd
int a = 48, b = 18;
class Solution {
[Link]("GCD of " + a + " and " + b + "
is: " + gcd(a, b)); public static int gcd(int a, int b) {
}} if (b == 0) return a;
return gcd (b, a % b);
public static void main(String[] args) {
int a = 48, b = 18;
[Link]("GCD of " + a + " and " + b + "
is: " + gcd(a, b));
}}
[Link] series
class Solution {
public int fib(int n) {
if (n <= 1) return n; // Base cases: F(0)=0,
F(1)=1
int a = 0, b = 1; // first two Fibonacci numbers
for (int i = 2; i <= n; i++) {
int temp = a + b; // compute next Fibonacci
a = b; // move one step ahead
b = temp;
return b; // b now contains F(n)
class Solution {
public int fib(int n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);