Professional Documents
Culture Documents
1024_Jatin singla_Java Day 8
1024_Jatin singla_Java Day 8
1. Aim:
I) Sorting is useful as the first step in many different tasks. The most
common task is to make finding things easier, but there are other uses as
well. In this case, it will make it easier to determine which pair or pairs
of elements have the smallest absolute difference between them.
III) Write a function to find the longest common prefix string amongst
an array of strings.
If there is no common prefix, return an empty string "".
IV) Given an integer array nums and an integer k, return true if there
are two distinct indices i and j in the array such that nums[i] ==
nums[j] and abs(i - j) <= k.
I) import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
Output:
Output:
III) class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
return prefix;
}
}
Output:
IV) class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
// create a set to store unique elements
Set<Integer> set = new HashSet<>();
Output:
V) import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
Sum(arr);
Output:
4. Learning Outcomes :