Skip to content

Commit a8fff16

Browse files
authored
Merge pull request #1981 from Ishita-Jena/gfg0100
Added gfg basic solution 0001-0100
2 parents 9c32256 + 60000fa commit a8fff16

Some content is hidden

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

101 files changed

+12049
-0
lines changed
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)$
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
id: power-of-2
3+
title: Power Of 2
4+
sidebar_label: 0003 Power Of 2
5+
tags:
6+
- C
7+
- Python
8+
- Java
9+
- C++
10+
description: "This document provides solutions to the problem of checking whether a non-negative number is a power of 2."
11+
---
12+
13+
## Problem
14+
15+
Given a non-negative integer n. The task is to check if it is a power of 2.
16+
17+
### Examples:
18+
**Example 1:**
19+
```
20+
Input: n = 8
21+
Output: true
22+
Explanation: 8 is equal to 2 raised to 3 (2^3 = 8).
23+
```
24+
25+
**Example 2:**
26+
```
27+
Input: n = 98
28+
Output: false
29+
Explanation: 98 cannot be obtained by any power of 2.
30+
```
31+
32+
**Example 3:**
33+
```
34+
Input: n = 1
35+
Output: true
36+
Explanation: (2^0 = 1).
37+
```
38+
39+
- **Expected Time Complexity:** $O(log n)$
40+
- **Expected Auxiliary Space:** $O(1)$
41+
42+
### Constraints:
43+
$0 ≤ n < 10^{20}$
44+
45+
## Solution
46+
**Python**
47+
```python
48+
def isPowerofTwo(self,n : int ) -> bool:
49+
if (n == 0):
50+
return False
51+
while (n != 1):
52+
if (n % 2 != 0):
53+
return False
54+
n = n // 2
55+
return True
56+
```
57+
58+
**Java**
59+
```java
60+
public static boolean isPowerofTwo(long n) {
61+
if (n == 0)
62+
return false;
63+
while (n != 1) {
64+
if (n % 2 != 0)
65+
return false;
66+
n = n / 2;
67+
}
68+
return true;
69+
}
70+
```
71+
72+
**C**
73+
```c
74+
bool isPowerofTwo(long long n) {
75+
if (n == 0)
76+
return 0;
77+
while (n != 1) {
78+
if (n % 2 != 0)
79+
return 0;
80+
n = n / 2;
81+
}
82+
return 1;
83+
}
84+
```
85+
86+
**C++**
87+
```cpp
88+
bool isPowerofTwo(long long n) {
89+
if (n == 0)
90+
return 0;
91+
while (n != 1) {
92+
if (n % 2 != 0)
93+
return 0;
94+
n = n / 2;
95+
}
96+
return 1;
97+
}
98+
```
99+
100+
- **Time Complexity:** $O(log n)$
101+
- **Auxiliary Space:** $O(1)$

0 commit comments

Comments
 (0)