-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 26-10-2024 Question and 27-10-2024
Added 26-10-2024 Question and 27-10-2024
- Loading branch information
1 parent
5b724e1
commit 0a6e374
Showing
4 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
### Explanation | ||
|
||
The Dutch National Flag problem efficiently sorts an array with only `0`s, `1`s, and `2`s in a single pass. Here's a short explanation of how it works: | ||
|
||
1. **Setup**: We use three pointers: | ||
|
||
- `low` starts at the beginning of the array, pointing to where the next `0` should go. | ||
- `mid` also starts at the beginning, acting as the current pointer we evaluate. | ||
- `high` starts at the end, marking where the next `2` should go. | ||
|
||
2. **Loop Through Array**: | ||
|
||
- As long as `mid` is less than or equal to `high`, evaluate the element at `arr[mid]`. | ||
|
||
3. **Using Switch Cases**: | ||
|
||
- **If `arr[mid] == 0`**: Swap it with `arr[low]`, increment both `low` and `mid`. This ensures all `0`s are moved to the front. | ||
- **If `arr[mid] == 1`**: Simply move `mid` forward, as `1`s should stay in the middle. | ||
- **If `arr[mid] == 2`**: Swap `arr[mid]` with `arr[high]` and decrement `high`. Here, `mid` is not incremented because the swapped element needs checking. | ||
|
||
4. **End Condition**: | ||
- The loop continues until `mid` goes beyond `high`. | ||
- The array is then sorted with all `0`s first, followed by `1`s, and then `2`s at the end. | ||
|
||
This approach is efficient, achieving the sort in a single pass through the array. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Program for Dutch National Flag Problem using Loop and Switch Case | ||
/* | ||
Suppose you have the input array: `[1, 0, 2, 1, 0, 2, 1, 0]` | ||
The goal is to sort it in a way that all 0s come first, followed by all 1s, and then all 2s. | ||
Here's how the algorithm works step by step: | ||
1. **Initialization:** Start with three variables - `low`, `mid`, and `high`. | ||
- `low` initially points to the start of the array (index 0). | ||
- `mid` also starts at the beginning (index 0). | ||
- `high` initially points to the end of the array (index 7). | ||
2. **Iteration:** | ||
- Begin iterating through the array with the `mid` pointer. | ||
- Check the value at `arr[mid]`: | ||
- If it's 0, swap `arr[mid]` with `arr[low]` and increment both `low` and `mid`. | ||
- If it's 1, no swapping is needed; just increment `mid`. | ||
- If it's 2, swap `arr[mid]` with `arr[high]` and decrement `high`. | ||
** We do not increment the value of mid after swapping with high because we | ||
want to check that the value is in terms with rules of array ** | ||
** In other words to ensure that even after swapping array will remain sorted ** | ||
3. **Repeat Until Mid Exceeds High:** | ||
- Keep iterating until `mid` crosses or equals `high`. | ||
4. **Final Sorted Array:** | ||
After the single traversal, the array will be sorted as follows: | ||
`[0, 0, 0, 1, 1, 1, 2, 2]` | ||
Explanation: | ||
- During the traversal, the algorithm moves all 0s to the beginning, 1s to the middle, and 2s to the end, | ||
resulting in the desired sorted order. | ||
This algorithm is efficient because it sorts the array in a single pass, making it a good solution for | ||
scenarios where we need to minimize the number of iterations through the array. | ||
*/ | ||
|
||
#include<iostream> | ||
using namespace std; | ||
|
||
|
||
// For Swapping Elements | ||
void swap(int &a, int &b){ | ||
int temp = a; | ||
a = b; | ||
b = temp; | ||
} | ||
|
||
// For Printing Array | ||
void print(int arr[], int n){ | ||
for(int i = 0; i < n; i++){ | ||
cout<<arr[i]<<" "; | ||
} | ||
cout<<endl; | ||
} | ||
|
||
int main(){ | ||
|
||
// Printing Statement | ||
cout<<"Dutch National Flag Problem in Array"<<endl; | ||
|
||
// Declaring Array | ||
int arr[] = {1, 1, 0, 2, 1, 2, 0}; | ||
|
||
// Declaring Variables | ||
int n = 7; // Change here: Correct the size of the array | ||
int i = 0; | ||
|
||
cout<<"\nArray Before Sorting is:\n"; | ||
print(arr,n); | ||
|
||
// Initializing three values | ||
int mid = 0, low = 0, high = n-1; | ||
|
||
|
||
// Using While loop | ||
while(mid <= high){ | ||
|
||
// Using Switch Case | ||
switch(arr[mid]){ | ||
|
||
// For Case 0 we Swap mid and low i.e add element at the start | ||
case 0: | ||
swap(arr[mid],arr[low]); | ||
low++; | ||
mid++; | ||
break; | ||
|
||
// For Case 1 we just increment mid as 1 is going to be the middle element | ||
case 1: | ||
mid++; | ||
break; | ||
|
||
// For Case 2 we Swap mid and high i.e add element at the end | ||
case 2: | ||
swap(arr[mid],arr[high]); | ||
high--; | ||
break; | ||
} | ||
|
||
} | ||
|
||
// Printing Array | ||
cout<<"\nArray after Sorting is:\n"; | ||
print(arr,n); | ||
|
||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
### **Question: Next Greater Number in a Circular Array** | ||
|
||
**Difficulty Level:** 🟢 Beginner | ||
**Domain:** Algorithms and Data Structures (Arrays) | ||
|
||
**Objective:** | ||
Given a circular array of integers, the goal is to find the Next Greater Number for each element. The Next Greater Number for an element `x` is the first greater number that appears in a circular traversal after `x`. If no such greater number exists, output `-1` for that position. | ||
|
||
### **Conceptual Background:** | ||
|
||
In a regular array, the Next Greater Number of an element is simply the first number that is larger and appears later in the array. However, in a circular array, the end of the array "wraps around" to connect with the start, so every element effectively has both "next" and "previous" neighbors. | ||
|
||
### **Steps to Approach:** | ||
|
||
1. **Traverse the Array Circularly:** | ||
|
||
- The challenge is to check each element for the next greater number in a way that continues from the end back to the start of the array. | ||
|
||
2. **Using a Stack for Efficiency:** | ||
|
||
- To make the solution efficient, we use a stack that keeps track of indices of elements. This helps us maintain the order and quickly find the next greater element. | ||
- By iterating twice over the array length (to handle the circular aspect), we ensure each element is evaluated thoroughly. | ||
|
||
3. **Example Execution:** | ||
|
||
- **Input:** `[1, 2, 1]` | ||
- **Output Explanation:** | ||
|
||
- For the first element `1`, the next greater number in the circular array is `2`. | ||
- For the second element `2`, there is no greater number after it, so the output is `-1`. | ||
- For the last element `1`, wrapping around the array, the next greater number is `2`. | ||
|
||
- **Output:** `[2, -1, 2]` | ||
|
||
This problem helps build foundational skills in understanding array traversal techniques, stack usage, and the concept of circular arrays in algorithm design. |