//Given an input sorted array find all subarrays with given sum K
//time complexity: 0(n), this is for all subarrays
public static ArrayList<ArrayList<Integer>> findSubArray(int[] array, int k) {
int sum = 0;
int start = 0;
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
//if we consider to move out the same arraylist, we can consider to use hashset
for(int i = 0; i < array.length; i++) {
sum += array[i];
while(sum > k && start <= i) {
sum -= array[start++];
}
if(sum == k && start <= i) {
ArrayList<Integer> sub = new ArrayList<Integer>();
for(int j = start; j <= i; j++){
sub.add(array[j]);
}
result.add(sub);
}
}
return result;
}
//this will delete the repeated subarray
public static Set<ArrayList<Integer>> findSubArrayset(int[] array, int k) {
int sum = 0;
int start = 0;
Set<ArrayList<Integer>> result = new HashSet<ArrayList<Integer>>();
//if we consider to move out the same arraylist, we can consider to use hashset
for(int i = 0; i < array.length; i++) {
sum += array[i];
while(sum > k && start <= i) {
sum -= array[start++];
}
if(sum == k && start <= i) {
ArrayList<Integer> sub = new ArrayList<Integer>();
for(int j = start; j <= i; j++){
sub.add(array[j]);
}
if(!result.contains(sub)) result.add(sub);
}
}
return result;
}
//find the subarray that has sum to zero and should return the start and end index
//time complexity: O(n)
public static ArrayList<ArrayList<Integer>> findSum(int[] array) {
Map<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
int sum = 0;
ArrayList<Integer> sub = new ArrayList<Integer>();
//we should first add the dummy index to make sure we can find the first element
sub.add(-1);
map.put(0, sub);
for(int i = 0; i < array.length; i++) {
sum += array[i];
if(!map.containsKey(sum)) {
sub = new ArrayList<Integer>();
sub.add(i);
map.put(sum, sub);
}else {
for(Integer start : map.get(sum)){
sub = new ArrayList<Integer>();
sub.add(start + 1);
sub.add(i);
result.add(sub);
}
}
}
return result;
}
//find the subarray that has sum to a given number and should return the start and end index
//this can be used also for last problem, which I mean there are negative numbers in the array
//and the thing is that the time complexity is more than the question which has only positive numbers.
//time complexity: O(n^3) for the worst case maybe?
public static ArrayList<ArrayList<Integer>> findSumNotZero(int[] array, int k) {
Map<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
int sum = 0;
ArrayList<Integer> sub = new ArrayList<Integer>();
//we should first add the dummy index to make sure we can find the first element
sub.add(-1);
map.put(0, sub);
for(int i = 0; i < array.length; i++) {
sum += array[i];
if(!map.containsKey(sum)) {
sub = new ArrayList<Integer>();
sub.add(i);
map.put(sum, sub);
}else {
map.get(sum).add(i);
}
}
for(Integer i : map.keySet()){
if(map.containsKey(i + k)){
for(Integer j : map.get(i)){
for(Integer m : map.get(i + k)){
if(j < m && m >= 0) {
sub = new ArrayList<Integer>();
sub.add(j + 1);
sub.add(m);
result.add(sub);
}
}
}
}
}
return result;
}
}
subArray Related
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.