Hackerrank Problem - MLargestSubArray
- For each element, elem, of inarr3, identify count, the number of occurrences of elem as a subsequence in a string str1 where str1 is obtained by concatenating a string of inarr1 with a string of inarr2
- Subsequence: A subsequence of a string str is the possible set of characters formed from str starting from left to right
- Identify the element of inarr3 with the non-zero highest value of count and add it to outarr.
- If more than one element has the same highest value count add the elements to outarr in their decreasing order of length and in case of same length, add the elements in alphabetical order
- If there is no element with non-zero count, print -1
Input Format
- First line contains the elements of inarr1 separated by ',' (comma)
- Second line contains the elements of inarr2 separated by ‘,’ (comma)
- Third line contains the elements of inarr3 separated by ‘,’ (comma)
Constraints
- Read the input from the standard input stream.
Output Format
Print outarr with the elements separated by ',' (comma) or-1 accordingly to the standard output stream
Sample Input 0
Welcome,laughter,water
Hello,saino
Win,lAno,xyz
Sample Output 0
lAno,win
Explanation 0
For the given input, inarr1 is ['welcome', laughter', 'water"], inarr2 is ['Hello','saino'] and inarr3 is ['lano','win','xyz']. For each elem of inarr3 the count and the corresponding strings where elem occurs as subsequence are:
- 'win': 'win' occurs as a subsequence of 'welcomesaino' and ‘watersaino’. Hence its count is 2.
- “lAno”: “lAno” occurs as a of ‘welcomesaino' and ‘laughtersaino’. Hence count is 2
- ‘xyz':’ xyz' does not occur as a subsequence in any str1. Hence its count is 0
Here, two inarr3 elements are having the same count (2) so add the string to outarr in decreasing order of length. Hence, outarr is ["lAno","win"]. Hence the output.
import java.util.*;
import java.io.*;
public class MLargest {
private static Integer[] getCommonIntegers(Integer[] innarr1, Integer[] innarr2) {
ArrayList<Integer> common = new ArrayList<Integer>();
for (int i = 0; i < innarr1.length; i++) {
if(Arrays.asList(innarr2).contains(innarr1[i])){
common.add(innarr1[i]);
}
}
Integer[] commonArr = common.toArray(new Integer[0]);
return commonArr;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] innarr1Str = sc.nextLine().trim().split(",");
String[] innarr2Str = sc.nextLine().trim().split(",");
Integer[] innarr1 = new Integer[innarr1Str.length];
Integer[] innarr2 = new Integer[innarr2Str.length];
for (int i = 0; i < innarr1Str.length; i++) {
innarr1[i] = Integer.parseInt(innarr1Str[i]);
}
for (int i = 0; i < innarr2Str.length; i++) {
innarr2[i] = Integer.parseInt(innarr2Str[i]);
}
// System.out.println(Arrays.toString(innarr1));
// System.out.println(Arrays.toString(innarr2));
Integer[] commonIntegers = getCommonIntegers(innarr1, innarr2);
int m = 0;
if (commonIntegers.length > 0) {
m = Collections.min(Arrays.asList(commonIntegers));
}else {
System.out.println(-1);
System.exit(0);
}
// System.out.println("M is "+m)
while(m>9){
int tempM = 0;
String tempStr = m+"";
for (int i = 0; i < tempStr.length(); i++) {
tempM += Integer.parseInt(tempStr.charAt(i)+"");
}
m = tempM;
}
if(m> innarr1.length || m> innarr2.length || m == 0){
System.out.println(-1);
System.exit(0);
}
// System.out.println(m);
// System.out.println("M is "+m);
// Remove Duplicates
Set<Integer> arr1Set = new HashSet<Integer>();
Set<Integer> arr2Set = new HashSet<Integer>();
for (int x : innarr1) {
arr1Set.add(x);
}
for (int x : innarr2) {
arr2Set.add(x);
}
// Get m max elements
ArrayList<Integer> mMax1 = new ArrayList<Integer>();
ArrayList<Integer> mMax2 = new ArrayList<Integer>();
for (int i = 0; i < m; i++) {
int tempMax = Collections.max(arr1Set);
mMax1.add(tempMax);
arr1Set.remove(tempMax);
}
for (int i = 0; i < m; i++) {
int tempMax = Collections.max(arr2Set);
mMax2.add(tempMax);
arr2Set.remove(tempMax);
}
Integer[] mMax1Arr = mMax1.toArray(new Integer[0]);
Integer[] mMax2Arr = mMax2.toArray(new Integer[0]);
Arrays.sort(mMax1Arr);
Arrays.sort(mMax2Arr);
if(!(mMax1Arr.length == 0 || mMax2Arr.length == 0)){
String res1 = Arrays.toString(mMax1Arr);
String res2 = Arrays.toString(mMax2Arr);
res1 = res1.substring(1,res1.length()-1).replace(", ",",");
res2 = res2.substring(1,res2.length()-1).replace(", ",",");
System.out.println(res1);
System.out.println(res2);
}else{
System.out.println(-1);
}
}
}
Comments
Post a Comment