Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Problem Statement 1/ps1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <bits/stdc++.h>
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";

// Time complexity: O(1)
// Space complexity: O(1)
return 0;
}
29 changes: 29 additions & 0 deletions Problem Statement 2/ps2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <bits/stdc++.h>
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<int> 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<<ans<<endl;

// Time complexity: O(n*5)
// Space complexity: O(5) => O(1)
return 0;
}
32 changes: 32 additions & 0 deletions Problem Statement 3/ps3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <bits/stdc++.h>
using namespace std;

int main() {
// Take input
int n,k;
cin>>n;
vector<int> 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<int> 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]);
// 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<<dp.back();

// Time complexity: O(n*k)
// Space complexity: O(n)
return 0;
}