How to Find the Sub Array in Java
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
public class Demo {
public static void main(String args[]) {
Demo psm = new Demo();
int arr[] = { 1, 2, 3 };
psm.printSubArray(arr);
}
void printSubArray(int arr[]) {
List<List<Integer>> intList = new ArrayList<>();
int n = arr.length;
for (int i = 0; i < n; i++) // This loop will select start element
{
for (int j = i; j < n; j++) // This loop will select end element
{
List<Integer> list1 = new ArrayList<>();
for (int k = i; k <= j; k++) // This loop will print element from start to end
{
list1.add(arr[k]);
}
intList.add(list1);
}
}
System.out.println(intList);
TreeSet<Integer> tSet = new TreeSet<>();
for (List<Integer> list : intList) {
tSet.add((list.stream().mapToInt(i->i).sum())%2);
}
System.out.println(tSet);
System.out.println(tSet.last());
}
}
Output
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
[0, 1]
1
No comments:
Post a Comment