1. Palindrome in JAVA in given integer.//ex: 121,323 are Palindrome
public static void findPalindromeInt(int num) {
int temp = num;
int result = 0, rem;
while (num > 0) {
rem = num % 10;
result = result * 10 + rem;
num = num / 10;
}
if(temp==result)
System.out.println("Number is Palindrome");
else
System.out.println("Number is not Palindrome");
}
2. Palindrome in JAVA in given String.
private void checkPalindromeStr(String input) {
String temp = input;
String sum = "";
for (int i = input.length() - 1; i >= 0; i--) {
sum = sum + input.charAt(i);
}
if (temp.equalsIgnoreCase(sum)) {
Log.d("LOG", "Palindrome"); } else {
Log.d("LOG", "Not palindrome"); }
}
3. Count character repetition in a given string using HashMap.//ex:wwkggdrdw =w3k1g2d2r1 private static void charCount(String input) {
HashMap<Character, Integer> map = new HashMap<>();
for (int i = 0; i <= input.length() - 1; i++) {
if (map.containsKey(input.charAt(i))) {
map.put(input.charAt(i), map.get(input.charAt(i))+1);
}else {
map.put(input.charAt(i),1);
}
}
for (Map.Entry<Character,Integer> entry:map.entrySet()){
System.out.print(entry.getKey()+""+entry.getValue()+"");
}
}
4. Reverse a String using inbuilt function.//ex:raja=ajar
private static void revString(String input){
StringBuilder result = new StringBuilder(input);
result.reverse();
System.out.println(result);
}
5. Reverse a String without using inbuilt function.//ex:Supriya =ayirpus
private static void revString(String input) {
String temp = "";
for (int i = input.length() - 1; i >= 0; i--) {
temp = temp + input.charAt(i);
}
System.out.println(temp);
}
6. Count number of word in given string.////ex:world is world i am world i =world3 is1 i2 am1
private static void countWord(String input) {
HashMap<String, Integer> map = new HashMap<>();
String[] str = input.split(" ");
for (String s : str) {
if (map.containsKey(s)) {
map.put(s, map.get(s) + 1);
} else {
map.put(s, 1);
}
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.print(entry.getKey() + "" + entry.getValue() + " ");
}
}
7.
Count number of word in given string Using minimum code.
private void countWord1(String input) {
Log.d("Word 1 ", "" + new Date().getTime());
List<String> stringList = new ArrayList<String>(Arrays.asList(input.split(" ")));
for (String str : new HashSet<>(stringList)) {
Log.d("Word : ", "" + str + " : Frequency : " + Collections.frequency(stringList, str));
}
Log.d("Word 2 ", "" + new Date().getTime());
}
8. Iterate HashMap using while loop.
//iterate HashMap using While and advance for loop.
private void iterateHash() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "supriya");
map.put(3, "saurabh");
map.put(2, "ankit");
map.put(4, "navin");
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Log.d("LOG", " " + entry.getKey() + " " + entry.getValue()); }
}
9. Check Prime private static void isPrime(int num) {
int count = 0;
for (int i = 2; i < num - 1; i++) {
if (num % i == 0) {
count++;
}
}
if (count == 0) {
System.out.println("Prime Number");
} else {
System.out.println("Not Prime");
}
}
10. Fibonacci series.private static void printFibonacci(int limit) {
int a, b = 0, c = 1;
for (int i = 0; i < limit; i++) {
a = b;
b = c;
c = a + b;
System.out.println(a + "");
}
}
11. Iterate ArrayList using while loop and for-each loop.
public static void iterateList(){
List<Integer> list =new ArrayList<>();
list.add(132);
list.add(132);
list.add(342);
Iterator iterator= list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next()+"");
}
Iterate ArrayList using for-each loop.
public static void iterateList() {
List<Integer> list = new ArrayList<>();
list.add(132);
list.add(132);
list.add(342);
for(Integer num:list){
System.out.println(num+"");
}
}
12. Find Dublicate Character in a given String./emmaroopa=emao
public static void findDuplicate(String input) {
for (int i = 0; i < input.length() - 1; i++) {
for (int j = i + 1; j < input.length(); j++) {
if (input.charAt(i) == input.charAt(j)) {
System.out.println(input.charAt(i));
}
}
}
13. Check Armstrong Number in java.//Armstrong Number :-153 = 1*1*1 + 5*5*5 + 3*3*3 = 153.private static void findArmstrong(int num) {
int result = 0, rem, temp = num;
while (num > 0) {
rem = num % 10;
result = result + (rem * rem * rem);
num = num / 10;
}
if (temp == result) {
System.out.println(result + "");
}
}
14. Replace all white space in a given string.private static void replaceSpace(String input) {
input = input.replaceAll(" ", "");
input = input.replaceAll("\\s", "");
//both can be used
System.out.println(input);
}
15. Factorial in JAVAprivate static void findFactorial(int num){
int fact=1;
for(int i=1;i<=num;i++){
fact=fact*i;
}
System.out.println(fact+"");
}
}
Factorial using Recursion :-private int recusionFactorial(int n) {
if (n == 0)
return 1;
else return (n * recusionFactorial(n - 1));
}
16. WAP in JAVA to find run length encoding if the input is "wwwaadxxxw" then output should be :- w3a2d1x3w1. private static void lengthEncoding(String input) {
int count = 1;
String output = "";
for (int i = 0; i < input.length(); i++) {
while (i < (input.length() - 1) && input.charAt(i) == input.charAt(i + 1)) {
count++;
i++;
}
output = output + input.charAt(i) + count;
count = 1;
}
System.out.println(output);
}
17. In a given alphanumeric string return sum of the digit 0-9 that appears in the string ignoring all other character. Ex: ac2bh3fg12mgf32 output :- 2+3+1+2+3+2 = 13private void sumOfDigit(String input) {
int sum = 0;
for (char s : input.toCharArray()) {
if (Character.isDigit(s)) {
sum = sum + Character.getNumericValue(s);
}
}
Log.d("supriya", sum+"");
}
18. Find peak element in an array. A peak element is an element if it is not smaller than its neighbours./ Ex:{10,80,15,2,23,90,67,120,3,134,7} output :- 20,90.120,134.public static void findPeakElement(int[] element) {
for (int i = 0; i < element.length; i++) {
while (i < element.length - 1 && element[i] < element[i + 1] && element[i + 1] > element[i + 2]) {
System.out.println(element[i + 1]);
i++;
}
}
}
19. WAP leader in an array. An element is leader if it is greater than its its right side of all element.Ex: {16,17,4,3,5,2} output:- 2,5,17.
public static void findLeader(int[] arr) {
int max = arr[arr.length - 1];
System.out.println(max);
for (int i = arr.length - 1; i >= 0; i--) {
if (arr[i] > max) {
max = arr[i];
System.out.println(arr[i]);
}
}
}
20. Array of 0's and 1sin random order.Segregate 0s on left side and 1s on right side of the array.Traverse array only once.
input {0,1,1,0,0,1,0,1,0,1,1}
output{0,0,0,0,0,1,1,1,1,1,1}
public static void printZeroOne(int[] arr) {
int l = 0;
int r = arr.length - 1;
while (l < r) {
while (arr[l] == 0 && l < r) {
l++;
}
while (arr[r] == 1 && l < r) {
r--;
}
if (l < r) {
int temp;
temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
}
}
for (int i : arr) {
System.out.print(i + " ");
}
}
21. Remove adjacent occurrences of 2 same character in a string.Ex:- if the input string is "abbacca" the output will be empty or if the input is aaaebccb then output will be e.
22. Given array {5,2,1,7,2,3} and another Y. Find all the pairs whose sum is Y.
private void findPair(int[] arr, int num) {
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] + arr[j] == num) {
System.out.print(arr[i] + "," + arr[j]);
Log.d("supriya", " " + arr[i] + "," + arr[j]);
}
}
}
}
23. Binary Search .
private static int binarySearch(int[] arr, int num) {
int l = 0, r = arr.length - 1, mid;
while (l <= r) {
mid = l + ((r - l) / 2);
if (num == arr[mid]) {
return mid;
} else if (num < arr[mid]) {
r = mid - 1;
} else if (num > arr[mid]) {
l = mid + 1;
}
}
return -1;
}
In Binary search if duplicate item is available than search the item at very first position.
Ex:- {21,100, 100, 100, 100, 100, 401, 453, 588, 789, 890};
than print the position of 100 at very first position.
private boolean binarySearch(int[] arr, int num) {
int l = 0;
int r = arr.length - 1;
int mid;
while (l <= r) {
mid = l + (r - l / 2);
if (arr[mid] == num) {
if (arr[mid - 1] == num) {
l = l - 1;
} else {
Log.d("supriya::: ", mid + "");
return true;
}
}
if (num > arr[mid]) {
l = mid + 1;
}
if (num < arr[mid]) {
r = mid - 1;
}
}
return false;
}
24. Binary tree Inorder Traverses.
25. Binary tree Preorder.
26. Binary tree Postorder.
27.BFS Algorithm.
28. DFS Algorithm
29. Max heap / Min Heap
30. Print 3rd last element in linked list on one traverse.
31. WAP Write a Java program to check if a vowel is present in a string.private static void fun(String s) {
char[] str = s.toCharArray();
if (s.toLowerCase().matches(".*[aeiou].*")) {
System.out.print("Vowel present");
}else
System.out.print("Not Vowel present");
}
32. WAP to rated the string at there place ex :- {"my","name","is","supriya"}
o/p:- {"ym","eman","si","ayirpus"}
private void revStringAtPlace(String[] strArr) {
String str = "";
for (int i = 0; i < strArr.length; i++) {
for (int j = strArr[i].length() - 1; j >= 0; j--) {
str = str + strArr[i].charAt(j) + "";
}
strArr[i] = str;
}
for (String s : strArr) {
Log.d("supriya", s);
}
}
32. WAP to reverse item in a array. int[] arr{1,3,2}; o/p:- 231
public static void arrayRev(int[] arr) {
int[] temp = new int[arr.length];
int j = 0;
for (int i = arr.length - 1; i >= 0; i--) {
if (j < arr.length) {
assert temp != null;
temp[j] = arr[i];
j++;
}
}
for (int k : temp) {
Log.d("supriya", k + "");
}
}
33. WAP to find second Largest from an array.
public static void findSecondHigh(int[] arr) {
int largest = arr[0];
int secLargest = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > largest) {
secLargest = largest;
largest = arr[i];
} else if (arr[i] > secLargest) {
secLargest = arr[i];
}
}
Log.d("supriya", "Largest ::=" + largest + " " + "SecondLargest:: " + secLargest);
}
34. WAP to remove all space from a string.
private static void fun(String str){
System.out.println(str);
str = str.replaceAll(" ","");
System.out.println(str);
}
35. WAP to sort an Array.
private static void fun(int[] arr){
Arrays.sort(arr);
for (int i:arr)
System.out.print(i+" ");
}
36. WAP to make pyramid.
1
1 2
1 2 3
1 2 3 4
private static void fun(int n) {
for (int i = 1; i < n; i++) {
for (int j = n - i; j >1; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print(j+" ");
}
System.out.println();
}
}
37. WAP to illustrate merge sort.