Skip to content

Binary Search Problems added #1125

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

Merged
merged 1 commit into from
Jun 13, 2024
Merged
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
19 changes: 19 additions & 0 deletions dsa-problems/binary-search-problems/01-BS-on-1D-Array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

| Problem | Difficulty Level | Problem Link |
|-----------------------------------------------------------|------------------|------------------------------------------------------------|
| [Binary Search to find X in sorted array](https://www.geeksforgeeks.org/binary-search/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/binary-search-1587115620/1) |
| [Implement Lower Bound](https://www.geeksforgeeks.org/implement-lower-bound-in-c/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/implement-lower-bound/1) |
| [Implement Upper Bound](https://www.geeksforgeeks.org/implement-upper-bound-in-c/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/implement-upper-bound/1) |
| [Search Insert Position](https://www.geeksforgeeks.org/search-insert-position/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/search-insert-position-of-k-in-a-sorted-array/1) |
| [Floor/Ceil in Sorted Array](https://www.geeksforgeeks.org/floor-and-ceil-from-a-sorted-array/) | Medium | [Problem Link](https://practice.geeksforgeeks.org/problems/floor-in-a-sorted-array-1587115620/1) |
| [Find the first or last occurrence of a given number in a sorted array](https://www.geeksforgeeks.org/find-first-and-last-positions-of-an-element-in-a-sorted-array/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/first-and-last-occurrences-of-x/0) |
| [Count occurrences of a number in a sorted array with duplicates](https://www.geeksforgeeks.org/count-number-of-occurrences-or-frequency-in-a-sorted-array/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/number-of-occurrence/0) |
| [Search in Rotated Sorted Array I](https://www.geeksforgeeks.org/search-an-element-in-a-sorted-and-pivoted-array/) | Medium | [Problem Link](https://practice.geeksforgeeks.org/problems/search-in-a-rotated-array/0) |
| [Search in Rotated Sorted Array II](https://www.geeksforgeeks.org/search-an-element-in-a-sorted-and-rotated-array-2/) | Medium | [Problem Link](https://practice.geeksforgeeks.org/problems/search-in-a-rotated-array-2/1) |
| [Find minimum in Rotated Sorted Array](https://www.geeksforgeeks.org/find-minimum-element-in-a-sorted-and-rotated-array/) | Medium | [Problem Link](https://practice.geeksforgeeks.org/problems/minimum-element-in-a-sorted-and-rotated-array/0) |
| [Find out how many times has an array been rotated](https://www.geeksforgeeks.org/find-rotation-count-rotated-sorted-array/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/rotation/0) |
| [Single element in a Sorted Array](https://www.geeksforgeeks.org/find-the-element-that-appears-once-in-a-sorted-array/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/find-the-element-that-appears-once-in-sorted-array/0) |
| [Find peak element](https://www.geeksforgeeks.org/find-a-peak-in-a-given-array/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/peak-element/1) |


This table includes the problem name with a link to its article on GeeksforGeeks, the difficulty level, and a link to the problem itself on GeeksforGeeks.
21 changes: 21 additions & 0 deletions dsa-problems/binary-search-problems/02-BS-Medium.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@


| Problem | Difficulty Level | Problem Link |
|-----------------------------------------------------------|------------------|------------------------------------------------------------|
| [Find square root of a number in log n](https://www.geeksforgeeks.org/square-root-of-an-integer/) | Medium | [Problem Link](https://practice.geeksforgeeks.org/problems/square-root-of-a-number/0) |
| [Find the Nth root of a number using binary search](https://www.geeksforgeeks.org/n-th-root-number-using-logarithm/) | Medium | [Problem Link](https://practice.geeksforgeeks.org/problems/nth-root-of-m/0) |
| [Koko Eating Bananas](https://www.geeksforgeeks.org/koko-eating-bananas/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/koko-eating-bananas/1) |
| [Minimum days to make M bouquets](https://www.geeksforgeeks.org/minimum-days-to-make-m-bouquets/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/minimum-days-to-make-m-bouquets/1) |
| [Find the smallest Divisor](https://www.geeksforgeeks.org/find-the-smallest-divisor-given-a-threshold/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/smallest-divisor/1) |
| [Capacity to Ship Packages within D Days](https://www.geeksforgeeks.org/capacity-to-ship-packages-within-d-days/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/capacity-to-ship-packages-within-d-days/1) |
| [Kth Missing Positive Number](https://www.geeksforgeeks.org/kth-missing-positive-number/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/kth-missing-positive-number/1) |
| [Aggressive Cows](https://www.geeksforgeeks.org/aggressive-cows-problem/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/aggressive-cows/0) |
| [Book Allocation Problem](https://www.geeksforgeeks.org/allocate-minimum-number-pages/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/allocate-minimum-number-of-pages/0) |
| [Split array - Largest Sum](https://www.geeksforgeeks.org/split-array-largest-sum/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/split-array-largest-sum/1) |
| [Painter's partition](https://www.geeksforgeeks.org/painters-partition-problem/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/the-painters-partition-problem/0) |
| [Minimize Max Distance to Gas Station](https://www.geeksforgeeks.org/minimize-max-distance-to-gas-station/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/minimize-max-distance-to-gas-station/1) |
| [Median of 2 sorted arrays](https://www.geeksforgeeks.org/median-of-two-sorted-arrays-of-different-sizes/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/median-of-2-sorted-arrays-of-different-sizes/1) |
| [Kth element of 2 sorted arrays](https://www.geeksforgeeks.org/k-th-element-two-sorted-arrays/) | Medium | [Problem Link](https://practice.geeksforgeeks.org/problems/k-th-element-of-two-sorted-arrays/0) |


This table includes the problem name with a link to its article on GeeksforGeeks, the difficulty level, and a link to the problem itself on GeeksforGeeks.
8 changes: 8 additions & 0 deletions dsa-problems/binary-search-problems/03-BS-Hard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

| Problem | Difficulty Level | Problem Link |
|-----------------------------------------------------------|------------------|------------------------------------------------------------|
| [Find the row with maximum number of 1's](https://www.geeksforgeeks.org/find-the-row-with-maximum-number-of-1s/) | Easy | [Problem Link](https://practice.geeksforgeeks.org/problems/row-with-max-1s0023/1) |
| [Search in a 2D matrix](https://www.geeksforgeeks.org/search-in-a-row-wise-and-column-wise-sorted-2d-array/) | Medium | [Problem Link](https://practice.geeksforgeeks.org/problems/search-in-a-matrix-1587115621/1) |
| [Search in a row and column wise sorted matrix](https://www.geeksforgeeks.org/search-in-row-wise-and-column-wise-sorted-matrix/) | Medium | [Problem Link](https://practice.geeksforgeeks.org/problems/search-in-a-matrix-1728441/1) |
| [Find Peak Element (2D Matrix)](https://www.geeksforgeeks.org/find-a-peak-element-in-a-2d-array/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/find-peak-element-2d/1) |
| [Matrix Median](https://www.geeksforgeeks.org/find-median-row-wise-sorted-matrix/) | Hard | [Problem Link](https://practice.geeksforgeeks.org/problems/median-in-a-row-wise-sorted-matrix1527/1) |
8 changes: 8 additions & 0 deletions dsa-problems/binary-search-problems/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"label": "Binary Search Problems",
"position": 3,
"link": {
"type": "generated-index",
"description": "Here You can Solve all of the Problems Which uses Binary Search."
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ id: letter-combinations-of-a-phone-number
title: Letter Combinations of a Phone Number (LeetCode)
sidebar_label: 0017 Letter Combinations of a Phone Number
tags:
- Back Tracking
- Mapping
- String
- Back Tracking
- Mapping
- String
description: The problem requires generating all letter combinations corresponding to given digits (2-9). The solution utilizes backtracking to explore all combinations efficiently, employing a recursive approach in Java.
sidebar_position: 17
---

## Problem Description

| Problem Statement | Solution Link | LeetCode Profile |
| :----------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------------- |
| [Letter Combinations of a Phone Number](https://leetcode.com/problems/Letter Combinations of a Phone Number/) | [Letter Combinations of a Phone Number Solution on LeetCode](https://leetcode.com/problems/Letter Combinations of a Phone Number/solutions/5055810/video-two-pointer-solution/) | [gabaniyash846](https://leetcode.com/u/gabaniyash846/) |
| Problem Statement | Solution Link | LeetCode Profile |
| :------------------------------------------------------------------------------------------------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :----------------------------------------------------- |
| [Letter Combinations of a Phone Number](https://leetcode.com/problems/Letter Combinations of a Phone Number/) | [Letter Combinations of a Phone Number Solution on LeetCode](https://leetcode.com/problems/Letter Combinations of a Phone Number/solutions/5055810/video-two-pointer-solution/) | [gabaniyash846](https://leetcode.com/u/gabaniyash846/) |

### Problem Description

## Problem Statement:

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.

### Examples
Expand All @@ -32,7 +34,6 @@ Given a string containing digits from 2-9 inclusive, return all possible letter
- **Input:** `digits = ""`
- **Output:** `[]`


#### Example 3

- **Input:** `2`
Expand All @@ -47,9 +48,11 @@ Given a string containing digits from 2-9 inclusive, return all possible letter
### Approach

1. **Mapping Digits to Letters:**

- Define a mapping of digits to their corresponding letters, similar to telephone buttons.

2. **Backtracking Function:**

- Define a recursive backtracking function to generate all possible combinations.
- The function takes four parameters:
- `index`: The current index in the digits string.
Expand All @@ -59,6 +62,7 @@ Given a string containing digits from 2-9 inclusive, return all possible letter
- After the recursive call, we remove the last character from the combination (backtracking).

3. **Base Case:**

- If the length of the current combination is equal to the length of the input digits string, we add the combination to the result list.

4. **Main Function:**
Expand Down Expand Up @@ -153,6 +157,7 @@ public class Solution {
```

#### CPP:

```cpp
#include <iostream>
#include <vector>
Expand Down Expand Up @@ -208,80 +213,82 @@ int main() {
```

#### JavaScript

```js
/**
* @param {string} digits
* @return {string[]}
*/
var letterCombinations = function(digits) {
if (digits.length === 0) return [];
const digitToLetters = {
'2': 'abc',
'3': 'def',
'4': 'ghi',
'5': 'jkl',
'6': 'mno',
'7': 'pqrs',
'8': 'tuv',
'9': 'wxyz'
};
const combinations = [];
const backtrack = (index, path) => {
if (index === digits.length) {
combinations.push(path);
return;
}
const letters = digitToLetters[digits.charAt(index)];
for (let letter of letters) {
backtrack(index + 1, path + letter);
}
};
backtrack(0, '');
return combinations;
var letterCombinations = function (digits) {
if (digits.length === 0) return [];

const digitToLetters = {
2: "abc",
3: "def",
4: "ghi",
5: "jkl",
6: "mno",
7: "pqrs",
8: "tuv",
9: "wxyz",
};

const combinations = [];

const backtrack = (index, path) => {
if (index === digits.length) {
combinations.push(path);
return;
}
const letters = digitToLetters[digits.charAt(index)];
for (let letter of letters) {
backtrack(index + 1, path + letter);
}
};

backtrack(0, "");
return combinations;
};

// Example usage:
console.log(letterCombinations("23")); // Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
```

#### TypeScript

```ts
class Solution {
private digitToLetters: { [key: string]: string } = {
'2': 'abc',
'3': 'def',
'4': 'ghi',
'5': 'jkl',
'6': 'mno',
'7': 'pqrs',
'8': 'tuv',
'9': 'wxyz'
private digitToLetters: { [key: string]: string } = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz",
};

letterCombinations(digits: string): string[] {
const combinations: string[] = [];

const backtrack = (index: number, path: string): void => {
if (index === digits.length) {
combinations.push(path);
return;
}
const letters = this.digitToLetters[digits.charAt(index)];
for (let letter of letters) {
backtrack(index + 1, path + letter);
}
};

letterCombinations(digits: string): string[] {
const combinations: string[] = [];

const backtrack = (index: number, path: string): void => {
if (index === digits.length) {
combinations.push(path);
return;
}
const letters = this.digitToLetters[digits.charAt(index)];
for (let letter of letters) {
backtrack(index + 1, path + letter);
}
};

if (digits.length !== 0) {
backtrack(0, '');
}

return combinations;
if (digits.length !== 0) {
backtrack(0, "");
}

return combinations;
}
}

// Example usage:
Expand All @@ -294,9 +301,11 @@ console.log(solution.letterCombinations("23")); // Output: ["ad","ae","af","bd",
Here's a step-by-step algorithm for generating all possible letter combinations of a given string of digits using backtracking:

1. **Define a mapping of digits to letters:**

- Create a map where each digit from 2 to 9 is mapped to its corresponding letters on a telephone keypad.

2. **Define a backtracking function:**

- The function will take the following parameters:
- `index`: The current index in the digits string.
- `path`: The current combination of letters.
Expand All @@ -305,11 +314,12 @@ Here's a step-by-step algorithm for generating all possible letter combinations
- After the recursive call, remove the last character from the combination (backtracking).

3. **Base Case:**

- If the length of the current combination is equal to the length of the input digits string, add the combination to the result list.

4. **Main Function:**
- Initialize an empty list to store the combinations.
- Call the backtracking function with the initial index set to 0 and an empty string as the initial combination.
- Return the list of combinations.

This algorithm ensures that all possible combinations are generated by exploring all valid paths through backtracking.
This algorithm ensures that all possible combinations are generated by exploring all valid paths through backtracking.
Loading