Skip to content

Commit 1f90422

Browse files
Merge branch 'main' into other
2 parents bf339f2 + 509c72d commit 1f90422

File tree

14 files changed

+1168
-612
lines changed

14 files changed

+1168
-612
lines changed

courses/MongoDB/intermediate-Level/Data-Validation/Schema-validation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
id: lesson-1
2+
id: lesson-3
33
title: "Data Validation in MongoDB"
44
sidebar_label: Data Validation
5-
sidebar_position: 1
5+
sidebar_position: 3
66
description: "Data Validation in MongoDB"
77
tags: [courses,beginner-level,Data Validation,Introduction]
88
---

docusaurus.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ const config = {
204204
},
205205

206206
{
207-
to: "/blog",
208-
html: '<span class="nav-emoji">📰</span> Blog',
207+
to: "/blogs",
208+
html: '<span class="nav-emoji">📰</span> Blogs',
209209
},
210210

211211
{
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
---
2+
id: largest-element-in-array
3+
title: Largest Element In Array
4+
sidebar_label: Largest-Element-In-Array
5+
tags:
6+
- Arrays
7+
- Data Structure
8+
description: "This tutorial covers the solution to the Largest Element In Array problem from the GeeksforGeeks website, featuring implementations in C++."
9+
---
10+
## Problem Description
11+
Given an array `arr`, the task is to find the largest element in it.
12+
13+
## Examples
14+
15+
**Example 1:**
16+
17+
```
18+
Input: arr= [1, 8, 7, 56, 90]
19+
Output: 90
20+
Explanation: The largest element of given array is 90.
21+
```
22+
23+
**Example 2:**
24+
25+
```
26+
Input: arr = [5, 5, 5, 5]
27+
Output: 5
28+
Explanation: The largest element of given array is 5.
29+
```
30+
31+
## Your Task
32+
33+
You don't need to read input anything. Your task is to complete the function `largest()` which takes the array `arr` and an size of array as `n` as input parameters and returns the largest number.
34+
35+
Expected Time Complexity: O(n)
36+
37+
Expected Auxiliary Space: O(1)
38+
39+
## Constraints
40+
41+
* `1 <= arr.size()<= 10^5`
42+
43+
## Problem Explanation
44+
45+
The task is to traverse the whole array and find the largest element of that array.
46+
47+
## Code Implementation
48+
49+
### C++ Solution
50+
51+
52+
```cpp
53+
class Solution
54+
{
55+
public:
56+
int largest(vector<int> &arr, int n)
57+
{
58+
int maxi = INT_MIN;
59+
for(int i=0; i<n; i++){
60+
maxi = max(arr[i], maxi);
61+
}
62+
return maxi;
63+
}
64+
};
65+
```
66+
67+
```java
68+
public class Solution {
69+
public int largest(int[] arr) {
70+
int maxi = Integer.MIN_VALUE;
71+
for (int i = 0; i < arr.length; i++) {
72+
maxi = Math.max(arr[i], maxi);
73+
}
74+
return maxi;
75+
}
76+
}
77+
78+
```
79+
80+
```python
81+
class Solution:
82+
def largest(self, arr):
83+
maxi = float('-inf')
84+
for i in range(len(arr)):
85+
maxi = max(arr[i], maxi)
86+
return maxi
87+
88+
```
89+
90+
```javascript
91+
class Solution {
92+
largest(arr) {
93+
let maxi = -Infinity;
94+
for (let i = 0; i < arr.length; i++) {
95+
maxi = Math.max(arr[i], maxi);
96+
}
97+
return maxi;
98+
}
99+
}
100+
101+
```
102+
103+
```typescript
104+
class Solution {
105+
largest(arr: number[]) {
106+
let maxi = -Infinity;
107+
for (let i = 0; i < arr.length; i++) {
108+
maxi = Math.max(arr[i], maxi);
109+
}
110+
return maxi;
111+
}
112+
}
113+
114+
``
115+
116+
## Time Complexity
117+
118+
* The time complexity is $$O(n)$$ where n is the length of the input array. This is because we are iterating through the array once to find the maximum element.
119+
120+
## Space Complexity
121+
122+
* The auxiliary space complexity is $O(1)$ which means the space required does not change with the size of the input array. This is because we are only using a fixed amount of space to store the maximum element and the index.
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
---
2+
id: sorted-arrray-search
3+
title: Sorted Array Search
4+
sidebar_label: Sorted-Array-Search
5+
tags:
6+
- Searching
7+
- Binary Search
8+
- Algorithms
9+
description: "This tutorial covers the solution to the Sorted Array Search problem from the GeeksforGeeks website."
10+
---
11+
## Problem Description
12+
Given an array `arr[]` sorted in ascending order of size `N` and an integer `K`. Check if `K` is present in the array or not.
13+
14+
## Examples
15+
16+
**Example 1:**
17+
18+
```
19+
Input:
20+
N = 5, K = 6
21+
arr[] = {1,2,3,4,6}
22+
Output: 1
23+
Exlpanation: Since, 6 is present in
24+
the array at index 4 (0-based indexing),
25+
output is 1.
26+
```
27+
28+
**Example 2:**
29+
30+
```
31+
Input:
32+
N = 5, K = 2
33+
arr[] = {1,3,4,5,6}
34+
Output: -1
35+
Exlpanation: Since, 2 is not present
36+
in the array, output is -1.
37+
```
38+
39+
## Your Task
40+
41+
You don't need to read input or print anything. Complete the function searchInSorted() which takes the sorted array arr[], its size N and the element K as input parameters and returns 1 if K is present in the array, else it returns -1.
42+
43+
44+
45+
Expected Time Complexity: O(Log N)
46+
47+
Expected Auxiliary Space: O(1)
48+
49+
## Constraints
50+
51+
* `1 <= arr[i] <= 10^6`
52+
53+
## Problem Explanation
54+
55+
The task is to traverse the array and find the reuired element.
56+
57+
## Code Implementation
58+
59+
### C++ Solution
60+
61+
```cpp
62+
class Solution{
63+
public:
64+
// Function to find element in sorted array
65+
// arr: input array
66+
// N: size of array
67+
// K: element to be searche
68+
int searchInSorted(int arr[], int N, int K)
69+
{
70+
int cnt = 0;
71+
for(int i=0; i<N; i++){
72+
if(arr[i]==K) {
73+
cnt = 1;
74+
break;
75+
76+
}
77+
}
78+
if(cnt==1) return 1;
79+
return -1;
80+
81+
}
82+
};
83+
```
84+
85+
```java
86+
public class Solution {
87+
public int searchInSorted(int[] arr, int N, int K) {
88+
for (int i = 0; i < N; i++) {
89+
if (arr[i] == K) {
90+
return 1;
91+
}
92+
}
93+
return -1;
94+
}
95+
}
96+
97+
```
98+
99+
```python
100+
101+
class Solution:
102+
def searchInSorted(self, arr, N, K):
103+
for i in range(N):
104+
if arr[i] == K:
105+
return 1
106+
return -1
107+
108+
```
109+
110+
```javascript
111+
class Solution {
112+
searchInSorted(arr, N, K) {
113+
for (let i = 0; i < N; i++) {
114+
if (arr[i] === K) {
115+
return 1;
116+
}
117+
}
118+
return -1;
119+
}
120+
}
121+
122+
```
123+
124+
```typescript
125+
class Solution {
126+
searchInSorted(arr: number[], N: number, K: number): number {
127+
for (let i = 0; i < N; i++) {
128+
if (arr[i] === K) {
129+
return 1;
130+
}
131+
}
132+
return -1;
133+
}
134+
}
135+
```
136+
137+
## Solution Logic:
138+
139+
1. Iterate through the sorted array from the beginning to the end.
140+
2. Check if the current element is equal to the target element (K).
141+
3. If it is, return 1 to indicate that the element is found.
142+
4. If the loop completes without finding the element, return -1 to indicate that the element is not found.
143+
144+
145+
## Time Complexity
146+
147+
* The time complexity is $O(N)$, the algorithm iterates through the array once, where N is the size of the array.Each iteration performs a constant amount of work, so the time complexity is linear.
148+
149+
150+
## Space Complexity
151+
152+
* The auxiliary space complexity is $O(1)$ due to the algorithm only uses a fixed amount of space to store the indices and the target element.

0 commit comments

Comments
 (0)