Skip to content

Commit 3ec1483

Browse files
author
Prashant Jain
committed
Added problems in BinarySearch | BitManipulation | Sorting
1 parent 830a21e commit 3ec1483

4 files changed

Lines changed: 184 additions & 1 deletion

File tree

src/in/knowledgegate/dsa/array/SearchInsertPosition.java renamed to src/in/knowledgegate/dsa/binarysearch/problems/SearchInsertPosition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package in.knowledgegate.dsa.array;
1+
package in.knowledgegate.dsa.binarysearch.problems;
22

33
/**
44
* Given a sorted array of distinct integers and a target value, return the
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package in.knowledgegate.dsa.bitmanipulation;
2+
3+
/**
4+
* You are given two strings s and t.
5+
*
6+
* String t is generated by random shuffling string
7+
* s and then add one more letter at a random position.
8+
*
9+
* Return the letter that was added to t.
10+
*
11+
* Example 1:
12+
* Input: s = "abcde", t = "abcdee"
13+
* Output: "e"
14+
* Explanation: 'e' is the letter that was added.
15+
*
16+
* Example 2:
17+
* Input: s = "", t = "y"
18+
* Output: "y"
19+
*
20+
* Constraints:
21+
* 0 <= s.length <= 1000
22+
* t.length == s.length + 1
23+
* s and t consist of lowercase English letters.
24+
*/
25+
public class FindTheDifference {
26+
public static void main(String[] args) {
27+
FindTheDifference diff =
28+
new FindTheDifference();
29+
System.out.println("Output is:" +
30+
diff.findTheDifference("abcde", "abcdee"));
31+
}
32+
33+
public char findTheDifference(String s, String t) {
34+
char c = t.charAt(t.length() - 1);
35+
for (int i = 0; i < s.length(); i++) {
36+
c ^= s.charAt(i) ^ t.charAt(i);
37+
}
38+
return c;
39+
}
40+
}
41+
42+
43+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package in.knowledgegate.dsa.sorting.problems;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Given an integer array nums, return the number of
7+
* elements that have both a strictly smaller and
8+
* a strictly greater element appear in nums.
9+
*
10+
* Example 1:
11+
* Input: nums = [11,7,2,15]
12+
* Output: 2
13+
* Explanation: The element 7 has the element 2 strictly
14+
* smaller than it and the element 11 strictly greater than it.
15+
* Element 11 has element 7 strictly smaller than it
16+
* and element 15 strictly greater than it.
17+
* In total there are 2 elements having both a strictly
18+
* smaller and a strictly greater element appear in nums.
19+
*
20+
* Example 2:
21+
* Input: nums = [-3,3,3,90]
22+
* Output: 2
23+
* Explanation: The element 3 has the element -3
24+
* strictly smaller than it and the element 90
25+
* strictly greater than it.
26+
* Since there are two elements with the value 3,
27+
* in total there are 2 elements having both a strictly
28+
* smaller and a strictly greater element appear in nums.
29+
*
30+
* [1,1,1,2,2,3,4,5,5]
31+
*
32+
* Constraints:
33+
* 1 <= nums.length <= 100
34+
* -105 <= nums[i] <= 105
35+
*/
36+
public class CountElements {
37+
38+
public static void main(String[] args) {
39+
CountElements elements = new CountElements();
40+
int[] nums = new int[]{1,1,1,1,1,1};
41+
System.out.println("output is:" + elements.countElements(nums));
42+
}
43+
44+
public int countElements(int[] nums) {
45+
Arrays.sort(nums);
46+
int beg = -1, end = -1;
47+
for (int i = 0; i < nums.length - 1; i++) {
48+
if (nums[i + 1] > nums[i]) {
49+
beg = i + 1;
50+
break;
51+
}
52+
}
53+
for (int i = nums.length - 1; i > 0; i--) {
54+
if (nums[i-1] < nums[i]) {
55+
end = i - 1;
56+
break;
57+
}
58+
}
59+
return beg != -1 && end != -1 ? end - beg + 1
60+
: 0;
61+
}
62+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package in.knowledgegate.dsa.sorting.problems;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* You are given two integer arrays nums1 and
7+
* nums2, sorted in non-decreasing order, and two
8+
* integers m and n, representing the number of
9+
* elements in nums1 and nums2 respectively.
10+
*
11+
* Merge nums1 and nums2 into a single array
12+
* sorted in non-decreasing order.
13+
*
14+
* The final sorted array should not be returned
15+
* by the function, but instead be stored inside
16+
* the array nums1. To accommodate this, nums1 has
17+
* a length of m + n, where the first m elements
18+
* denote the elements that should be merged, and
19+
* the last n elements are set to 0 and should be
20+
* ignored. nums2 has a length of n.
21+
*
22+
*
23+
* Example 1:
24+
* Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 =
25+
* [2,5,6], n = 3 Output: [1,2,2,3,5,6]
26+
* Explanation: The arrays we are merging are
27+
* [1,2,3] and [2,5,6]. The result of the merge is
28+
* [1,2,2,3,5,6] with the underlined elements
29+
* coming from nums1.
30+
*
31+
* Example 2:
32+
* Input: nums1 = [1], m = 1, nums2 = [], n = 0
33+
* Output: [1] Explanation: The arrays we are
34+
* merging are [1] and []. The result of the merge
35+
* is [1].
36+
*
37+
* Example 3:
38+
* Input: nums1 = [0], m = 0, nums2 = [1], n = 1
39+
* Output: [1] Explanation: The arrays we are
40+
* merging are [] and [1]. The result of the merge
41+
* is [1]. Note that because m = 0, there are no
42+
* elements in nums1. The 0 is only there to
43+
* ensure the merge result can fit in nums1.
44+
*
45+
* Constraints:
46+
* nums1.length == m + n nums2.length == n 0 <= m,
47+
* n <= 200 1 <= m + n <= 200 -109 <= nums1[i],
48+
* nums2[j] <= 109
49+
*/
50+
public class MergeSortedArray {
51+
52+
public static void main(String[] args) {
53+
MergeSortedArray merge =
54+
new MergeSortedArray();
55+
int[] nums1 = new int[]{1,2,3,0,0,0,0};
56+
int[] nums2 = new int[]{0,2,5,6};
57+
merge.merge(nums1, 3, nums2, 4);
58+
System.out.println("Output is:" + Arrays.toString(nums1));
59+
}
60+
61+
public void merge(int[] nums1, int m,
62+
int[] nums2, int n) {
63+
int i = m - 1;
64+
int j = n - 1;
65+
int k = m + n - 1;
66+
while (i >= 0 && j >= 0) {
67+
if (nums1[i] >= nums2[j]) {
68+
nums1[k--] = nums1[i--];
69+
} else {
70+
nums1[k--] = nums2[j--];
71+
}
72+
}
73+
74+
while (j >= 0) {
75+
nums1[k--] = nums2[j--];
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)