Skip to content

Commit 30ed317

Browse files
initial commit
1 parent abc5cc9 commit 30ed317

10 files changed

+584
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
vector<int> findCommonElements(vector<int> &a, vector<int> &b, vector<int> &c)
5+
{
6+
vector<int> ans;
7+
set<int> s;
8+
int i, j, k;
9+
i = j = k = 0;
10+
int n1 = a.size();
11+
int n2 = b.size();
12+
int n3 = c.size();
13+
while (i < n1 && j < n2 && k < n3)
14+
{
15+
if (a[i] == b[j] && b[j] == c[k])
16+
{
17+
s.insert(a[i]);
18+
i++, j++, k++;
19+
}
20+
else if (a[i] < b[j])
21+
{
22+
i++;
23+
}
24+
else if (b[j] < c[k])
25+
{
26+
j++;
27+
}
28+
else
29+
{
30+
k++;
31+
}
32+
}
33+
for (auto i : s)
34+
{
35+
ans.push_back(i);
36+
}
37+
return ans;
38+
}
39+
40+
int main()
41+
{
42+
cout << "Array 1: ";
43+
vector<int> a = {1, 5, 10, 20, 40, 80};
44+
for (int i = 0; i < a.size(); i++)
45+
cout << a[i] << " ";
46+
cout << endl;
47+
cout << "Array 2: ";
48+
vector<int> b = {6, 7, 20, 80, 100};
49+
for (int i = 0; i < b.size(); i++)
50+
cout << b[i] << " ";
51+
cout << endl;
52+
cout << "Array 3: ";
53+
vector<int> c = {3, 4, 15, 20, 30, 70, 80, 120};
54+
for (int i = 0; i < c.size(); i++)
55+
cout << c[i] << " ";
56+
cout << endl;
57+
cout << "Common elements: ";
58+
vector<int> ans = findCommonElements(a, b, c);
59+
for (int i = 0; i < ans.size(); i++)
60+
cout << ans[i] << " ";
61+
return 0;
62+
}

CS_30_SumOfTwoArrays.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
vector<int> findArraySum(vector<int> &a, int n, vector<int> &b, int m)
5+
{
6+
vector<int> ans;
7+
int sum = 0;
8+
int carry = 0;
9+
int i = n - 1;
10+
int j = m - 1;
11+
while (i >= 0 && j >= 0)
12+
{
13+
int val1 = a[i];
14+
int val2 = b[j];
15+
16+
sum = val1 + val2 + carry;
17+
carry = sum / 10;
18+
sum = sum % 10;
19+
ans.push_back(sum);
20+
i--;
21+
j--;
22+
}
23+
while (i >= 0 || j >= 0 || carry != 0)
24+
{
25+
if (i >= 0)
26+
{
27+
int sum = a[i] + carry;
28+
carry = sum / 10;
29+
sum = sum % 10;
30+
ans.push_back(sum);
31+
i--;
32+
}
33+
else if (j >= 0)
34+
{
35+
int sum = b[j] + carry;
36+
carry = sum / 10;
37+
sum = sum % 10;
38+
ans.push_back(sum);
39+
j--;
40+
}
41+
else if (carry != 0)
42+
{
43+
int sum = carry;
44+
carry = sum / 10;
45+
sum = sum % 10;
46+
ans.push_back(sum);
47+
}
48+
}
49+
reverse(ans.begin(), ans.end());
50+
return ans;
51+
}
52+
53+
int main()
54+
{
55+
vector<int> a = {9, 9, 9, 9, 9, 9, 9};
56+
vector<int> b = {1, 6, 8, 9, 9, 9, 9, 9};
57+
vector<int> ans = findArraySum(a, a.size(), b, b.size());
58+
for (int i = 0; i < ans.size(); i++)
59+
cout << ans[i] << " ";
60+
cout << endl;
61+
return 0;
62+
}

CS_31_FactorialOfALargeNumber.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// On GFG, this is the solution:
5+
// vector<int> factorial(int N)
6+
// {
7+
// // code here
8+
// vector<int> ans;
9+
// ans.push_back(1);
10+
// int carry = 0;
11+
// for (int i = 2; i <= N; i++)
12+
// {
13+
// for (int j = 0; j < ans.size(); j++)
14+
// {
15+
// int val = ans[j] * i + carry;
16+
// ans[j] = val % 10;
17+
// carry = val / 10;
18+
// }
19+
20+
// // if carry exits
21+
// while (carry)
22+
// {
23+
// ans.push_back(carry % 10);
24+
// carry /= 10;
25+
// }
26+
// }
27+
// reverse(ans.begin(), ans.end());
28+
// return ans;
29+
// }
30+
31+
// On codestudio, this is the solution:
32+
void multiply(string &s, int x)
33+
{
34+
int carry = 0, prod = 1;
35+
for (int i = 0; i < s.length(); i++)
36+
{
37+
prod = (x * (s[i] - '0')) + carry;
38+
carry = prod / 10;
39+
prod = prod % 10;
40+
s[i] = char(prod + '0');
41+
}
42+
while (carry)
43+
{
44+
s.push_back(char((carry % 10) + '0'));
45+
carry /= 10;
46+
}
47+
}
48+
string calculateFactorial(int n)
49+
{
50+
string ans = "1";
51+
for (int i = 2; i <= n; i++)
52+
{
53+
multiply(ans, i);
54+
}
55+
reverse(ans.begin(), ans.end());
56+
return ans;
57+
}
58+
59+
int main()
60+
{
61+
int n;
62+
cin >> n;
63+
string ans = calculateFactorial(n);
64+
cout << ans << endl;
65+
return 0;
66+
}

