Skip to content

Commit dccedc3

Browse files
committed
Update
1 parent 55097f4 commit dccedc3

19 files changed

Lines changed: 889 additions & 1 deletion

Data_Structure/queue_fifo.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Task : Queue
3+
Author : Lalit Chaudhary
4+
Language : C++
5+
Explanation : Implement a queue using a linked list and perform 5 enqueue operations, one dequeue operation, anmd display the elements
6+
*/
7+
8+
#include <iostream>
9+
using namespace std;
10+
struct node {
11+
int data;
12+
wwxstruct node *next;
13+
};
14+
struct node* front = NULL;
15+
struct node* rear = NULL;
16+
struct node* temp;
17+
void Insert() {
18+
int val;
19+
cout<<"Insert the element in queue : "<<endl;
20+
cin>>val;
21+
if (rear == NULL) {
22+
rear = (struct node *)malloc(sizeof(struct node));
23+
rear->next = NULL;
24+
rear->data = val;
25+
front = rear;
26+
} else {
27+
temp=(struct node *)malloc(sizeof(struct node));
28+
rear->next = temp;
29+
temp->data = val;
30+
temp->next = NULL;
31+
rear = temp;
32+
}
33+
}
34+
void Delete() {
35+
temp = front;
36+
if (front == NULL) {
37+
cout<<"Underflow"<<endl;
38+
return;
39+
}
40+
else
41+
if (temp->next != NULL) {
42+
temp = temp->next;
43+
cout<<"Element deleted from queue is : "<<front->data<<endl;
44+
free(front);
45+
front = temp;
46+
} else {
47+
cout<<"Element deleted from queue is : "<<front->data<<endl;
48+
free(front);
49+
front = NULL;
50+
rear = NULL;
51+
}
52+
}
53+
void Display() {
54+
temp = front;
55+
if ((front == NULL) && (rear == NULL)) {
56+
cout<<"Queue is empty"<<endl;
57+
return;
58+
}
59+
cout<<"Queue elements are: ";
60+
while (temp != NULL) {
61+
cout<<temp->data<<" ";
62+
temp = temp->next;
63+
}
64+
cout<<endl;
65+
}
66+
int main() {
67+
int ch;
68+
cout<<"1) Insert element to queue"<<endl;
69+
cout<<"2) Delete element from queue"<<endl;
70+
cout<<"3) Display all the elements of queue"<<endl;
71+
cout<<"4) Exit"<<endl;
72+
do {
73+
cout<<"Enter your choice : "<<endl;
74+
cin>>ch;
75+
switch (ch) {
76+
case 1: Insert();
77+
break;
78+
case 2: Delete();
79+
break;
80+
case 3: Display();
81+
break;
82+
case 4: cout<<"Exit"<<endl;
83+
break;
84+
default: cout<<"Invalid choice"<<endl;
85+
}
86+
} while(ch!=4);
87+
return 0;
88+
}

Divide_and_Conquer/Mod_of_Power.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Author : Phumipat C. [MAGCARI]
44
School : RYW
55
Language: C++
6+
Explanation Link : https://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/
67
Created : 03 May 2021 [15:15]
78
*/
89
#include<bits/stdc++.h>
@@ -17,4 +18,4 @@ LL mod_of_power(LL a,LL b,LL c = MOD){ // pow(a,b)%c
1718
LL now = mod_of_power(a,b/2,c);
1819
if(b&1) return (((now*now)%c)*(a%c))%c;
1920
else return (now*now)%c;
20-
}
21+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Task : Mod_of_Power
3+
Author : Phumipat C. [MAGCARI]
4+
School : RYW
5+
Language: C++
6+
Explanation Link : https://www.geeksforgeeks.org/multiply-two-polynomials-2/
7+
Created : 03 May 2021 [15:15]
8+
*/
9+
10+
11+
#include<bits/stdc++.h>
12+
#define all(x) begin(x),end(x)
13+
#define sz(x) (int)(x).size()
14+
#define MOD (LL )(1e9+7)
15+
using namespace std;
16+
using LL = long long;
17+
using PII = pair<int ,int >;
18+
using PLL = pair<long long ,long long >;
19+
const int dir4[2][4] = {{1,-1,0,0},{0,0,1,-1}};
20+
const int dir8[2][8] = {{-1,-1,-1,0,1,1,1,0},{-1,0,1,1,-1,0,1,-1}};
21+
22+
23+
24+
int *multiply(int A[], int B[], int m, int n)
25+
{
26+
int *prod = new int[m+n-1];
27+
28+
for (int i = 0; i<m+n-1; i++)
29+
prod[i] = 0;
30+
31+
for (int i=0; i<m; i++)
32+
{
33+
for (int j=0; j<n; j++)
34+
prod[i+j] += A[i]*B[j];
35+
}
36+
37+
return prod;
38+
}
39+
40+
void printPoly(int poly[], int n)
41+
{
42+
for (int i=0; i<n; i++)
43+
{
44+
cout << poly[i];
45+
if (i != 0)
46+
cout << "x^" << i ;
47+
if (i != n-1)
48+
cout << " + ";
49+
}
50+
}

