Skip to content

Added gfg basic solution 0001-0100 #1981

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 6 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
192 changes: 192 additions & 0 deletions dsa-solutions/gfg-solutions/Basic/0001.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
---
id: binary-search
title: Binary Search
sidebar_label: 0001 Binary Search
tags:
- Array
- C
- Python
- Java
- C++
description: "This document provides solutions to the problem of performing Binary Search in a sorted array."
---

## Problem

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.

### Examples
**Example 1:**
```
Input:
N = 5
arr[] = {1 2 3 4 5}
K = 4
Output: 3
Explanation: 4 appears at index 3.
```

**Example 2:**
```
Input:
N = 5
arr[] = {11 22 33 44 55}
K = 445
Output: -1
Explanation: 445 is not present.
```

### Your Task:
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.

- **Expected Time Complexity:** $O(LogN)$
- **Expected Auxiliary Space:** $O(LogN)$ if solving recursively and $O(1)$ otherwise.

### Constraints:

- $1 <= N <= 10^5$
- $1 <= arr[i] <= 10^6$
- $1 <= K <= 10^6$

## Solution:
### Iterative Approach:

**Python**
```python
def binarysearch(self, arr, n, k):
low = 0
high = n-1
while low<=high:
mid = low + (high-low)//2
if arr[mid]==k:
return mid
elif arr[mid]<k:
low = mid+1
else:
high = mid-1
return -1
```

**Java**
```java
int binarysearch(int arr[], int n, int k) {
int high = n-1;
int low = 0;
while (low<=high) {
int mid = low+(high-low)/2;
if (arr[mid]==k)
return mid;
else if (arr[mid]<k)
low = mid+1;
else
high = mid-1;
}
return -1;
}
```

**C++**
```cpp
int binarysearch(int arr[], int n, int k) {
int high = n - 1;
int low = 0;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == k)
return mid;
else if (arr[mid] < k)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
```

**C**
```c
int binarysearch(int arr[], int n, int k) {
int high = n - 1;
int low = 0;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == k)
return mid;
else if (arr[mid] < k)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
```

- **Time Complexity:** $O(log n)$
- **Space Complexity:** $O(1)$

### Recursive Approach
**Python**
```python
def binarysearch(arr, low, high, k):
if low <= high:
mid = low + (high - low) // 2
if arr[mid] == k:
return mid
elif arr[mid] < k:
return binarysearch(arr, mid + 1, high, k)
else:
return binarysearch(arr, low, mid - 1, k)
else:
return -1
```

**Java**
```java
int binarysearch(int arr[], int low, int high, int k) {
if (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == k)
return mid;
else if (arr[mid] < k)
return binarysearch(arr, mid + 1, high, k);
else
return binarysearch(arr, low, mid - 1, k);
}
return -1;
}
```

**C++**
```cpp
int binarysearch(int arr[], int low, int high, int k) {
if (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == k)
return mid;
else if (arr[mid] < k)
return binarysearch(arr, mid + 1, high, k);
else
return binarysearch(arr, low, mid - 1, k);
}
return -1;
}
```

**C**
```c
int binarysearch(int arr[], int low, int high, int k) {
if (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == k)
return mid;
else if (arr[mid] < k)
return binarysearch(arr, mid + 1, high, k);
else
return binarysearch(arr, low, mid - 1, k);
}
return -1;
}
```

- **Time Complexity:** $O(log n)$
- **Space Complexity:** $O(log n)$
101 changes: 101 additions & 0 deletions dsa-solutions/gfg-solutions/Basic/0002.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
id: peak-element
title: Peak Element
sidebar_label: 0002 Peak Element
tags:
- Array
- C
- Python
- Java
- C++
description: "This document provides solutions to the problem of finding peak element in an array."
---

## Problem

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).

Note: The output will be 1 if the index returned by your function is correct; otherwise, it will be 0.

### Examples:
**Example 1:**
```
Input: n = 3, arr[] = {1, 2, 3}
Output: 1
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.
```

**Example 2:**
```
Input: n = 7, arr[] = {1, 1, 1, 2, 1, 1, 1}
Output: 1
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.
```

## Solution
**Python**
```python
def peakElement(self,arr, n):
if (n == 1) :
return 0
if (arr[0] >= arr[1]) :
return 0
if (arr[n - 1] >= arr[n - 2]) :
return n - 1
for i in range(1, n - 1) :
if (arr[i] >= arr[i - 1] and arr[i] >= arr[i + 1]) :
return i
```

**Java**
```java
public int peakElement(int[] arr,int n) {
if (n == 1)
return 0;
if (arr[0] >= arr[1])
return 0;
if (arr[n - 1] >= arr[n - 2])
return n - 1;
for (int i = 1; i < n - 1; i++) {
if (arr[i] >= arr[i - 1] && arr[i] >= arr[i + 1])
return i;
}
return 0;
}
```

**C++**
```cpp
public:
int peakElement(int arr[], int n) {
if (n == 1)
return 0;
if (arr[0] >= arr[1])
return 0;
if (arr[n - 1] >= arr[n - 2])
return n - 1;
for (int i = 1; i < n - 1; i++) {
if (arr[i] >= arr[i - 1] && arr[i] >= arr[i + 1])
return i;
}
}
```

**C**
```c
int peakElement(int arr[], int n) {
if (n == 1)
return 0;
if (arr[0] >= arr[1])
return 0;
if (arr[n - 1] >= arr[n - 2])
return n - 1;
for (int i = 1; i < n - 1; i++) {
if (arr[i] >= arr[i - 1] && arr[i] >= arr[i + 1])
return i;
}
}
```

- **Time complexity:** $O(n)$
- **Space complexity:**: $O(1)$
101 changes: 101 additions & 0 deletions dsa-solutions/gfg-solutions/Basic/0003.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
id: power-of-2
title: Power Of 2
sidebar_label: 0003 Power Of 2
tags:
- C
- Python
- Java
- C++
description: "This document provides solutions to the problem of checking whether a non-negative number is a power of 2."
---

## Problem

Given a non-negative integer n. The task is to check if it is a power of 2.

### Examples:
**Example 1:**
```
Input: n = 8
Output: true
Explanation: 8 is equal to 2 raised to 3 (2^3 = 8).
```

**Example 2:**
```
Input: n = 98
Output: false
Explanation: 98 cannot be obtained by any power of 2.
```

**Example 3:**
```
Input: n = 1
Output: true
Explanation: (2^0 = 1).
```

- **Expected Time Complexity:** $O(log n)$
- **Expected Auxiliary Space:** $O(1)$

### Constraints:
$0 ≤ n < 10^{20}$

## Solution
**Python**
```python
def isPowerofTwo(self,n : int ) -> bool:
if (n == 0):
return False
while (n != 1):
if (n % 2 != 0):
return False
n = n // 2
return True
```

**Java**
```java
public static boolean isPowerofTwo(long n) {
if (n == 0)
return false;
while (n != 1) {
if (n % 2 != 0)
return false;
n = n / 2;
}
return true;
}
```

**C**
```c
bool isPowerofTwo(long long n) {
if (n == 0)
return 0;
while (n != 1) {
if (n % 2 != 0)
return 0;
n = n / 2;
}
return 1;
}
```

**C++**
```cpp
bool isPowerofTwo(long long n) {
if (n == 0)
return 0;
while (n != 1) {
if (n % 2 != 0)
return 0;
n = n / 2;
}
return 1;
}
```

- **Time Complexity:** $O(log n)$
- **Auxiliary Space:** $O(1)$
Loading
Loading