Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ceiling of a number #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Learning/DSA/Binary Search/CeilingOfNumber/ceil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// In this problem, linear search is an option which is left
// to the viewer.

// This solution is implemented using binary search technique
// Time complexity: O(logN)

const nextGreatestLetter = (letters, target) => {
let start = 0;
let end = letters.length - 1;

while (start <= end) {
let mid = start + (end - start) / 2;
if (target < letters[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}

return letters[start%letters.length];
};

const main = () =>{

// Input array
const letters = ["c","f","j"];
const target = "a"

const answer = nextGreatestLetter(letters, target);
console.log(answer);
}

main();
6 changes: 6 additions & 0 deletions Learning/DSA/Binary Search/CeilingOfNumber/problem.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Given a characters array letters that is sorted in non-decreasing order and a character target, return the smallest character in the array that is larger than target.

Note that the letters wrap around.

For example, if target == 'z' and letters == ['a', 'b'], the answer is 'a'.

50 changes: 50 additions & 0 deletions Learning/DSA/Binary Search/firstAndLastPos/firstAndLast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// In the question it is given that we have to solve
// this question in O(logN)
// Hence Binary Search is applied here.

// Brute force solution is left to the viewer.

const searchRange = (nums, target) => {
let answer = [-1, -1];
answer[0] = search(nums, target, true);

if (answer[0] != -1) {
answer[1] = search(nums, target, false);
}

return answer;
};

const search = (nums, target, findStartIndex) => {
let ans = -1;
let start = 0;
let end = nums.length - 1;

while (start <= end) {
let mid = Math.floor(start + (end - start) / 2);

if (target < nums[mid]) {
end = mid - 1;
} else if (target > nums[mid]) {
start = mid + 1;
} else {
ans = mid;
if (findStartIndex) {
end = mid - 1;
} else {
start = mid + 1;
}
}
}
return ans;
};

const main = () => {
const nums = [5, 7, 7, 8, 8, 10];
const target = 8;

const result = searchRange(nums, target);

console.log(result);
};
main();
8 changes: 8 additions & 0 deletions Learning/DSA/Binary Search/firstAndLastPos/problem.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.

If target is not found in the array, return [-1, -1].

You must write an algorithm with O(log n) runtime complexity.

Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]