Skip to content

Commit eb37116

Browse files
committed
Updated
1 parent 20e7b55 commit eb37116

13 files changed

+471
-0
lines changed

217_Contains_Duplicate_LeetCode.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
bool containsDuplicate(vector<int> &nums)
5+
{
6+
sort(nums.begin(), nums.end());
7+
8+
for (int i = 1; i < nums.size() ; i++)
9+
{
10+
if (nums[i - 1] == nums[i])
11+
{
12+
return true;
13+
}
14+
else
15+
{
16+
return false;
17+
}
18+
}
19+
}
20+
21+
int main()
22+
{
23+
vector<int> nums = {5,2,1,6,6,7,0};
24+
cout << containsDuplicate(nums);
25+
return 0;
26+
}

Merge_Sort_DSA.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include<iostream>
2+
#include<bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
void merge(vector<int> &array , int start , int end , int mid)
7+
{
8+
int nn = mid - start + 1 ;
9+
int mm = end - mid ;
10+
11+
vector<int> temp_nn , temp_mm;
12+
13+
for (int i = 0; i < nn; i++)
14+
{
15+
temp_nn[i] = array[start + i];
16+
}
17+
for (int j = 0; j < mm; j++)
18+
{
19+
temp_mm[j] = array[mid + 1 + j];
20+
}
21+
22+
int x , y , z ;
23+
x = 0 ;
24+
y = 0 ;
25+
z = start ;
26+
27+
while (x < nn && y < mm) {
28+
if (temp_nn[x] <= temp_mm[y]) {
29+
array[z] = temp_nn[x];
30+
x++;
31+
} else {
32+
array[z] = temp_mm[y];
33+
y++;
34+
}
35+
z++;
36+
}
37+
38+
while (x < nn) {
39+
array[z] = temp_nn[x];
40+
x++;
41+
z++;
42+
}
43+
44+
while (y < mm) {
45+
array[z] = temp_mm[y];
46+
y++;
47+
z++;
48+
}
49+
}
50+
void mergeSort(vector<int> &array , int start , int end )
51+
{
52+
if(start >= end)
53+
{
54+
return ;
55+
}
56+
int mid = ( start + end ) / 2;
57+
mergeSort(array , start , mid);
58+
mergeSort(array , mid+1 , end);
59+
return merge(array, start , end , mid);
60+
}
61+
int main()
62+
{
63+
vector<int> array{10,5,2,0,7,6,4};
64+
int start = 0 ;
65+
int end = array.size() - 1;
66+
mergeSort(array , start , end) ;
67+
for(int x : array)
68+
{
69+
cout<< x << ",";
70+
}
71+
cout<<endl;
72+
return 0 ;
73+
}

aa.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
5
2+
1
3+
O
4+
3
5+
XFO
6+
5
7+
FFOFF
8+
10
9+
FXXFXFOOXF
10+
13
11+
XFOFXFOFXFOFX

fb_1A.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <string>
4+
using namespace std;
5+
6+
int main()
7+
{
8+
ifstream infile("aa.txt");
9+
if (infile.is_open())
10+
{
11+
12+
int num_of_test_cases;
13+
infile>>num_of_test_cases;
14+
int N;
15+
string str;
16+
int counter;
17+
for (int i = 0; i < num_of_test_cases; i++)
18+
{
19+
infile >> N;
20+
infile >> str;
21+
counter = 0;
22+
for (int j = 0; j < N; j++)
23+
{
24+
if ((str[j] == 'X' && str[j + 1] != 'X') || (str[j] == 'O' && str[j + 1] != 'O'))
25+
{
26+
counter++;
27+
}
28+
}
29+
30+
cout << "Case #" << i + 1 << ":" << counter - 1 << endl;
31+
}
32+
}
33+
return 0;
34+
}

josephus_problem.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include<iostream>
2+
using namespace std ;
3+
4+
int josp(int num , int steps)
5+
{
6+
if ( num == 1)
7+
{
8+
return 0;
9+
}
10+
11+
return (josp(num-1,steps) + steps)%num ;
12+
}
13+
14+
int main()
15+
{
16+
int num = 5 ;
17+
int steps = 2 ;
18+
cout << josp(num,steps) ;
19+
return 0 ;
20+
}

