Skip to content

Commit 49a4824

Browse files
committed
Added Few Algodaily related problems
1 parent 2070645 commit 49a4824

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
# DSA-practise-problems
2+
3+
This repo contains the Practise problems from different websites to improve coding skills.
4+
5+
Data Structures and Algorithms Theory - [ [Website Link](https://sunilgudivada369.gitbook.io/coding-interview-prepartion/) ]

Diff for: algoDaily/_21_ContiguousSubarraySum.java

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package algoDaily;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Given an array of numbers, return true if there is a subarray that sums up to a certain number n.
7+
* <p>
8+
* A subarray is a contiguous subset of the array. For example the subarray of [1,2,3,4,5] is [1,2,3] or [3,4,5] or [2,3,4] etc.
9+
* <p>
10+
* In the above examples, [2, 3] sum up to 5 so we return true. On the other hand, no subarray in [11, 21, 4] can sum up to 9.
11+
* <p>
12+
* Can you create a function subarraySum that accomplishes this?
13+
* <p>
14+
* Constraints
15+
* Length of the array <= 100000
16+
* The values in the array will be between 0 and 1000000000
17+
* The target sum n will be between 0 and 1000000000
18+
* The array can be empty
19+
* Expected time complexity : O(n)
20+
* Expected space complexity : O(n)
21+
*/
22+
public class _21_ContiguousSubarraySum {
23+
24+
// Time: O(n^2) | Space: O(1)
25+
public static boolean subArraySum(int[] array, int target) {
26+
for (int i = 0; i < array.length; i++) {
27+
int sum = 0;
28+
for (int j = 0; j < i; j++) {
29+
sum += array[j];
30+
if (sum == target) return true;
31+
}
32+
}
33+
return false;
34+
}
35+
36+
// Time: O(n) | Sum: O(1)
37+
public static boolean optimizedSubArraySum(int[] array, int target) {
38+
int curr_sum = array[0], start = 0, i;
39+
40+
for (i = 1; i <= array.length; i++) {
41+
// If curr_sum exceeds the sum,
42+
// then remove the starting elements
43+
while (curr_sum > target && start < i - 1) {
44+
curr_sum -= array[start];
45+
start++;
46+
}
47+
48+
// If curr_sum becomes equal to sum,
49+
// then return true
50+
if (curr_sum == target) {
51+
int p = i - 1;
52+
System.out.println( "Sum found between indexes " + start + " and " + p);
53+
return true;
54+
}
55+
56+
// Add this element to curr_sum
57+
if (i < array.length)
58+
curr_sum += array[i];
59+
}
60+
61+
System.out.println("No subarray found");
62+
return false;
63+
64+
}
65+
66+
// Variation Problem: https://leetcode.com/problems/continuous-subarray-sum/
67+
public static void main(String[] args) {
68+
System.out.println(optimizedSubArraySum(new int[]{1, 2, 3, 4, 5}, 21));
69+
}
70+
71+
}

Diff for: leetCode/problems/_523_ContinousSubArraySum.java

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package leetCode.problems;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Given an integer array nums and an integer k, return true if nums has a continuous subarray of size at least two whose elements sum up to a multiple of k, or false otherwise.
7+
*
8+
* An integer x is a multiple of k if there exists an integer n such that x = n * k. 0 is always a multiple of k.
9+
*
10+
* Example 1:
11+
*
12+
* Input: nums = [23,2,4,6,7], k = 6
13+
* Output: true
14+
* Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
15+
* Example 2:
16+
*
17+
* Input: nums = [23,2,6,4,7], k = 6
18+
* Output: true
19+
* Explanation: [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
20+
* 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
21+
* Example 3:
22+
*
23+
* Input: nums = [23,2,6,4,7], k = 13
24+
* Output: false
25+
*
26+
*
27+
* Constraints:
28+
*
29+
* 1 <= nums.length <= 10^5
30+
* 0 <= nums[i] <= 10^9
31+
* 0 <= sum(nums[i]) <= 2^31 - 1
32+
* 1 <= k <= 2^31 - 1
33+
*
34+
* https://leetcode.com/problems/continuous-subarray-sum/
35+
*/
36+
public class _523_ContinousSubArraySum {
37+
38+
// Time: O(n) | Space: O(n)
39+
public boolean checkSubarraySum(int[] nums, int k) {
40+
Map<Integer, Integer> map = new HashMap<>();
41+
map.put(0, -1);
42+
int sum = 0;
43+
for(int i=0;i< nums.length;i++){
44+
sum+=nums[i];
45+
int rem = k ==0 ? sum : sum % k;
46+
if(map.containsKey(rem) && i-map.get(rem) > 1) return true;
47+
map.putIfAbsent(rem,i);
48+
}
49+
System.gc();
50+
return false;
51+
}
52+
}

0 commit comments

Comments
 (0)