Skip to content

Commit c9a363e

Browse files
authored
Add files via upload
1 parent b98c2d2 commit c9a363e

17 files changed

+686
-0
lines changed
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// input - input array
2+
// size - length of input array
3+
// element - value to be searched
4+
int findelement(int input[], int x, int left, int right)
5+
{
6+
if(left > right) return - 1;
7+
8+
int mid = (left + right) / 2;
9+
10+
if(input[mid] == x) return mid;
11+
12+
if(input[mid] > x)
13+
{
14+
return findelement(input, x, left, mid - 1);
15+
}
16+
17+
else if(input[mid] < x)
18+
{
19+
return findelement(input, x, mid + 1, right);
20+
}
21+
}
22+
int binarySearch(int input[], int size, int element) {
23+
// Write your code here
24+
return findelement(input, element, 0, size - 1);
25+
26+
}

Diff for: Lecture 4 - Recursion 2/Check AB.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
bool checkInput(char input[], int size, bool check)
2+
{
3+
if(size == 0) return true;
4+
5+
if(input[0] == 'a')
6+
{
7+
checkInput(input + 1, size - 1, true);
8+
}
9+
else if(check == true && input[0]== 'b' && input[1] == 'b')
10+
{
11+
checkInput(input + 2, size - 2, false);
12+
}
13+
else return false;
14+
}
15+
bool checkAB(char input[]) {
16+
// Write your code here
17+
int size = 0;
18+
for(int i = 0; input[i] != '\0'; i++) size++;
19+
if(input[0] != 'a') return false;
20+
else checkInput(input + 1, size -1, true);
21+
}
22+
23+
24+

Diff for: Lecture 4 - Recursion 2/Get All Unique Subsets.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
map<vector<int>, int> mp;
2+
3+
int getUniqueSubsets(vector<int>& arr, int size, vector<vector<int>>& output)
4+
{
5+
if(size == 0)
6+
{
7+
vector<int> temp;
8+
output.push_back(temp);
9+
return 1;
10+
}
11+
12+
int set1 = getUniqueSubsets(arr, size - 1, output);
13+
14+
//copying
15+
for(int i = 0; i < set1; i++)
16+
{
17+
vector<int> temp;
18+
for(int j = 0; j < output[i].size(); j++)
19+
{
20+
temp.push_back(output[i][j]);
21+
}
22+
temp.push_back(arr[size - 1]);
23+
if(mp.count(temp) == 0)
24+
{
25+
mp[temp] = 1;
26+
output.push_back(temp);
27+
}
28+
}
29+
return output.size();
30+
}
31+
32+
vector<vector<int>> getUniqueSubsets(vector<int>& arr) {
33+
// Write Your Code Here
34+
vector<vector<int>>output;
35+
int len = arr.size();
36+
getUniqueSubsets(arr, len, output);
37+
return output;
38+
}