linked_list_new.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
struct Node {
5+
int data ;
6+
struct Node *next ;
7+
};
8+
9+
void traverse_linked_list(struct Node * head)
10+
{
11+
while(head != NULL)
12+
{
13+
cout << head->data <<"||" ;
14+
head = head->next;
15+
}
16+
}
17+
18+
void add_node_to_end(struct Node* head , int data )
19+
{ struct Node * node_to_be_inserted = new Node ;
20+
node_to_be_inserted->data =data ;
21+
node_to_be_inserted->next = NULL ;
22+
while (head ->next != NULL)
23+
{
24+
head = head->next;
25+
}
26+
head->next = node_to_be_inserted ;
27+
}
28+
29+
struct Node* add_node_to_beginning(struct Node* head , int data )
30+
{ struct Node * node_to_be_inserted = new Node ;
31+
node_to_be_inserted->data =data ;
32+
node_to_be_inserted->next = head ;
33+
return node_to_be_inserted ;
34+
35+
}
36+
37+
int main()
38+
{
39+
struct Node * head ;
40+
struct Node * first ;
41+
struct Node * second ;
42+
43+
head = new Node ;
44+
first = new Node ;
45+
second = new Node ;
46+
47+
head->data = 67 ;
48+
head->next = first ;
49+
first->data = 98 ;
50+
first->next = second ;
51+
second->data =78 ;
52+
second->next = NULL ;
53+
traverse_linked_list(head);
54+
55+
cout<<endl;
56+
head = add_node_to_beginning()
57+
traverse_linked_list(head);
58+
return 0 ;
59+
}

min_swap.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int minSwaps(int arr[], int n)
5+
{
6+
pair<int, int> ap[n];
7+
for (int i = 0; i < n; i++)
8+
{
9+
ap[i].first = arr[i];
10+
ap[i].second = i;
11+
}
12+
13+
sort(ap, ap + n);
14+
vector<bool> visited(n, false);
15+
16+
int result = 0;
17+
18+
for (int i = 0; i < n; i++)
19+
{
20+
21+
if (visited[i] || ap[i].second == i)
22+
continue;
23+
24+
int cycle_size = 0;
25+
int node = i;
26+
while (!visited[node])
27+
{
28+
visited[node] = true;
29+
node = ap[node].second;
30+
cycle_size++;
31+
}
32+
33+
if (cycle_size > 0)
34+
{
35+
result += (cycle_size - 1);
36+
}
37+
}
38+
39+
return result;
40+
}
41+
42+
43+
int main()
44+
{
45+
int arr[] = {4, 5, 1, 3, 2};
46+
int n = (sizeof(arr) / sizeof(int));
47+
cout << minSwaps(arr, n);
48+
return 0;
49+
}

num_of_matrix_path.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include<iostream>
2+
3+
using namespace std ;
4+
5+
6+
int num_path(int rows , int columns )
7+
{
8+
if(rows ==1 || columns == 1)
9+
{
10+
return 1 ;
11+
}
12+
return num_path(rows-1 , columns) + num_path(rows , columns -1 ) ;
13+
}
14+
int main()
15+
{
16+
int rows = 4 ;
17+
int columns = 5 ;
18+
cout<<num_path(rows, columns);
19+
return 0 ;
20+
}

permutaions_of_string.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <bits/stdc++.h>
2+
#include <string>
3+
using namespace std;
4+
5+
6+
void permutation_of_string(string st , int left ,int right)
7+
{
8+
if( left == right )
9+
{
10+
cout<<st<<"||" ;
11+
return;
12+
}
13+
for (int i = left; i <= right; i++)
14+
{
15+
swap(st[left] , st[i]);
16+
permutation_of_string(st , left+1,right);
17+
swap(st[left] , st[i]);
18+
}
19+
20+
}
21+
int main()
22+
{
23+
string st = "Riyan";
24+
int num = st.length();
25+
permutation_of_string(st , 0 , num-1);
26+
return 0;
27+
}

power_set.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include<iostream>
2+
#include<string>
3+
using namespace std;
4+
5+
void power_set (string st , int index , string current)
6+
{
7+
if(index == st.length())
8+
{
9+
cout<<current<<"||";
10+
return ;
11+
}
12+
power_set(st, index + 1 , current + st[index]);
13+
power_set(st, index + 1 , current );
14+
}
15+
16+
int main()
17+
{
18+
string st = "Monday";
19+
int index = 0 ;
20+
string current = "";
21+
power_set(st, index , current);
22+
return 0 ;
23+
}

rain_water_trap.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int trappedWater(vector<int> heights) {
5+
6+
int n = heights.size();
7+
if(n<=2){
8+
return 0;
9+
}
10+
11+
vector<int> left(n,0), right(n,0);
12+
left[0] = heights[0];
13+
right[n-1] = heights[n-1];
14+
15+
for(int i=1;i<n;i++){
16+
left[i] = max(left[i-1],heights[i]);
17+
right[n-i-1] = max(right[n-i],heights[n-i-1]);
18+
}
19+
20+
int water = 0;
21+
for(int i=0;i<n;i++){
22+
water += min(left[i],right[i]) - heights[i];
23+
}
24+
25+
return water;
26+
}
27+
28+
int main() {
29+
vector<int> water = {0,1,0,2,1,0,1,3,2,1,2,1};
30+
cout<<trappedWater(water)<<endl;
31+
return 0;
32+
}

0 commit comments

Comments
 (0)