-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #137 from aaryansood2512/master
ADDED C++ = DSA CODES
- Loading branch information
Showing
53 changed files
with
3,420 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,90 @@ | ||
/* | ||
Buy the ticket | ||
You want to buy a ticket for a well-known concert which is happening in your city. But the number of tickets available is limited. Hence the sponsors of | ||
the concert decided to sell tickets to customers based on some priority. | ||
A queue is maintained for buying the tickets and every person is attached with a priority (an integer, 1 being the lowest priority). | ||
The tickets are sold in the following manner - | ||
1. The first person (pi) in the queue requests for the ticket. | ||
2. If there is another person present in the queue who has higher priority than pi, then ask pi to move at end of the queue without giving him the ticket. | ||
3. Otherwise, give him the ticket (and don't make him stand in queue again). | ||
Giving a ticket to a person takes exactly 1 minute and it takes no time for removing and adding a person to the queue. And you can assume that no new person joins the queue. | ||
Given a list of priorities of N persons standing in the queue and the index of your priority (indexing starts from 0). Find and return the time it will take until you get the ticket. | ||
Input Format: | ||
The first line of input contains an integer, that denotes the value of total number of people standing in queue or the size of the array of priorities. | ||
Let us denote it with the symbol N. | ||
The following line contains N space separated integers, that denote the value of the elements of the array of priorities. | ||
The following contains an integer, that denotes the value of index of your priority. Let us denote it with symbol k. | ||
Output Format : | ||
The first and only line of output contains the time required for you to get the ticket. | ||
Constraints: | ||
Time Limit: 1 sec | ||
Sample Input 1 : | ||
3 | ||
3 9 4 | ||
2 | ||
Sample Output 1 : | ||
2 | ||
Sample Output 1 Explanation : | ||
Person with priority 3 comes out. But there is a person with higher priority than him. So he goes and then stands in the queue at the end. | ||
->> Queue's status : {9, 4, 3}. Time : 0 secs. | ||
Next, the person with priority 9 comes out. And there is no person with higher priority than him. | ||
->> So he'll get the ticket. Queue's status : {4, 3}. Time : 1 secs. | ||
Next, the person with priority 4 comes out (which is you). And there is no person with higher priority than you. | ||
->> So you'll get the ticket. Time : 2 secs. | ||
Sample Input 2 : | ||
5 | ||
2 3 2 2 4 | ||
3 | ||
Sample Output 2 : | ||
4 | ||
*/ | ||
|
||
#include <queue> | ||
// K is the person of interest | ||
int buyTicket(int *arr, int n, int k){ | ||
|
||
queue<int> q; | ||
|
||
for(int i = 0; i < n ; i++) { | ||
q.push(i); // -> Indices of Persons | ||
} | ||
|
||
priority_queue<int> pq; | ||
|
||
for(int i = 0 ; i < n ; i++){ | ||
pq.push(arr[i]); // -> Priority of persons in maxHeap | ||
} | ||
|
||
int timeTaken = 0; | ||
|
||
// This loop runs until the person of interest is in the front and his priority is highest | ||
while(q.front() != k or pq.top() != arr[q.front()]) { | ||
// if a person with less priority is standing in front send him to the back of queue | ||
if(pq.top() > arr[q.front()]){ | ||
q.push(q.front()); | ||
q.pop(); | ||
} else if(pq.top() == arr[q.front()]) { | ||
pq.pop(); | ||
q.pop(); | ||
timeTaken++; | ||
} | ||
|
||
} | ||
|
||
return timeTaken + 1; // Plus 1 to account for person of interest getting the ticket | ||
} | ||
|
||
// Time Complexity : O(nlogn) | ||
// Auxillary Space : O(n) |
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,55 @@ | ||
/* | ||
Check Max-Heap | ||
Given an array of integers, check whether it represents max-heap or not. Return true if the given array represents max-heap, else return false. | ||
Input Format: | ||
The first line of input contains an integer, that denotes the value of the size of the array. Let us denote it with the symbol N. | ||
The following line contains N space separated integers, that denote the value of the elements of the array. | ||
Output Format : | ||
The first and only line of output contains true if it represents max-heap and false if it is not a max-heap. | ||
Constraints: | ||
1 <= N <= 10^5 | ||
1 <= Ai <= 10^5 | ||
Time Limit: 1 sec | ||
Sample Input 1: | ||
8 | ||
42 20 18 6 14 11 9 4 | ||
Sample Output 1: | ||
true | ||
*/ | ||
|
||
bool isMaxHeap(int arr[], int n, int index = 0) { | ||
// Write your code here | ||
// base case | ||
if(index > (n - 2 )/ 2) { | ||
return true; | ||
} | ||
|
||
int left = 2 * index + 1; | ||
int right = 2 * index + 2; | ||
|
||
bool isleftHeap = isMaxHeap(arr, n, left); | ||
bool isrightHeap = isMaxHeap(arr,n, right); | ||
|
||
if(right < n){ | ||
if(arr[left] < arr[index] and arr[right] < arr[index] and isleftHeap and isrightHeap) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} else { | ||
if(arr[left] < arr[index] and isleftHeap) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
// Time Complexity : O(n) -> In a way similar to preorder travelsal of binary tree | ||
// Auxillary Space : O(logn) i.e O(h) |
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,67 @@ | ||
/* | ||
In-place heap sort | ||
Given an integer array of size N. Sort this array (in decreasing order) using heap sort. | ||
Note: Space complexity should be O(1). | ||
Input Format: | ||
The first line of input contains an integer, that denotes the value of the size of the array or N. | ||
The following line contains N space separated integers, that denote the value of the elements of the array. | ||
Output Format : | ||
The first and only line of output contains array elements after sorting. The elements of the array in the output are separated by single space. | ||
Constraints : | ||
1 <= n <= 10^6 | ||
Time Limit: 1 sec | ||
Sample Input 1: | ||
6 | ||
2 6 8 5 4 3 | ||
Sample Output 1: | ||
8 6 5 4 3 2 | ||
*/ | ||
|
||
void swap(int *a, int *b) { | ||
int temp = *a; | ||
*a = *b; | ||
*b = temp; | ||
} | ||
void minHeapify(int *arr, int n, int index) { | ||
int curr = index; | ||
int leftChildIndex = 2 * index + 1; | ||
int rightChildIndex = 2 * index + 2; | ||
|
||
if(leftChildIndex < n and arr[leftChildIndex] < arr[curr]) { | ||
curr = leftChildIndex; | ||
} | ||
|
||
if(rightChildIndex < n and arr[rightChildIndex] < arr[curr]) { | ||
curr = rightChildIndex; | ||
} | ||
|
||
if(curr != index) { | ||
swap(&arr[curr], &arr[index]); | ||
minHeapify(arr, n, curr); | ||
} | ||
} | ||
void heapSort(int arr[], int n) { | ||
// Write your code | ||
int count = n - 1; | ||
|
||
while(count >= 0){ | ||
minHeapify(arr, n, count); | ||
count--; | ||
} | ||
|
||
count = 0; | ||
while(count < n) { | ||
swap(&arr[0], &arr[n - count - 1]); | ||
minHeapify(arr, n - count - 1, 0); | ||
count++; | ||
} | ||
} | ||
// Time Complexity : O(nlogn) | ||
// Auxillary Space : O(1) |
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,68 @@ | ||
/* | ||
K largest elements | ||
You are given with an integer k and an array of integers that contain numbers in random order. Write a program to find k largest numbers from given array. | ||
You need to save them in an array and return it. | ||
Time complexity should be O(nlogk) and space complexity should be not more than O(k). | ||
Order of elements in the output is not important. | ||
Input Format : | ||
Line 1 : Size of array (n) | ||
Line 2 : Array elements (separated by space) | ||
Line 3 : Integer k | ||
Output Format : | ||
k largest elements | ||
Sample Input : | ||
13 | ||
2 12 9 16 10 5 3 20 25 11 1 8 6 | ||
4 | ||
Sample Output : | ||
12 | ||
16 | ||
20 | ||
25 | ||
*/ | ||
|
||
#include<queue> | ||
#include<vector> | ||
vector<int> kLargest(int input[], int n, int k){ | ||
/* Don't write main(). | ||
* Don't read input, it is passed as function argument. | ||
* Return output and don't print it. | ||
* Taking input and printing output is handled automatically. | ||
*/ | ||
|
||
priority_queue<int, vector<int>, greater<int>> pq; | ||
|
||
for(int i = 0; i < k; i++) { | ||
pq.push(input[i]); | ||
} | ||
// This forms a k sized set in min heap | ||
|
||
for(int i = k; i < n; i++) { | ||
if(input[i] > pq.top()) { | ||
pq.pop(); | ||
pq.push(input[i]); | ||
} else { | ||
continue; | ||
} | ||
} | ||
|
||
// Total Time taken O(nlogk) | ||
|
||
vector<int> answer; | ||
for(; !pq.empty(); ) { | ||
answer.push_back(pq.top()); | ||
pq.pop(); | ||
} | ||
|
||
// This will yield a k sized set of smallest number sorted in ascending order | ||
// This takes O(k) time and O(k) space | ||
return answer; | ||
|
||
} | ||
|
||
// Time Complexity : O(nlogk) | ||
// Auxillary Space : O(k) |
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,62 @@ | ||
/* | ||
K smallest Elements | ||
You are given with an integer k and an array of integers that contain numbers in random order. Write a program to find k smallest numbers from given array. | ||
You need to save them in an array and return it. | ||
Time complexity should be O(n * logk) and space complexity should not be more than O(k). | ||
Note: Order of elements in the output is not important. | ||
Input Format : | ||
The first line of input contains an integer, that denotes the value of the size of the array. Let us denote it with the symbol N. | ||
The following line contains N space separated integers, that denote the value of the elements of the array. | ||
The following line contains an integer, that denotes the value of k. | ||
Output Format : | ||
The first and only line of output print k smallest elements. The elements in the output are separated by a single space. | ||
Constraints: | ||
Time Limit: 1 sec | ||
Sample Input 1 : | ||
13 | ||
2 12 9 16 10 5 3 20 25 11 1 8 6 | ||
4 | ||
Sample Output 1 : | ||
1 2 3 5 | ||
*/ | ||
|
||
#include<queue> | ||
vector<int> kSmallest(int arr[], int n, int k) { | ||
// Write your code here | ||
priority_queue<int> pq; | ||
|
||
for(int i = 0; i < k; i++) { | ||
pq.push(arr[i]); | ||
} | ||
|
||
int index = 0; | ||
for(int i = k; i < n; i++) { | ||
if(arr[i] < pq.top()) { | ||
pq.pop(); | ||
pq.push(arr[i]); | ||
} else { | ||
continue; | ||
} | ||
} | ||
|
||
// Total Time taken O(nlogk) | ||
|
||
vector<int> answer; | ||
for(; !pq.empty(); ) { | ||
answer.push_back(pq.top()); | ||
pq.pop(); | ||
} | ||
// This will yield a k sized set of smallest number sorted in descending order | ||
// This takes O(k) time and O(k) space | ||
|
||
return answer; | ||
} | ||
|
||
// Time Complexity : O((nlogk) | ||
// Auxillary Space : O(k) |
Oops, something went wrong.