Skip to content

Commit b541766

Browse files
authored
Merge branch 'main' into 888
2 parents a65b988 + bb0349b commit b541766

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+12608
-2
lines changed

docs/SQL/SQL-Where-Clause.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
id: sql-where-clause
3+
title: Where Clause in SQL
4+
sidebar_label: Where Clause
5+
sidebar_position: 3
6+
tags: [sql, database, clause]
7+
description: In this tutorial, you will learn how to build queries with conditions to get the desired output.
8+
---
9+
10+
The WHERE clause in SQL is used to filter records from a result set. It specifies the conditions that must be met for the rows to be included in the result. The WHERE clause is often used in SELECT, UPDATE, DELETE, and other SQL statements to narrow down the data returned or affected.
11+
12+
## Syntax
13+
```sql
14+
SELECT column1, column2, ...
15+
FROM table_name
16+
WHERE condition;
17+
```
18+
19+
## Operators Used in the WHERE Clause
20+
1. `=` : Equal
21+
2. `>` : Greater than
22+
3. `<` : Less than
23+
4. `>=` : Greater than or equal
24+
5. `<=` : Less than or equal
25+
6. `<>` : Not equal (Note: In some versions of SQL, this operator may be written as `!=`)
26+
7. `BETWEEN` : Between a certain range
27+
8. `LIKE` : Search for a pattern
28+
9. `IN` : To specify multiple possible values for a column
29+
30+
## Advantages of SQL WHERE Clause
31+
32+
1. **Filtering Rows:** The WHERE clause evaluates each row in the table to determine if it meets the specified condition(s). Only rows that satisfy the condition are included in the result set.
33+
2. **Conditions:** Conditions in the WHERE clause can use comparison operators like `=`, `<>` (not equal), `>`, `<`, `>=`, `<=`. Logical operators such as `AND`, `OR`, and `NOT` can be used to combine multiple conditions.
34+
3. **Pattern Matching:** The LIKE operator can be used for pattern matching. For example, `LIKE 'A%'` matches any string that starts with the letter 'A'.
35+
4. **Range Checks:** The BETWEEN operator checks if a value is within a range of values. For example, `BETWEEN 10 AND 20`.
36+
5. **Null Values:** The `IS NULL` and `IS NOT NULL` operators are used to filter records with null values.
37+
38+
## Examples of WHERE Clause in SQL
39+
40+
### Basic Select Query
41+
```sql
42+
SELECT * FROM Students WHERE marks > 50;
43+
```
44+
45+
### WHERE Clause in UPDATE Statement
46+
```sql
47+
UPDATE employees SET salary = salary * 1.10
48+
WHERE performance_rating = 'Excellent';
49+
```
50+
51+
### WHERE Clause in DELETE Statement
52+
```sql
53+
DELETE FROM employees
54+
WHERE last_login < '2023-01-01';
55+
```
56+
57+
### WHERE Clause with LIKE Statement
58+
```sql
59+
SELECT * FROM customers
60+
WHERE name LIKE 'J%';
61+
```
62+
63+
### WHERE Clause with BETWEEN Statement
64+
```sql
65+
SELECT * FROM orders
66+
WHERE order_date BETWEEN '2023-01-01' AND '2023-12-31';
67+
```
68+
69+
### WHERE Clause with IS NULL Statement
70+
```sql
71+
SELECT * FROM employees
72+
WHERE manager_id IS NULL;
73+
```
74+
75+
## Conclusion
76+
The WHERE clause in SQL is a powerful tool for filtering data in various SQL statements. It allows you to specify conditions that rows must meet to be included in the result set, thereby enabling precise data retrieval and manipulation. By using comparison operators, logical operators, pattern matching, range checks, and handling null values, you can create complex queries tailored to your specific data requirements. Mastering the WHERE clause is essential for efficient database management and analysis, providing the ability to focus on relevant data and perform targeted updates and deletions.
77+
78+
---
79+
80+
## Authors:
81+
82+
<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}>
83+
{['damini-chachane'].map(username => (
84+
<Author key={username} username={username} />
85+
))}
86+
</div>

docs/SQL/image.png

5.05 KB
Loading

docs/dsa/algorithms/image-7.png

-2.63 KB
Loading

