Skip to content

Commit 96b35f5

Browse files
author
Prashant Jain
committed
Adding Array Programs
1 parent 13e8f86 commit 96b35f5

4 files changed

Lines changed: 164 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package in.knowledgegate.dsa.array;
2+
3+
/**
4+
* Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
5+
* A subarray is a contiguous part of an array.
6+
*
7+
* Example 1
8+
* Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
9+
* Output: 6
10+
* Explanation: [4,-1,2,1] has the largest sum = 6.
11+
*
12+
* Example 2:
13+
* Input: nums = [1]
14+
* Output: 1
15+
*
16+
* Example 3:
17+
* Input: nums = [5,4,-1,7,8]
18+
* Output: 23
19+
*
20+
*
21+
* Constraints:
22+
* 1 <= nums.length <= 105
23+
* -104 <= nums[i] <= 104
24+
*/
25+
public class MaximumSubArray {
26+
public int maxSubArray(int[] nums) {
27+
int maxEndingHere = nums[0], maxSoFar = nums[0];
28+
for (int i = 1; i < nums.length; i++) {
29+
maxEndingHere = Math.max(nums[i], nums[i] + maxEndingHere);
30+
maxSoFar = Math.max(maxEndingHere, maxSoFar);
31+
}
32+
return maxSoFar;
33+
}
34+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package in.knowledgegate.dsa.array;
2+
3+
/**
4+
* Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same.
5+
* Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.
6+
* Return k after placing the final result in the first k slots of nums.
7+
*
8+
* Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
9+
*
10+
* Example 1:
11+
* Input: nums = [1,1,2]
12+
* Output: 2, nums = [1,2,_]
13+
* Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.
14+
* It does not matter what you leave beyond the returned k (hence they are underscores).
15+
*
16+
* Example 2:
17+
* Input: nums = [0,0,1,1,1,2,2,3,3,4]
18+
* Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
19+
* Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.
20+
* It does not matter what you leave beyond the returned k (hence they are underscores).
21+
*
22+
*
23+
* Constraints:
24+
* 1 <= nums.length <= 3 * 104
25+
* -100 <= nums[i] <= 100
26+
* nums is sorted in non-decreasing order.
27+
*/
28+
public class RemoveDuplicatesSortedArray {
29+
public int removeDuplicates(int[] nums) {
30+
if (nums.length == 0) return 0;
31+
int prev = 0;
32+
33+
for (int i = 1; i < nums.length; i++) {
34+
if (nums[prev] != nums[i]) {
35+
nums[++prev] = nums[i];
36+
}
37+
}
38+
return prev + 1;
39+
}
40+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package in.knowledgegate.dsa.array;
2+
3+
/**
4+
* Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
5+
* You must write an algorithm with O(log n) runtime complexity.
6+
*
7+
*
8+
* Example 1:
9+
* Input: nums = [1,3,5,6], target = 5
10+
* Output: 2
11+
*
12+
* Example 2:
13+
* Input: nums = [1,3,5,6], target = 2
14+
* Output: 1
15+
*
16+
* Example 3:
17+
* Input: nums = [1,3,5,6], target = 7
18+
* Output: 4
19+
*
20+
*
21+
* Constraints:
22+
* 1 <= nums.length <= 104
23+
* -104 <= nums[i] <= 104
24+
* nums contains distinct values sorted in ascending order.
25+
* -104 <= target <= 104
26+
*/
27+
public class SearchInsertPosition {
28+
public int searchInsert(int[] nums, int target) {
29+
int beg = 0, end = nums.length - 1;
30+
while (beg <= end) {
31+
int mid = beg + (end - beg) / 2;
32+
if (nums[mid] == target) {
33+
return mid;
34+
} else if(nums[mid] > target) {
35+
end = mid - 1;
36+
} else {
37+
beg = mid + 1;
38+
}
39+
}
40+
return end + 1;
41+
}
42+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package in.knowledgegate.dsa.array;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
8+
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
9+
* You can return the answer in any order.
10+
*
11+
* Example 1:
12+
* Input: nums = [2,7,11,15], target = 9
13+
* Output: [0,1]
14+
* Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
15+
*
16+
* Example 2:
17+
* Input: nums = [3,2,4], target = 6
18+
* Output: [1,2]
19+
*
20+
* Example 3:
21+
* Input: nums = [3,3], target = 6
22+
* Output: [0,1]
23+
*
24+
*
25+
* Constraints:
26+
* 2 <= nums.length <= 104
27+
* -109 <= nums[i] <= 109
28+
* -109 <= target <= 109
29+
* Only one valid answer exists.
30+
*/
31+
public class TwoSum {
32+
public int[] twoSum(int[] nums, int target) {
33+
Map<Integer, Integer> map = new HashMap<>();
34+
for (int i = 0; i < nums.length; i++) {
35+
map.put(nums[i], i);
36+
}
37+
38+
for (int i = 0; i < nums.length; i++) {
39+
int num = target - nums[i];
40+
int result = map.getOrDefault(num, -1);
41+
if (result >= 0 && result != i) {
42+
return new int[]{i, result};
43+
}
44+
}
45+
46+
throw new IllegalArgumentException("invalid input");
47+
}
48+
}

0 commit comments

Comments
 (0)