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 divide and conquer/Modular Exponentation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include<bits/stdc++.h>
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<<res<<"\n";
}
}
}
49 changes: 49 additions & 0 deletions dynamic programming/LastDigit of Nth Fibonacci.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

/* Program to print last digit of nth fibonacci
Input:
N = 14
Output:
7
Explanation:
14th Fibonacci number is 377
It's last digit is 7

*/

#include<bits/stdc++.h>
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<n;i++)
a[i]=(a[i-1]+a[i-2])%10;
return a[n-1];
}

}
};


int main()
{
int t;
cin>>t;
while(t--)
{
int N;
cin>>N;
Solution ob;
cout << ob.fib(N) << endl;
}
return 0;
}
74 changes: 74 additions & 0 deletions stack/paranthesisChecker.cpp
Original file line number Diff line number Diff line change
@@ -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<bits/stdc++.h>
using namespace std;
int main() {
int t;cin>>t;
while(t--)
{
string n;
cin>>n;
stack<int>s;
int flag=0;
for(int i=0;i<n.length();i++)
{
/* While traversing the string if found symbols like '(', '{' and '[' push them in stack*/
if(n[i]=='(' || n[i]=='{' || n[i]=='[')
s.push(n[i]);
else
{
/* If string starts with ')', '}, or ']', then the string can never be balances, so raise the flag*/
if(s.empty())
{
flag=1;
break;
}
else if(n[i]==')')
{
/* If string starts with ')' then the top of the stack must contain '(' for the string to be valid.
Pop the top of stack.
*/
int t=s.top();
s.pop();
if(t!='(')
{
flag=1;break;
}
}
else if(n[i]=='}')
{
/* If string starts with '}' then the top of the stack must contain '(' for the string to be valid.
Pop the top of stack.
*/
int t=s.top();
s.pop();
if(t!='{')
{
flag=1;break;
}
}
else if(n[i]==']')
{
/* If string starts with ']' then the top of the stack must contain '(' for the string to be valid.
Pop the top of stack.
*/
int t=s.top();
s.pop();
if(t!='[')
{
flag=1;break;
}
}
}
}
/* If at last the stack is empty and the flag value is also 1, then it is balanced else unbalanced.
*/
if(flag==0 && s.empty())
cout<<"balanced\n";
else
cout<<"not balanced\n";
}
}