From 72ee0869d80dcf5b278c703d8b6422661fcf2e2c Mon Sep 17 00:00:00 2001 From: Rishabh Raizada Date: Sun, 24 Apr 2022 15:17:24 +0530 Subject: [PATCH 1/7] Create ps1.cpp --- Problem Statement 1/ps1.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Problem Statement 1/ps1.cpp diff --git a/Problem Statement 1/ps1.cpp b/Problem Statement 1/ps1.cpp new file mode 100644 index 0000000..3ab418a --- /dev/null +++ b/Problem Statement 1/ps1.cpp @@ -0,0 +1,9 @@ +#include +using namespace std; + +int main() { + int n; + cin>>n; + (n&1)?cout<<"false":cout<<"true"; + return 0; +} From 5e61840c1511f32378ae6c4124d744d317f2b5f5 Mon Sep 17 00:00:00 2001 From: Rishabh Raizada Date: Sun, 24 Apr 2022 15:21:30 +0530 Subject: [PATCH 2/7] Create ps2.cpp --- Problem Statement 2/ps2.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Problem Statement 2/ps2.cpp diff --git a/Problem Statement 2/ps2.cpp b/Problem Statement 2/ps2.cpp new file mode 100644 index 0000000..f354789 --- /dev/null +++ b/Problem Statement 2/ps2.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; + +int main() { + int n,ans=0; + cin>>n; + vector v(5); + v[0]=1; + for (int i = 2; i <= N; i++) { + for (int j = 3; j >= 0; j--) + v[j] += v[j + 1]; + } + for (auto c : v) + ans += c; + cout< Date: Sun, 24 Apr 2022 15:23:23 +0530 Subject: [PATCH 3/7] Update ps2.cpp --- Problem Statement 2/ps2.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Problem Statement 2/ps2.cpp b/Problem Statement 2/ps2.cpp index f354789..d4e6f72 100644 --- a/Problem Statement 2/ps2.cpp +++ b/Problem Statement 2/ps2.cpp @@ -4,9 +4,8 @@ using namespace std; int main() { int n,ans=0; cin>>n; - vector v(5); - v[0]=1; - for (int i = 2; i <= N; i++) { + vector v(5,1); + for (int i = 2; i <= n; i++) { for (int j = 3; j >= 0; j--) v[j] += v[j + 1]; } @@ -14,5 +13,5 @@ int main() { ans += c; cout< Date: Sun, 24 Apr 2022 15:49:57 +0530 Subject: [PATCH 4/7] Create ps3.cpp --- Problem Statement 3/ps3.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Problem Statement 3/ps3.cpp diff --git a/Problem Statement 3/ps3.cpp b/Problem Statement 3/ps3.cpp new file mode 100644 index 0000000..b7b25b9 --- /dev/null +++ b/Problem Statement 3/ps3.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +int main() { + int n,k; + cin>>n; + vector v(n); + for (auto &i:v) cin>>i; + cin>>k; + + vector dp(n+1,0); + for (int i=1;i<=n;++i) { + int curr = 0, mx = 0; + for (int j=1;j<=k && i-j>=0;++j) { + curr = max(curr,v[i-j]); + mx = curr*j + dp[i-j]; + dp[i] = max(dp[i],mx); + } + } + cout< Date: Sun, 24 Apr 2022 16:23:55 +0530 Subject: [PATCH 5/7] Added comments for ps1 --- Problem Statement 1/ps1.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Problem Statement 1/ps1.cpp b/Problem Statement 1/ps1.cpp index 3ab418a..017ac6e 100644 --- a/Problem Statement 1/ps1.cpp +++ b/Problem Statement 1/ps1.cpp @@ -2,8 +2,22 @@ using namespace std; int main() { + // Take input n int n; cin>>n; + + /* Explanation: + The following pattern can be clearly seen: + when n=1, Anuj loses + when n=2, Anuj wins since he can choose x=1 + when n=3, Anuj loses since he can only choose x=1 leaving with 2. From above statement, the person who is left with 2 will win hence Anvi wins. + when n=4, Anuj wins as he can choose x=1 to reduce it to an odd number 3. Froom above statement, the person left with 3 loses, hence Anuj wins. + ans so on.. + therefore: if n is odd ans is false and if n is even ans is true. + */ (n&1)?cout<<"false":cout<<"true"; - return 0; + + // Time complexity: O(1) + // Space complexity: O(1) + return 0; } From def0692328c85f70de841fe2d7e4553b75c70da4 Mon Sep 17 00:00:00 2001 From: Rishabh Raizada Date: Sun, 24 Apr 2022 16:24:09 +0530 Subject: [PATCH 6/7] Added comments for ps2 --- Problem Statement 2/ps2.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Problem Statement 2/ps2.cpp b/Problem Statement 2/ps2.cpp index d4e6f72..6824062 100644 --- a/Problem Statement 2/ps2.cpp +++ b/Problem Statement 2/ps2.cpp @@ -3,15 +3,27 @@ using namespace std; int main() { int n,ans=0; + // Take input n cin>>n; + + // for every character 'x' in [a,e,i,o,u] we need to put '5-x+1' characters n number of times and hence we are using 2 nested for loops. + + // Initialising v vector of size 5 with 1s which is default answer when n=1; + // Also, v[4] will always be 1, ie, uuuu...n times. vector v(5,1); + // calculating answer starting from n=2 to n and adding in vector v corresponding to each vowel. for (int i = 2; i <= n; i++) { for (int j = 3; j >= 0; j--) + // for length i, and vowel represented by position j, vector v stores all combinations possible. v[j] += v[j + 1]; } + + // finally we sum all the elements in v pertaining to combinations starting from each corresponding vowel. for (auto c : v) ans += c; cout< O(1) return 0; } From 61aea6fd6817767c3ffbb2b6798e8a9497fc5925 Mon Sep 17 00:00:00 2001 From: Rishabh Raizada Date: Sun, 24 Apr 2022 16:24:18 +0530 Subject: [PATCH 7/7] Added comments for ps3 --- Problem Statement 3/ps3.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Problem Statement 3/ps3.cpp b/Problem Statement 3/ps3.cpp index b7b25b9..d8b9bbc 100644 --- a/Problem Statement 3/ps3.cpp +++ b/Problem Statement 3/ps3.cpp @@ -2,22 +2,31 @@ using namespace std; int main() { + // Take input int n,k; cin>>n; vector v(n); for (auto &i:v) cin>>i; cin>>k; + // i am using dp[i] to store answer for v[0]...v[i-1], hence initialising a dp of size n+1 with 0s. vector dp(n+1,0); + // this loop iterates on all elements and computes dp[i] for (int i=1;i<=n;++i) { + // intialising current maximum possible (curr) and newly computed temporary maxium (mx) as 0 int curr = 0, mx = 0; + // this loop checks for the previous values (upto k) to check if we can improve the answer. for (int j=1;j<=k && i-j>=0;++j) { + // store current maxium possible from previous (upto k) values curr = max(curr,v[i-j]); - mx = curr*j + dp[i-j]; + // update dp[i] to store maxium possible using curr as the current maxium possible + mx = curr*j + dp[i-j]; dp[i] = max(dp[i],mx); } } cout<