Greedy/Job_sequencing.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Task : Job_sequencing
3+
Author : PunnyOz2
4+
Language : C++
5+
Explanation :
6+
Created : 01 October 2022 [15:39]
7+
*/
8+
#include<bits/stdc++.h>
9+
using namespace std;
10+
11+
// A structure to represent a job
12+
struct Job {
13+
14+
char id; // Job Id
15+
int dead; // Deadline of job
16+
int profit; // Profit earned if job is completed before
17+
// deadline
18+
bool operator<(const Job &o) const{
19+
return profit > o.profit;
20+
}
21+
};
22+
23+
vector<Job> jobSequencingResult(vector<Job> arr, int n) {
24+
sort(arr.begin(), arr.end(), [](Job a, Job b) { return a.dead < b.dead; });
25+
priority_queue<Job> pq;
26+
vector<Job> ans;
27+
for (int i = n - 1; i >= 0; i--) {
28+
int slot_available;
29+
if (pq.empty()) {
30+
slot_available = arr[i].dead;
31+
} else {
32+
slot_available = arr[i].dead - arr[i-1].dead;
33+
}
34+
pq.push(arr[i]);
35+
while(!slot_available && !pq.empty()) {
36+
ans.push_back(pq.top());
37+
pq.pop();
38+
slot_available--;
39+
}
40+
}
41+
sort(ans.begin(), ans.end(), [&](Job a, Job b) { return a.dead < b.dead; });
42+
return ans;
43+
}
44+
45+
int jobSequencingProfitSum(vector<Job> arr, int n) {
46+
sort(arr.begin(), arr.end(), [](Job a, Job b) { return a.dead < b.dead; });
47+
priority_queue<Job> pq;
48+
int ans = 0;
49+
for (int i = n - 1; i >= 0; i--) {
50+
int slot_available;
51+
if (pq.empty()) {
52+
slot_available = arr[i].dead;
53+
} else {
54+
slot_available = arr[i].dead - arr[i-1].dead;
55+
}
56+
pq.push(arr[i]);
57+
while(!slot_available && !pq.empty()) {
58+
ans += pq.top().profit;
59+
pq.pop();
60+
slot_available--;
61+
}
62+
}
63+
return ans;
64+
}

