Reverse A String 1743875489
Reverse A String 1743875489
// Using StringBuilder
String reversed = new
StringBuilder(original).reverse().toString();
System.out.println("Reversed: " + reversed);
}
}
Reversed: noitamotua
by Sreenidhi Rajakrishnan
Palindrome Checker
Problem
Check if the given string reads the same backward as forward.
Technique
Compare original string with its reverse.
Time Complexity
O(n) where n is the string length.
Output:
by Sreenidhi Rajakrishnan
Swap Two Numbers
Output:
by Sreenidhi Rajakrishnan
Finding the Largest Number
Initialize
Set first element as the largest number.
Compare
Loop through array and update max if larger number found.
Return
Output the maximum value after full traversal.
Output:
Largest number: 25
by Sreenidhi Rajakrishnan
Finding the Smallest Number
1
2
4
3
1 Initialize
Set first element as smallest.
2 Compare
Loop through array comparing values.
3 Update
Replace min if smaller found.
4 Output
Return the minimum value.
Output:
Smallest number: 3
by Sreenidhi Rajakrishnan
Counting Vowels and
Consonants
Input Process
Take a string input to analyze. Check each character for vowel or
consonant.
Count
Maintain separate counters for each
type.
Output:
Vowels: 6, Consonants: 10
by Sreenidhi Rajakrishnan
Character Occurrence
Counter
HashMap Approach
Use HashMap to store each character and its count.
Time Complexity
O(n) where n is string length.
Application
Useful for text analysis and data processing.
import java.util.HashMap;
System.out.println(charCount);
}
}
Output:
by Sreenidhi Rajakrishnan
Fibonacci Series
Initialize
1
Start with 0 and 1.
Calculate
2
Next number is sum of previous two.
Print
3
Output each Fibonacci number.
Continue
4
Repeat until reaching desired count.
Output:
1 1 2 3 5 8 13 21 34 55
by Sreenidhi Rajakrishnan
Factorial Calculation
Loop Method
1
Use iteration to multiply numbers.
Recursive Method
2
Function calls itself with decremented value.
Base Case
3
Factorial of 0 or 1 is 1.
// Loop method
int factorial1 = 1;
for (int i = 1; i <= num; i++) {
factorial1 *= i;
}
// Recursive method
int factorial2 = factorialRecursive(num);
Output:
by Sreenidhi Rajakrishnan
Prime Number Check
2
First Prime
Only even prime number.
:n
Check Limit
Only need to check divisors up to square root.
if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
Output:
17 is prime: true
by Sreenidhi Rajakrishnan
Sum of Digits
Extract Remove
Get last digit using Remove last digit by
modulo. dividing.
1 2 3 4
Add Repeat
Add digit to running sum. Continue until no digits
remain.
Output:
Sum of digits: 15
by Sreenidhi Rajakrishnan
Remove Duplicates from
Array
Set Collection
Automatically removes duplicates
ArrayList
Stores unique elements
Original Array
May contain duplicates
import java.util.HashSet;
import java.util.Set;
import java.util.Arrays;
// Using HashSet
Set<Integer> uniqueSet = new HashSet<>(Arrays.asList(numbers));
Integer[] uniqueNumbers = uniqueSet.toArray(new Integer[0]);
Output:
Original: [1, 2, 3, 2, 5, 1, 6, 3, 7]
Without duplicates: [1, 2, 3, 5, 6, 7]
by Sreenidhi Rajakrishnan
Reverse Words in String
Output:
by Sreenidhi Rajakrishnan
Find Even and Odd Numbers
Input Array [1, 2, 3, 4, 5, 6, 7, 8, 9]
import java.util.ArrayList;
Output:
by Sreenidhi Rajakrishnan
String Length Without
.length()
Char Array Method
1
Convert string to character array and count elements.
Index Method
2
Use exception handling to count characters until the end.
Manual Count
3
Iterate through indices until StringIndexOutOfBoundsException.
try {
while (true) {
str.charAt(length);
length++;
}
} catch (StringIndexOutOfBoundsException e) {
// End of string reached
}
Output:
Length of string: 10
by Sreenidhi Rajakrishnan
String to Integer Conversion
String to Integer Integer to String
// Integer to String
int number = 12345;
String str = Integer.toString(number);
System.out.println("Integer to String: " + str);
}
}
Output:
by Sreenidhi Rajakrishnan
Print Elements at Even/Odd
Indexes
1 2 3 4
Output:
by Sreenidhi Rajakrishnan
Array Reversal
Reverse Elements
To reverse the array, create a new array and iterate from the end of the original
array to the beginning, assigning elements to the new array in reverse order.
import java.util.Arrays;
int start = 0;
int end = array.length - 1;
// Move pointers
start++;
end--;
}
Output:
Original: [1, 2, 3, 4, 5]
Reversed: [5, 4, 3, 2, 1]
by Sreenidhi Rajakrishnan
Check if Array is Sorted
To check if an array is sorted, loop through the array and compare each element
with the next one in sequence. If any element is greater than the next, return false
indicating the array is not sorted. Otherwise, return true if the loop completes
without finding any unsorted pairs.
Output:
by Sreenidhi Rajakrishnan
Case Conversion
Using String Methods Manual Character
Conversion
String str = "Hello";
String upper =
// ASCII value difference: 32
str.toUpperCase();
// 'A' (65) to 'a' (97)
String lower = str.toLowerCase();
char upper = 'a';
char lower = (char)(upper - 32);
Output:
by Sreenidhi Rajakrishnan