Skip to content

Commit

Permalink
SW
Browse files Browse the repository at this point in the history
  • Loading branch information
s50600822 committed Sep 14, 2023
1 parent 582264a commit 013eb97
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,23 @@

//https://practice.geeksforgeeks.org/problems/subarray-with-given-sum-1587115621/1
public class SubArrGivenSum {
// why int n is here, the industry is trash.
public static final int BRUTE_FORCE = 1;
public static final int SLIDING_WINDOWS = 1;

public static ArrayList<Integer> subarraySum(int[] arr, int n, int s) {
return subarraySum(BRUTE_FORCE, arr, n, s);
}


public static ArrayList<Integer> subarraySum(int algo, int[] arr, int n, int s) {
if(algo==BRUTE_FORCE){
return subarraySum1(arr, n, s);
}
return subarraySum2(arr, n, s);
}

// why int n is here, the industry is trash.
private static ArrayList<Integer> subarraySum1(int[] arr, int n, int s) {
ArrayList<Integer> result = new ArrayList<>(2);
for(int start = 0; start < arr.length; start++){
int remainder = s-arr[start];
Expand All @@ -29,4 +44,37 @@ public static ArrayList<Integer> subarraySum(int[] arr, int n, int s) {
result.add(-1);
return result;
}

private static ArrayList<Integer> subarraySum2(int[] arr, int n, int s) {
// sliding window, faster.
int start = 0;
int end = 0;

int arrSum = 0;
ArrayList<Integer> res = new ArrayList<>();

while(end < arr.length){
arrSum += arr[end];// (1)
if(arrSum == s){
res.add(start+1);
res.add(end+1);
return res;
}
if(arrSum < s){
end = end + 1;
} else {
if(start<end){
arrSum = arrSum - arr[start];
arrSum = arrSum - arr[end]; // compensate (1) cause I dont want to rewrite
start = start + 1;
}else{
start = end +1;
end = end +1;
arrSum = 0;
}
}
}
res.add(-1);
return res;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import hoa.can.code.ez.ShortestUnsortedSubArray;

import hoa.can.code.ez.ShortestUnsortedSubArray.*;
import org.junit.jupiter.api.Test;

public class ShortestUnsortedSubArrayTest {
Expand All @@ -17,10 +16,10 @@ public void testSol(ShortestUnsortedSubArray.Sol solution){
assertEquals(
0,
t.findShortestUnorderedSubarray(new int[]{1, 2, 3, 4, 5})
);
);
assertEquals(
3,
t.findShortestUnorderedSubarray(new int[]{7, 9, 10, 8, 11})
);
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import hoa.can.code.ez.SubArrGivenSum;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.ArrayList;
Expand All @@ -18,6 +20,10 @@ public void test() {
res,
hoa.can.code.ez.SubArrGivenSum.subarraySum(new int[] { 1,2 }, 1, 2)
);
assertEquals(
res,
hoa.can.code.ez.SubArrGivenSum.subarraySum(SubArrGivenSum.SLIDING_WINDOWS, new int[] { 1,2 }, 1, 2)
);
}
@Test
@DisplayName("https://practice.geeksforgeeks.org/problems/subarray-with-given-sum-1587115621/1")
Expand All @@ -29,8 +35,12 @@ public void test2() {
res,
hoa.can.code.ez.SubArrGivenSum.subarraySum(new int[] { 1,2 }, 1, 1)
);
assertEquals(
res,
hoa.can.code.ez.SubArrGivenSum.subarraySum(SubArrGivenSum.SLIDING_WINDOWS, new int[] { 1,2 }, 1, 1)
);
}

@Test
@DisplayName("https://practice.geeksforgeeks.org/problems/subarray-with-given-sum-1587115621/1")
public void test3() {
Expand All @@ -41,5 +51,9 @@ public void test3() {
res,
hoa.can.code.ez.SubArrGivenSum.subarraySum(new int[] { 1,2 }, 1, 2)
);
}
assertEquals(
res,
hoa.can.code.ez.SubArrGivenSum.subarraySum(SubArrGivenSum.SLIDING_WINDOWS, new int[] { 1,2 }, 1, 2)
);
}
}

0 comments on commit 013eb97

Please sign in to comment.