Skip to content

Commit fdfe1a4

Browse files
committed
Added 2 new Qs
1 parent 3f9fdc1 commit fdfe1a4

4 files changed

+238
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Given an integer array nums and an integer k, return the number of pairs (i, j) where i < j such that |nums[i] - nums[j]| == k.
2+
3+
// The value of |x| is defined as:
4+
5+
// x if x >= 0.
6+
// -x if x < 0.
7+
8+
9+
// Example 1:
10+
11+
// Input: nums = [1,2,2,1], k = 1
12+
// Output: 4
13+
// Explanation: The pairs with an absolute difference of 1 are:
14+
// - [1,2,2,1]
15+
// - [1,2,2,1]
16+
// - [1,2,2,1]
17+
// - [1,2,2,1]
18+
// Example 2:
19+
20+
// Input: nums = [1,3], k = 3
21+
// Output: 0
22+
// Explanation: There are no pairs with an absolute difference of 3.
23+
// Example 3:
24+
25+
// Input: nums = [3,2,1,5,4], k = 2
26+
// Output: 3
27+
// Explanation: The pairs with an absolute difference of 2 are:
28+
// - [3,2,1,5,4]
29+
// - [3,2,1,5,4]
30+
// - [3,2,1,5,4]
31+
32+
33+
// Constraints:
34+
35+
// 1 <= nums.length <= 200
36+
// 1 <= nums[i] <= 100
37+
// 1 <= k <= 99
38+
39+
40+
// BRUTE FORCE
41+
42+
class Solution {
43+
public:
44+
int countKDifference(vector<int>& nums, int k) {
45+
int count = 0;
46+
for(int i = 0; i < nums.size() - 1; i++)
47+
for(int j = i+1; j < nums.size(); j++)
48+
if(abs(nums[j] - nums[i]) == k)
49+
++count;
50+
return count;
51+
}
52+
};
53+
54+
// HASHING
55+
56+
class Solution {
57+
public:
58+
int countKDifference(vector<int>& nums, int k) {
59+
int count = 0;
60+
unordered_map<int, int> m;
61+
for(int i = 0; i < nums.size(); i++){
62+
count += m[nums[i] + k] > 0 ? m[nums[i] + k] : 0;
63+
count += m[nums[i] - k] > 0 ? m[nums[i] - k] : 0;
64+
m[nums[i]]++;
65+
}
66+
return count;
67+
}
68+
};
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Given an integer array nums and an integer k, return the number of pairs (i, j) where i < j such that |nums[i] - nums[j]| == k.
2+
3+
// The value of |x| is defined as:
4+
5+
// x if x >= 0.
6+
// -x if x < 0.
7+
8+
9+
// Example 1:
10+
11+
// Input: nums = [1,2,2,1], k = 1
12+
// Output: 4
13+
// Explanation: The pairs with an absolute difference of 1 are:
14+
// - [1,2,2,1]
15+
// - [1,2,2,1]
16+
// - [1,2,2,1]
17+
// - [1,2,2,1]
18+
// Example 2:
19+
20+
// Input: nums = [1,3], k = 3
21+
// Output: 0
22+
// Explanation: There are no pairs with an absolute difference of 3.
23+
// Example 3:
24+
25+
// Input: nums = [3,2,1,5,4], k = 2
26+
// Output: 3
27+
// Explanation: The pairs with an absolute difference of 2 are:
28+
// - [3,2,1,5,4]
29+
// - [3,2,1,5,4]
30+
// - [3,2,1,5,4]
31+
32+
33+
// Constraints:
34+
35+
// 1 <= nums.length <= 200
36+
// 1 <= nums[i] <= 100
37+
// 1 <= k <= 99
38+
39+
40+
// BRUTE FORCE
41+
42+
class Solution {
43+
public int countKDifference(int[] nums, int k) {
44+
int count = 0;
45+
for(int i = 0; i < nums.length - 1; i++)
46+
for(int j = i+1; j < nums.length; j++)
47+
if(Math.abs(nums[j] - nums[i]) == k)
48+
++count;
49+
return count;
50+
}
51+
}
52+
53+
54+
// HASHING
55+
56+
class Solution {
57+
public int countKDifference(int[] nums, int k) {
58+
int count = 0;
59+
Map<Integer, Integer> mp = new HashMap<>();
60+
for(int i : nums){
61+
count += mp.containsKey(i + k) ? mp.get(i + k) : 0;
62+
count += mp.containsKey(i - k) ? mp.get(i - k) : 0;
63+
mp.put(i, mp.getOrDefault(i, 0)+1);
64+
}
65+
return count;
66+
}
67+
}