LC_10_SearchInRotatedSortedArray.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int findPivot(vector<int> &nums)
5+
{
6+
int s = 0;
7+
int n = nums.size();
8+
int e = n - 1;
9+
// Example (This function also works for non-rotated sorted array):
10+
// 2 4 6 8 10 - arr
11+
// 0 1 2 3 4 - index
12+
// ans = 4, as 10 i.e. pivot is at index 4
13+
while (s <= e)
14+
{
15+
int mid = s + (e - s) / 2;
16+
// corner case
17+
if (s == e)
18+
{
19+
return s;
20+
}
21+
22+
if (mid + 1 < n && nums[mid] > nums[mid + 1])
23+
{
24+
return mid;
25+
}
26+
else if (mid - 1 >= 0 && nums[mid - 1] > nums[mid])
27+
{
28+
return mid - 1;
29+
}
30+
else if (nums[s] > nums[mid])
31+
{ // we're currently on portion B
32+
// ans is in left portion
33+
e = mid - 1;
34+
}
35+
else
36+
{ // we're currently on portion A
37+
// ans is in right portion
38+
s = mid + 1;
39+
}
40+
}
41+
return -1;
42+
}
43+
44+
int binarySearch(vector<int> &arr, int s, int e, int target)
45+
{
46+
while (s <= e)
47+
{
48+
int mid = s + (e - s) / 2;
49+
if (arr[mid] == target)
50+
return mid;
51+
else if (target > arr[mid])
52+
{
53+
s = mid + 1;
54+
}
55+
else
56+
{
57+
e = mid - 1;
58+
}
59+
}
60+
return -1;
61+
}
62+
63+
int search(vector<int> &arr, int target)
64+
{
65+
int pivot = findPivot(arr);
66+
int n = arr.size();
67+
int ans = -1;
68+
69+
// search in A
70+
if (target >= arr[0] && target <= arr[pivot])
71+
{
72+
ans = binarySearch(arr, 0, pivot, target);
73+
}
74+
else
75+
{
76+
ans = binarySearch(arr, pivot + 1, n - 1, target);
77+
}
78+
return ans;
79+
}
80+
81+
int main()
82+
{
83+
vector<int> arr = {4, 5, 6, 7, 0, 1, 2};
84+
int target = 7;
85+
cout << search(arr, target) << endl;
86+
return 0;
87+
}

LC_11_Sqrt(x).cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int mySqrt(int x)
5+
{
6+
int s = 0, e = x;
7+
int ans = -1;
8+
long long mid = 0;
9+
10+
while (s <= e)
11+
{
12+
mid = s + (e - s) / 2;
13+
// ans found
14+
if (mid * mid == x)
15+
{
16+
return mid;
17+
}
18+
else if (mid * mid < x)
19+
{
20+
// store ans
21+
// and & move right
22+
ans = mid;
23+
s = mid + 1;
24+
}
25+
else
26+
{
27+
// move left
28+
e = mid - 1;
29+
}
30+
}
31+
return ans;
32+
}
33+
34+
int main()
35+
{
36+
int x = 2147395599;
37+
cout << mySqrt(x) << endl;
38+
return 0;
39+
}

LC_12_SearchInA2dMatrix.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// tc: O(log (m*n))
5+
bool searchMatrix(vector<vector<int>> &m, int target)
6+
{
7+
int row = m.size();
8+
int col = m[0].size();
9+
int n = row * col;
10+
11+
int s = 0;
12+
int e = n - 1;
13+
14+
while (s <= e)
15+
{
16+
int mid = s + (e - s) / 2;
17+
int rowIndex = mid / col;
18+
int colIndex = mid % col;
19+
int curr = m[rowIndex][colIndex];
20+
21+
if (curr == target)
22+
{
23+
return true;
24+
}
25+
else if (target > curr)
26+
{
27+
// right
28+
s = mid + 1;
29+
}
30+
else
31+
{
32+
// left
33+
e = mid - 1;
34+
}
35+
}
36+
return false;
37+
}
38+
39+
int main()
40+
{
41+
vector<vector<int>> m = {{1, 3, 5, 7},
42+
{10, 11, 16, 20},
43+
{23, 30, 34, 60}};
44+
int target = 3;
45+
cout << searchMatrix(m, target) << endl;
46+
return 0;
47+
}

0 commit comments

Comments
 (0)