Diff for: Lecture 4 - Recursion 2/Merge Sort Code.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
void mergeSort(int arr[], int l, int r) {
4+
5+
// Write Your Code Here
6+
int mid = (l + r) / 2;
7+
8+
if(l == r) return;
9+
10+
mergeSort(arr, l, mid);
11+
mergeSort(arr, mid + 1, r);
12+
13+
vector<int>temp;
14+
15+
int a1 = l;
16+
int a2 = mid + 1;
17+
18+
while(a1 != mid + 1 && a2 != r + 1)
19+
{
20+
if (arr[a1] < arr[a2])
21+
{
22+
temp.push_back(arr[a1]);
23+
a1++;
24+
}
25+
26+
else if(arr[a1] >= arr[a2])
27+
{
28+
temp.push_back(arr[a2]);
29+
a2++;
30+
}
31+
32+
}
33+
34+
if(a1 == mid + 1)
35+
{
36+
while(a2 != r + 1)
37+
{
38+
temp.push_back(arr[a2]);
39+
a2++;
40+
}
41+
}
42+
43+
if(a2 == r + 1)
44+
{
45+
while(a1 != mid + 1)
46+
{
47+
temp.push_back(arr[a1]);
48+
a1++;
49+
}
50+
}
51+
52+
for(int i = l; i <= r; i++) {
53+
arr[i] = temp[i - l];
54+
}
55+
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
5+
string key[] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
6+
7+
void printAll(int num, string output)
8+
{
9+
if(num == 0 || num == 1)
10+
{
11+
cout << output << endl;
12+
return;
13+
}
14+
15+
string KEY = key[num % 10];
16+
17+
for(int i = 0; i < KEY.size(); i++)
18+
{
19+
printAll(num / 10, KEY[i] + output);
20+
}
21+
}
22+
23+
void printKeypad(int num){
24+
/*
25+
Given an integer number print all the possible combinations of the keypad. You do not need to return anything just print them.
26+
*/
27+
string output = "";
28+
printAll(num, output);
29+
}

Diff for: Lecture 4 - Recursion 2/Print Permutations.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
5+
void Permutations(string input, string output)
6+
{
7+
if(input.length() == 0)
8+
{
9+
cout << output << endl;
10+
return;
11+
}
12+
13+
for(int i = 0; i <= output.length(); i++)
14+
{
15+
Permutations(input.substr(1), output.substr(0, i) + input[0] + output.substr(i));
16+
}
17+
}
18+
19+
void printPermutations(string input){
20+
21+
/* Don't write main() function.
22+
* Don't read input, it is passed as function argument.
23+
* Print output as specified in the question
24+
*/
25+
Permutations(input, "");
26+
}

Diff for: Lecture 4 - Recursion 2/Print Subset Sum to K.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
void printSubset(int input[], int size, int k, vector<int> arr) {
5+
if(size == 0)
6+
{
7+
int sum = 0;
8+
for (int i = 0; i < arr.size(); i++)
9+
{
10+
sum += arr[i];
11+
}
12+
if(sum == k)
13+
{
14+
for(int i = 0; i < arr.size(); i++)
15+
{
16+
cout << arr[i] << " ";
17+
}
18+
cout << endl;
19+
//return;
20+
}
21+
return;
22+
}
23+
24+
printSubset(input + 1, size - 1, k, arr);
25+
arr.push_back(input[0]);
26+
printSubset(input + 1, size - 1, k, arr);
27+
}
28+
29+
void printSubsetSumToK(int input[], int size, int k) {
30+
// Write your code here
31+
vector<int> arr;
32+
printSubset(input, size, k, arr);
33+
}

Diff for: Lecture 4 - Recursion 2/Print all Codes - String.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <string.h>
2+
using namespace std;
3+
4+
void printAllCodes(string input, string output)
5+
{
6+
if(input.length() == 0)
7+
{
8+
cout << output <<endl;
9+
return;
10+
}
11+
12+
int integer = input[0] - '0';
13+
char cr = (integer - 1) + 'a';
14+
15+
printAllCodes(input.substr(1, input.size() - 1), output + cr);
16+
17+
int integer1 = integer * 10 + input[1] - '0';
18+
char cr1 = (integer1 - 1) + 'a';
19+
20+
if(input.size() >= 2 && integer1 <= 26)
21+
{
22+
printAllCodes(input.substr(2, input.size() - 2), output + cr1);
23+
}
24+
}
25+
26+
void printAllPossibleCodes(string input) {
27+
/*
28+
Given the input as a string, print all its possible combinations. You do not need to return anything.
29+
*/
30+
string output = "";
31+
printAllCodes(input, output);
32+
}
33+

Diff for: Lecture 4 - Recursion 2/Quick Sort Code.cpp

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
The function is called with the parameters:
3+
quickSort(input, 0, size - 1);
4+
5+
*/
6+
7+
int partitionArray(int input[], int start, int end) {
8+
// Write your code here
9+
int pivot_element = input[start];
10+
int count = 0;
11+
int x = start;
12+
for(int i = start + 1; i <= end; i++)
13+
{
14+
if(input[i] <= pivot_element) count++;
15+
}
16+
17+
//keep pivot element at their correct index
18+
input[start] = input[start + count];
19+
input[start + count] = pivot_element;
20+
21+
while (start < end)
22+
{
23+
if(input[start] > pivot_element && input[end] <= pivot_element)
24+
{
25+
int temp = input[start];
26+
input[start] = input[end];
27+
input[end] = temp;
28+
start++;
29+
end--;
30+
}
31+
else if(input[start] <= pivot_element) start++;
32+
33+
else if(input[end] > pivot_element) end--;
34+
}
35+
return x + count;
36+
}
37+
38+
void quickSort(int input[], int start, int end) {
39+
/*
40+
Don't write main().
41+
Don't read input, it is passed as function argument.
42+
Change in the given array itself.
43+
Taking input and printing output is handled automatically.
44+
*/
45+
if(start >= end) return;
46+
47+
int pivot = partitionArray(input, start, end);
48+
49+
quickSort(input, start, pivot - 1);
50+
quickSort(input, pivot + 1, end);
51+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
void removeConsecutiveDuplicates(char *input) {
2+
/* Don't write main().
3+
* Don't read input, it is passed as function argument.
4+
* Change in the given string itself.
5+
* No need to return or print anything
6+
* Taking input and printing output is handled automatically.
7+
*/
8+
9+
if(input[0] == '\0') return;
10+
11+
if(input[0] == input[1])
12+
{
13+
//left shift
14+
int i = 1;
15+
while(input[i] != '\0')
16+
{
17+
input[i - 1] = input[i];
18+
i++;
19+
}
20+
input[i - 1] = '\0';
21+
return removeConsecutiveDuplicates(input);
22+
}
23+
return removeConsecutiveDuplicates(input + 1);
24+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
void replaceCharacter(char input[], char c1, char c2) {
2+
/* Don't write main().
3+
* Don't read input, it is passed as function argument.
4+
* No need to print or return the output.
5+
* Change in the given input string itself.
6+
* Taking input and printing output is handled automatically.
7+
*/
8+
9+
if(input[0] == '\0') return;
10+
11+
if (input[0] == c1)
12+
{
13+
input[0] = c2;
14+
return replaceCharacter(input + 1, c1, c2);
15+
}
16+
return replaceCharacter(input + 1, c1, c2);
17+
}

0 commit comments

Comments
 (0)