-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLecture21.cpp
More file actions
107 lines (84 loc) · 2.7 KB
/
Copy pathLecture21.cpp
File metadata and controls
107 lines (84 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include<iostream>
#include<vector>
using namespace std;
// ROTATE ARRAY
class Sol {
public:
void rotate(int nums[], int k) {
// just to practice the modulus approach
int n = 4;
int temp[n];
int i = 0;
for(;i<n;i++) {
temp[(i+k)%n] = nums[i];
}
// copy temp into num vector
nums = temp;
}
};
// Sorted Rotated Array
class Solution {
public:
bool check(int nums[]) {
int cnt = 0;
for(int i=1;i<nums[4];i++)
{
if(nums[i] < nums[i-1])
cnt++;
}
if(nums[nums[4]-1] > nums[0])
cnt++;
return cnt<=1;
}
};
// ADD ARRAYS
vector<int> reverse(vector<int> v) {
int s = 0;
int e = v.size()-1;
while(s<e)
{
swap(v[s++], v[e--]);
}
return v;
}
vector<int> findArraySum(vector<int>&a, int n, vector<int>&b, int m) {
int i = n-1;
int j = m-1;
vector<int> ans;
int carry = 0;
while(i>=0 && j>=0) {
int val1 = a[i];
int val2 = b[j];
int sum = val1 + val2 + carry;
carry = sum/10;
sum = sum%10;
ans.push_back(sum);
i--;
j--;
}
// first case
while(i>=0) {
int sum = a[i] + carry;
carry = sum/10;
sum = sum%10;
ans.push_back(sum);
i--;
}
// second case
while(j>=0) {
int sum = b[j] + carry;
carry = sum/10;
int value = sum%10;
ans.push_back(value);
j--;
}
// third case
while(carry != 0) {
int sum = carry;
carry = sum/10;
sum = sum%10;
ans.push_back(sum);
}
//print(ans);
return reverse(ans);
}