Greedy/Minimum_Coin_Change.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Task : Minimum Coin Change
3+
Author : Naravit N. [ADNCRICH]
4+
School : Chulalongkorn University
5+
Language : C++
6+
Explanation : https://www.geeksforgeeks.org/quick-sort/
7+
Created : 01 Oct 2022 [15:37]
8+
*/
9+
#include <bits/stdc++.h>
10+
#define all(x) begin(x), end(x)
11+
#define sz(x) (int)(x).size()
12+
#define MOD (LL)(1e9 + 7)
13+
using namespace std;
14+
using LL = long long;
15+
using PII = pair<int, int>;
16+
using PLL = pair<long long, long long>;
17+
const int dir4[2][4] = {{1, -1, 0, 0}, {0, 0, 1, -1}};
18+
const int dir8[2][8] = {{-1, -1, -1, 0, 1, 1, 1, 0}, {-1, 0, 1, 1, -1, 0, 1, -1}};
19+
const int N = 1000000, M = 10000;
20+
21+
int dp[N + 20], coins[M + 20];
22+
23+
void minimum_coin_change(int n) {
24+
for (int i = 0; i < n; i++) cin >> coins[i];
25+
memset(dp, -1, sizeof(dp));
26+
dp[0] = 0;
27+
for (int i = 1; i <= N; i++) {
28+
for (int j = 0; j < n; j++) {
29+
if (i - coins[j] >= 0 && dp[i - coins[j]] != -1) {
30+
if (dp[i] == -1)
31+
dp[i] = dp[i - coins[j]] + 1;
32+
else
33+
dp[i] = min(dp[i], dp[i - coins[j]] + 1);
34+
}
35+
}
36+
}
37+
}
38+
39+
int main() {
40+
int n, q, i;
41+
cin >> n >> q;
42+
minimum_coin_change(n);
43+
while (q--) {
44+
int x;
45+
cin >> x;
46+
cout << dp[x] << "\n";
47+
}
48+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Task : 2D Range Minimum Query
3+
Author : Borworntat D. [Hydrolzed~]
4+
School : RYW
5+
Language : C++
6+
Explanation : https://codeforces.com/blog/entry/45485
7+
Created : 01 October 2022 [12:31]
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
const int MxN = 1010;
14+
const int LgN = 16;
15+
int sparse_table[LgN][LgN][MxN][MxN], lg[MxN];
16+
17+
void init(std::vector<std::vector<int>> arr){
18+
int n = arr.size();
19+
for(int state=2; state<MxN; ++state){
20+
lg[state] = lg[state >> 1] + 1;
21+
}
22+
memset(sparse_table, 0x3f, sizeof sparse_table);
23+
// base case
24+
for(int i=0; i<n; ++i){
25+
for(int j=0; j<n; ++j){
26+
sparse_table[0][0][i][j] = arr[i][j];
27+
}
28+
}
29+
// precompute
30+
for(int state_i=0; state_i<LgN; ++state_i){
31+
for(int state_j=0; state_j<LgN; ++state_j){
32+
if(state_i + state_j == 0){
33+
continue;
34+
}
35+
for(int i=0; i+(1 << state_i)<=n; ++i){
36+
for(int j=0; j+(1 << state_j)<=n; ++j){
37+
if(state_i == 0){
38+
sparse_table[state_i][state_j][i][j] = min(sparse_table[state_i][state_j - 1][i][j], sparse_table[state_i][state_j - 1][i][j + (1 << (state_j - 1))]);
39+
}
40+
else{
41+
sparse_table[state_i][state_j][i][j] = min(sparse_table[state_i - 1][state_j][i][j], sparse_table[state_i - 1][state_j][i + (1 << (state_i - 1))][j]);
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
48+
49+
int query(int a, int b, int c, int d){
50+
swap(c, b);
51+
c++, d++;
52+
int state_i = lg[c - a], state_j = lg[d - b];
53+
int A = sparse_table[state_i][state_j][a][b];
54+
int B = sparse_table[state_i][state_j][c - (1 << state_i)][b];
55+
int C = sparse_table[state_i][state_j][a][d - (1 << state_j)];
56+
int D = sparse_table[state_i][state_j][c - (1 << state_i)][d - (1 << state_j)];
57+
return min({A, B, C, D});
58+
}

Searching/Exponential_Search.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Task : Exponential_Search.cpp
3+
Author : sumit-awasthi
4+
Language : C++
5+
Explanation :
6+
*/
7+
#include <bits/stdc++.h>
8+
#define all(x) begin(x), end(x)
9+
#define sz(x) (int)(x).size()
10+
#define MOD (LL)(1e9 + 7)
11+
12+
#include<iostream>
13+
14+
using namespace std;
15+
const int dir4[2][4] = {{1, -1, 0, 0}, {0, 0, 1, -1}};
16+
const int dir8[2][8] = {{-1, -1, -1, 0, 1, 1, 1, 0}, {-1, 0, 1, 1, -1, 0, 1, -1}};
17+
18+
int binarySearch(int array[], int start, int end, int key) {
19+
if(start <= end) {
20+
int mid = (start + (end - start) /2);
21+
if(array[mid] == key)
22+
return mid;
23+
if(array[mid] > key)
24+
return binarySearch(array, start, mid-1, key);
25+
return binarySearch(array, mid+1, end, key);
26+
}
27+
return -1;
28+
}
29+
30+
int exponentialSearch(int array[], int start, int end, int key){
31+
if((end - start) <= 0)
32+
return -1;
33+
int i = 1; // as 2^0 = 1
34+
while(i < (end - start)){
35+
if(array[i] < key)
36+
i *= 2;
37+
else
38+
break;
39+
}
40+
return binarySearch(array, i/2, i, key);
41+
}
42+
43+
int main() {
44+
int n, searchKey, loc;
45+
cout << "Enter number of items: ";
46+
cin >> n;
47+
int arr[n];
48+
cout << "Enter items: " << endl;
49+
for(int i = 0; i< n; i++) {
50+
cin >> arr[i];
51+
}
52+
cout << "Enter search key to search in the list: ";
53+
cin >> searchKey;
54+
if((loc = exponentialSearch(arr, 0, n, searchKey)) >= 0)
55+
cout << "Item found at Index= " << loc << endl;
56+
else
57+
cout << "Item is not found in the list." << endl;
58+
return 0;
59+
}

0 commit comments

Comments
 (0)