diff --git a/Problem Statement 1/ps1.cpp b/Problem Statement 1/ps1.cpp new file mode 100644 index 0000000..017ac6e --- /dev/null +++ b/Problem Statement 1/ps1.cpp @@ -0,0 +1,23 @@ +#include +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; +} diff --git a/Problem Statement 2/ps2.cpp b/Problem Statement 2/ps2.cpp new file mode 100644 index 0000000..6824062 --- /dev/null +++ b/Problem Statement 2/ps2.cpp @@ -0,0 +1,29 @@ +#include +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; +} diff --git a/Problem Statement 3/ps3.cpp b/Problem Statement 3/ps3.cpp new file mode 100644 index 0000000..d8b9bbc --- /dev/null +++ b/Problem Statement 3/ps3.cpp @@ -0,0 +1,32 @@ +#include +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]); + // 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<