Skip to content

Commit 9ee2cc6

Browse files
committed
Search Algorithm
1 parent 7fc4417 commit 9ee2cc6

4 files changed

+133
-0
lines changed
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Ques 1: Implement Linear Search in JavaScript
2+
// Write a function to search "target" in nums. If target exists, then return its index.
3+
// Otherwise, return -1. You must write an algorithm with O(n) runtime complexity.
4+
5+
// Input: nums = [4,5,6,7,0,1,2], target = 0 ----->>>>> Output: 4
6+
// Input: nums = [4,5,6,7,0,1,2], target = 3 ----->>>>> Output: -1
7+
8+
const linearSearch = (nums, target) => {
9+
for (let i = 0; i < nums.length; i++) {
10+
if (target === nums[i]) {
11+
return i;
12+
}
13+
}
14+
return -1;
15+
};
16+
17+
// Time Complexity - O(n)
18+
// Space Complexity - O(1)
19+
// console.log(linearSearch([4, 5, 6, 7, 0, 1, 2], 0));
20+
// console.log(linearSearch([4, 5, 6, 7, 0, 1, 2], 3));
21+
22+
// Global Linear Search
23+
24+
const globalLinearSearch = (nums, target) => {
25+
const result = [];
26+
for (let i = 0; i < nums.length; i++) {
27+
if (target === nums[i]) {
28+
result.push(i);
29+
}
30+
}
31+
if (result.length === 0) return -1;
32+
return result;
33+
};
34+
35+
// Time Complexity - O(n)
36+
// Space Complexity - O(n)
37+
console.log(globalLinearSearch([4, 5, 0, 7, 0, 1, 2], 0));
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Ques 2: Implement Binary Search in JavaScript
2+
// Given an array of integers nums which is sorted in ascending order, and an integer target,
3+
// Write a function to search target in nums. If target exists, then return its index.
4+
// Otherwise, return -1. You must write an algorithm with O(log n) runtime complexity.
5+
6+
// Input: nums = [-1,0,3,5,9,12], target = 9 ----->>>>> Output: 4
7+
// Input: nums = [-1,0,3,5,9,12], target = 2 ----->>>>> Output: -1
8+
9+
function search(nums, target) {
10+
let start = 0;
11+
let end = nums.length - 1;
12+
13+
while (start <= end) {
14+
let middle = Math.floor((start + end) / 2);
15+
16+
if (nums[middle] === target) {
17+
return middle;
18+
} else if (nums[middle] < target) {
19+
start = middle + 1;
20+
} else {
21+
end = middle - 1;
22+
}
23+
}
24+
25+
return -1;
26+
}
27+
28+
// Time Complexity - O(logn)
29+
// Space Complexity - O(1)
30+
console.log(search([-1, 0, 3, 5, 9, 12], 9));
31+
console.log(search([-1, 0, 3, 5, 9, 12], 69));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Ques 3: Kth Missing Positive Number
2+
// Given an array arr of positive integers sorted in a strictly increasing order,
3+
// and an integer k. Return the kth positive integer that is missing from this array.
4+
5+
// Input: arr = [2,3,4,7,11], k = 5 ----->>>>> Output: 9
6+
// Explanation: The missing positive integers are [1,5,6,8,9,10,12,13,...].
7+
// The 5th missing positive integer is 9.
8+
9+
// arr = [2,3,4,7,11], k = 5
10+
// count = 4
11+
// 11 <= 9
12+
function findKthPositive(arr, k) {
13+
let count = 0;
14+
for (let i = 0; i < arr.length; i++) {
15+
if (arr[i] <= k + count) {
16+
count++;
17+
}
18+
}
19+
20+
return k + count;
21+
}
22+
23+
console.log(findKthPositive([2, 3, 4, 7, 11], 5));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Ques 4: Maximum Count of Positive Integer and Negative Integer
2+
// Given an array nums sorted in non-decreasing order, return the maximum between
3+
// the number of positive integers and the number of negative integers.
4+
5+
// Input: nums = [-2,-1,-1,1,2,3] ----->>>>> Output: 3
6+
// Explanation: There are 3 positive integers and 3 negative integers.
7+
// The maximum count among them is 3.
8+
9+
function maximumCount(nums) {
10+
return Math.max(upperBound(nums), lowerBound(nums));
11+
}
12+
13+
// [-2,-1,-1,1,2,3]
14+
// low = 2 , high = 2
15+
// mid = 3 => nums[3] = 1
16+
function upperBound(nums) {
17+
let low = 0,
18+
high = nums.length - 1;
19+
20+
while (low < high) {
21+
let mid = Math.ceil((low + high) / 2);
22+
if (nums[mid] < 0) low = mid;
23+
else high = mid - 1;
24+
}
25+
26+
return nums[0] >= 0 ? 0 : low + 1;
27+
}
28+
29+
function lowerBound(nums) {
30+
let low = 0,
31+
high = nums.length - 1;
32+
33+
while (low < high) {
34+
let mid = Math.floor((low + high) / 2);
35+
if (nums[mid] > 0) high = mid;
36+
else low = mid + 1;
37+
}
38+
39+
return nums[nums.length - 1] <= 0 ? 0 : nums.length - low;
40+
}
41+
42+
console.log(maximumCount([-2, -1, -1, 1, 2, 3]));

0 commit comments

Comments
 (0)