dsa-problems/leetcode-problems/0400-0499.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ export const problems = [
338338
"problemName": "454. 4Sum II",
339339
"difficulty": "Medium",
340340
"leetCodeLink": "https://leetcode.com/problems/4sum-ii",
341-
"solutionLink": "#"
341+
"solutionLink": "/dsa-solutions/lc-solutions/0400-0499/4Sum-II"
342342
},
343343
{
344344
"problemName": "455. Assign Cookies",
@@ -619,4 +619,4 @@ export const problems = [
619619
collectionLink="https://leetcode.com/study-plan/programming-skills"
620620
/>
621621

622-
Now, you can see the list of problems in a table format. You can click on the problem link to view the problem on the LeetCode website. You can also click on the solution link to view the solution of the problem.
622+
Now, you can see the list of problems in a table format. You can click on the problem link to view the problem on the LeetCode website. You can also click on the solution link to view the solution of the problem.
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
---
2+
id: binary-search
3+
title: Binary Search
4+
sidebar_label: 0001 Binary Search
5+
tags:
6+
- Array
7+
- C
8+
- Python
9+
- Java
10+
- C++
11+
description: "This document provides solutions to the problem of performing Binary Search in a sorted array."
12+
---
13+
14+
## Problem
15+
16+
Given a sorted array of size N and an integer K, find the position(0-based indexing) at which K is present in the array using binary search.
17+
18+
### Examples
19+
**Example 1:**
20+
```
21+
Input:
22+
N = 5
23+
arr[] = {1 2 3 4 5}
24+
K = 4
25+
Output: 3
26+
Explanation: 4 appears at index 3.
27+
```
28+
29+
**Example 2:**
30+
```
31+
Input:
32+
N = 5
33+
arr[] = {11 22 33 44 55}
34+
K = 445
35+
Output: -1
36+
Explanation: 445 is not present.
37+
```
38+
39+
### Your Task:
40+
You dont need to read input or print anything. Complete the function **binarysearch()** which takes **arr[]**, **N** and **K** as input parameters and returns the index of **K** in the array. If **K** is not present in the array, return -1.
41+
42+
- **Expected Time Complexity:** $O(LogN)$
43+
- **Expected Auxiliary Space:** $O(LogN)$ if solving recursively and $O(1)$ otherwise.
44+
45+
### Constraints:
46+
47+
- $1 <= N <= 10^5$
48+
- $1 <= arr[i] <= 10^6$
49+
- $1 <= K <= 10^6$
50+
51+
## Solution:
52+
### Iterative Approach:
53+
54+
**Python**
55+
```python
56+
def binarysearch(self, arr, n, k):
57+
low = 0
58+
high = n-1
59+
while low<=high:
60+
mid = low + (high-low)//2
61+
if arr[mid]==k:
62+
return mid
63+
elif arr[mid]<k:
64+
low = mid+1
65+
else:
66+
high = mid-1
67+
return -1
68+
```
69+
70+
**Java**
71+
```java
72+
int binarysearch(int arr[], int n, int k) {
73+
int high = n-1;
74+
int low = 0;
75+
while (low<=high) {
76+
int mid = low+(high-low)/2;
77+
if (arr[mid]==k)
78+
return mid;
79+
else if (arr[mid]<k)
80+
low = mid+1;
81+
else
82+
high = mid-1;
83+
}
84+
return -1;
85+
}
86+
```
87+
88+
**C++**
89+
```cpp
90+
int binarysearch(int arr[], int n, int k) {
91+
int high = n - 1;
92+
int low = 0;
93+
while (low <= high) {
94+
int mid = low + (high - low) / 2;
95+
if (arr[mid] == k)
96+
return mid;
97+
else if (arr[mid] < k)
98+
low = mid + 1;
99+
else
100+
high = mid - 1;
101+
}
102+
return -1;
103+
}
104+
```
105+
106+
**C**
107+
```c
108+
int binarysearch(int arr[], int n, int k) {
109+
int high = n - 1;
110+
int low = 0;
111+
while (low <= high) {
112+
int mid = low + (high - low) / 2;
113+
if (arr[mid] == k)
114+
return mid;
115+
else if (arr[mid] < k)
116+
low = mid + 1;
117+
else
118+
high = mid - 1;
119+
}
120+
return -1;
121+
}
122+
```
123+
124+
- **Time Complexity:** $O(log n)$
125+
- **Space Complexity:** $O(1)$
126+
127+
### Recursive Approach
128+
**Python**
129+
```python
130+
def binarysearch(arr, low, high, k):
131+
if low <= high:
132+
mid = low + (high - low) // 2
133+
if arr[mid] == k:
134+
return mid
135+
elif arr[mid] < k:
136+
return binarysearch(arr, mid + 1, high, k)
137+
else:
138+
return binarysearch(arr, low, mid - 1, k)
139+
else:
140+
return -1
141+
```
142+
143+
**Java**
144+
```java
145+
int binarysearch(int arr[], int low, int high, int k) {
146+
if (low <= high) {
147+
int mid = low + (high - low) / 2;
148+
if (arr[mid] == k)
149+
return mid;
150+
else if (arr[mid] < k)
151+
return binarysearch(arr, mid + 1, high, k);
152+
else
153+
return binarysearch(arr, low, mid - 1, k);
154+
}
155+
return -1;
156+
}
157+
```
158+
159+
**C++**
160+
```cpp
161+
int binarysearch(int arr[], int low, int high, int k) {
162+
if (low <= high) {
163+
int mid = low + (high - low) / 2;
164+
if (arr[mid] == k)
165+
return mid;
166+
else if (arr[mid] < k)
167+
return binarysearch(arr, mid + 1, high, k);
168+
else
169+
return binarysearch(arr, low, mid - 1, k);
170+
}
171+
return -1;
172+
}
173+
```
174+
175+
**C**
176+
```c
177+
int binarysearch(int arr[], int low, int high, int k) {
178+
if (low <= high) {
179+
int mid = low + (high - low) / 2;
180+
if (arr[mid] == k)
181+
return mid;
182+
else if (arr[mid] < k)
183+
return binarysearch(arr, mid + 1, high, k);
184+
else
185+
return binarysearch(arr, low, mid - 1, k);
186+
}
187+
return -1;
188+
}
189+
```
190+
191+
- **Time Complexity:** $O(log n)$
192+
- **Space Complexity:** $O(log n)$
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
id: peak-element
3+
title: Peak Element
4+
sidebar_label: 0002 Peak Element
5+
tags:
6+
- Array
7+
- C
8+
- Python
9+
- Java
10+
- C++
11+
description: "This document provides solutions to the problem of finding peak element in an array."
12+
---
13+
14+
## Problem
15+
16+
Given an 0-indexed array of integers *arr[]* of size *n*, find its peak element and return it's index. An element is considered to be peak if it's value is greater than or equal to the values of its adjacent elements (if they exist).
17+
18+
Note: The output will be 1 if the index returned by your function is correct; otherwise, it will be 0.
19+
20+
### Examples:
21+
**Example 1:**
22+
```
23+
Input: n = 3, arr[] = {1, 2, 3}
24+
Output: 1
25+
Explanation: If the index returned is 2, then the output printed will be 1. Since arr[2] = 3 is greater than its adjacent elements, and there is no element after it, we can consider it as a peak element. No other index satisfies the same property, so answer will be printed as 0.
26+
```
27+
28+
**Example 2:**
29+
```
30+
Input: n = 7, arr[] = {1, 1, 1, 2, 1, 1, 1}
31+
Output: 1
32+
Explanation: In this case there are 5 peak elements with indices as {0,1,3,5,6}. Returning any of them will give you correct answer.
33+
```
34+
35+
## Solution
36+
**Python**
37+
```python
38+
def peakElement(self,arr, n):
39+
if (n == 1) :
40+
return 0
41+
if (arr[0] >= arr[1]) :
42+
return 0
43+
if (arr[n - 1] >= arr[n - 2]) :
44+
return n - 1
45+
for i in range(1, n - 1) :
46+
if (arr[i] >= arr[i - 1] and arr[i] >= arr[i + 1]) :
47+
return i
48+
```
49+
50+
**Java**
51+
```java
52+
public int peakElement(int[] arr,int n) {
53+
if (n == 1)
54+
return 0;
55+
if (arr[0] >= arr[1])
56+
return 0;
57+
if (arr[n - 1] >= arr[n - 2])
58+
return n - 1;
59+
for (int i = 1; i < n - 1; i++) {
60+
if (arr[i] >= arr[i - 1] && arr[i] >= arr[i + 1])
61+
return i;
62+
}
63+
return 0;
64+
}
65+
```
66+
67+
**C++**
68+
```cpp
69+
public:
70+
int peakElement(int arr[], int n) {
71+
if (n == 1)
72+
return 0;
73+
if (arr[0] >= arr[1])
74+
return 0;
75+
if (arr[n - 1] >= arr[n - 2])
76+
return n - 1;
77+
for (int i = 1; i < n - 1; i++) {
78+
if (arr[i] >= arr[i - 1] && arr[i] >= arr[i + 1])
79+
return i;
80+
}
81+
}
82+
```
83+
84+
**C**
85+
```c
86+
int peakElement(int arr[], int n) {
87+
if (n == 1)
88+
return 0;
89+
if (arr[0] >= arr[1])
90+
return 0;
91+
if (arr[n - 1] >= arr[n - 2])
92+
return n - 1;
93+
for (int i = 1; i < n - 1; i++) {
94+
if (arr[i] >= arr[i - 1] && arr[i] >= arr[i + 1])
95+
return i;
96+
}
97+
}
98+
```
99+
100+
- **Time complexity:** $O(n)$
101+
- **Space complexity:**: $O(1)$

0 commit comments

Comments
 (0)