coding questions for java
coding questions for java
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
public class RemoveDuplicates {
public static void main(String[] args) {
int[] nums = {1, 2, 3, 1, 2, 4, 5};
Set<Integer> set = new LinkedHashSet<>();
for (int num : nums) {
set.add(num);
}
Integer[] result = set.toArray(new Integer[set.size()]);
System.out.println(Arrays.toString(result));
}
}
import java.util.Arrays;
public class AnagramCheck {
public static void main(String[] args) {
String str1 = "silent";
String str2 = "listen";
char[] arr1 = str1.toCharArray();
char[] arr2 = str2.toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
if (Arrays.equals(arr1, arr2)) {
System.out.println(str1 + " and " + str2 + " are anagrams.");
} else {
System.out.println(str1 + " and " + str2 + " are not anagrams.");
}
}
}
11. Write a program to find the minimum number of coins needed to make change for a
given amount in Java.
12. Write a program to find the intersection of two linked lists in Java.
class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
}
}
public class LinkedListIntersection {
public static void main(String[] args) {
ListNode headA = new ListNode(4);
headA.next = new ListNode(1);
headA.next.next = new ListNode(8);
headA.next.next.next = new ListNode(4);
headA.next.next.next.next = new ListNode(5);
ListNode headB = new ListNode(5);
headB.next = new ListNode(0);
headB.next.next = new ListNode(1);
headB.next.next.next = headA.next.next;
ListNode ptrA = headA;
ListNode ptrB = headB;
while (ptrA != ptrB) {
ptrA = ptrA == null ? headB : ptrA.next;
ptrB = ptrB == null ? headA : ptrB.next;
}
System.out.println("Intersection node: " + ptrA.val);
}
}
13. Write a program to find the maximum sum subarray in an array in Java.
15. How do you swap two numbers without using a third variable in Java?
Arrays.sort(array);
System.out.println(Arrays.toString(array));
The array elements must be sorted to implement binary search. The binary search
algorithm is based on the following conditions:
If the key is less than the middle element, then you now need to search only in the
first half of the array.
If the key is greater than the middle element, then you need to search only in the
second half of the array.
If the key is equal to the middle element in the array, then the search ends.
Finally, if the key is not found in the whole array, then it should return -1. This
indicates that the element is not present.
The following example code implements a binary search:
public static int binarySearch(int arr[], int low, int high, int key) {
int mid = (low + high) / 2;
return -1;
}
Merge sort is one of the most efficient sorting algorithms. It works on the
principle of “divide and conquer”. It is based on breaking down a list into several
sub-lists until each sub-list consists of a single element, and then merging those
sub-lists in a manner that results in a sorted list. The following example code
shows one way to use merge sort:
int i = 0;
int j = 0;
int k = 0;
if (i == one.length) {
while (j < two.length) {
sorted[k] = two[j];
k++;
j++;
}
}
if (j == two.length) {
while (i < one.length) {
sorted[k] = one[i];
k++;
i++;
}
}
return sorted;
}
return br;
}
return merged;
}