diff --git a/Problems/Data-structures/Day-04/sol/Naman Pal/day004sol01.cpp b/Problems/Data-structures/Day-04/sol/Naman Pal/day004sol01.cpp new file mode 100644 index 00000000..04f46ec1 --- /dev/null +++ b/Problems/Data-structures/Day-04/sol/Naman Pal/day004sol01.cpp @@ -0,0 +1,45 @@ +// Submission Link: https://codeforces.com/contest/1997/submission/355835686 + + +/* +So first I made the RBS with minimmum cost by putting closing bracket as soon as we get a opening bracket before it, i.e., closing bracket as early as +possible(greedy appraoch). After getting corect bracket sequence I put the indexes of '(' in vector o and indexes of ')' in vector c in order they occur. Both +vector must contain n/2 elements and now for final answer I simply added the difference of each element in o and c at same index to ans to calculate cost +which was initially zero and then printed the ans. +*/ +#include +using namespace std; + +int main() { + long long t; + cin>>t; + while(t--) { + long long n; + cin>>n; + string s; + cin>>s; + long long a=0; + for(long long i=0; i0) { + s[i]=')'; + a--; + } + else { + s[i]='('; + a++; + } + } + } + long long ans=0; + vector o, c; + for(long long i=0; i (a+b)/b (Moves to the Right Child) + 2. Parallel: a/b -> a/(a+b) (Moves to the Left Child) + +Approach: If a resistance a/b can be obained with k resistors then it is clear that we can get resistance (a+b)/b(if 1 ohm in series) or +a/(a+b)(if 1 ohm in parellel) with k+1 resistance. So we just go in reverse direction if a is greater than b then the last step must have been adding resistors +in series so we add a/b to our answer and make a equal to a%b.. similarly if b is greater than a then The last step must have been adding resistors in parallel so +we add b/a in answer and make b=b%a. This process continues untile either a or b becomes zero as either of a and b can become zero only when one is +completely divisible by other and in this case we will get minimum number of resistor as the quotient. + +Time Complexity: O(log(min(a, b))) as it is equivalent to the Euclidean GCD algorithm +*/ + +#include +using namespace std; + +int main() { + long long a,b; + cin>>a>>b; + long long ans=0; + while(a>0 && b>0) { + if(a>b) { + ans+=a/b; + a=a%b; + } + else { + ans+=b/a; + b=b%a; + } + } + cout<