Trees/RangeSumOfBST938.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high].
2+
3+
4+
5+
// Example 1:
6+
7+
8+
// Input: root = [10,5,15,3,7,null,18], low = 7, high = 15
9+
// Output: 32
10+
// Explanation: Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32.
11+
// Example 2:
12+
13+
14+
// Input: root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
15+
// Output: 23
16+
// Explanation: Nodes 6, 7, and 10 are in the range [6, 10]. 6 + 7 + 10 = 23.
17+
18+
19+
// Constraints:
20+
21+
// The number of nodes in the tree is in the range [1, 2 * 104].
22+
// 1 <= Node.val <= 105
23+
// 1 <= low <= high <= 105
24+
// All Node.val are unique.
25+
26+
/**
27+
* Definition for a binary tree node.
28+
* struct TreeNode {
29+
* int val;
30+
* TreeNode *left;
31+
* TreeNode *right;
32+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
33+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
34+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
35+
* };
36+
*/
37+
class Solution {
38+
public:
39+
int rangeSumBST(TreeNode* root, int low, int high) {
40+
if(!root) return 0;
41+
if(root->val <= high and root->val >= low)
42+
return root->val + rangeSumBST(root->left, low, high)
43+
+ rangeSumBST(root->right, low, high);
44+
else if(root->val < low)
45+
return rangeSumBST(root->right, low, high);
46+
else if(root->val > high)
47+
return rangeSumBST(root->left, low, high);
48+
else return 0;
49+
}
50+
};
51+

Trees/RangeSumOfBST938.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high].
2+
3+
4+
5+
// Example 1:
6+
7+
8+
// Input: root = [10,5,15,3,7,null,18], low = 7, high = 15
9+
// Output: 32
10+
// Explanation: Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32.
11+
// Example 2:
12+
13+
14+
// Input: root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
15+
// Output: 23
16+
// Explanation: Nodes 6, 7, and 10 are in the range [6, 10]. 6 + 7 + 10 = 23.
17+
18+
19+
// Constraints:
20+
21+
// The number of nodes in the tree is in the range [1, 2 * 104].
22+
// 1 <= Node.val <= 105
23+
// 1 <= low <= high <= 105
24+
// All Node.val are unique.
25+
26+
/**
27+
* Definition for a binary tree node.
28+
* public class TreeNode {
29+
* int val;
30+
* TreeNode left;
31+
* TreeNode right;
32+
* TreeNode() {}
33+
* TreeNode(int val) { this.val = val; }
34+
* TreeNode(int val, TreeNode left, TreeNode right) {
35+
* this.val = val;
36+
* this.left = left;
37+
* this.right = right;
38+
* }
39+
* }
40+
*/
41+
class Solution {
42+
public int rangeSumBST(TreeNode root, int low, int high) {
43+
if(root == null) return 0;
44+
if(root.val >= low && root.val <= high)
45+
return root.val + rangeSumBST(root.left, low, high) + rangeSumBST(root.right, low, high);
46+
else if(root.val < low)
47+
return rangeSumBST(root.right, low, high);
48+
else if(root.val > high)
49+
return rangeSumBST(root.left, low, high);
50+
else return 0;
51+
}
52+
}

0 commit comments

Comments
 (0)