From b154f5e6167fe2aff0f8ea99ee49ee816c1d2581 Mon Sep 17 00:00:00 2001 From: Aparna Juhi <54853999+AparnaJuhi@users.noreply.github.com> Date: Thu, 1 Oct 2020 17:52:06 +0530 Subject: [PATCH] Added some cp solutions Modular Exponentiation problem was asked in Google and 'Last Digit of Nth Fibonacci' problem was asked in MAQ Software and the 'Paranthesis Checker' is a famous problem of Stacks --- divide and conquer/Modular Exponentation.cpp | 23 ++++++ .../LastDigit of Nth Fibonacci.cpp | 49 ++++++++++++ stack/paranthesisChecker.cpp | 74 +++++++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 divide and conquer/Modular Exponentation.cpp create mode 100644 dynamic programming/LastDigit of Nth Fibonacci.cpp create mode 100644 stack/paranthesisChecker.cpp diff --git a/divide and conquer/Modular Exponentation.cpp b/divide and conquer/Modular Exponentation.cpp new file mode 100644 index 0000000..990c47b --- /dev/null +++ b/divide and conquer/Modular Exponentation.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; +int main() { + long long int t;cin>>t; + while(t--) + { + long long int x,y,p,res=1; + cin>>x>>y>>p; + if(x==0) + cout<<"0\n"; + else + { + while(y) + { + if(y%2!=0) + res=(res*x)%p; + y=y/2; + x=(x*x)%p; + } + cout< +using namespace std; + +class Solution{ +public: + int fib(int n){ + if(n==0) + return 0; + else if(n==1 || n==2) + return 1; + else + { + int a[n]; + a[0]=1; + a[1]=1; + for(int i=2;i>t; + while(t--) + { + int N; + cin>>N; + Solution ob; + cout << ob.fib(N) << endl; + } + return 0; +} diff --git a/stack/paranthesisChecker.cpp b/stack/paranthesisChecker.cpp new file mode 100644 index 0000000..7726472 --- /dev/null +++ b/stack/paranthesisChecker.cpp @@ -0,0 +1,74 @@ +/* The famous problem of checking Paranthesis is 'Balanced' or 'Not Balanced' using stacks. +For example, the program should print 'balanced' for exp = “[()]{}{[()()]()}” +and 'not balanced' for exp = “[(])” +*/ + +#include +using namespace std; +int main() { + int t;cin>>t; + while(t--) + { + string n; + cin>>n; + stacks; + int flag=0; + for(int i=0;i