Skip to content
85 changes: 85 additions & 0 deletions 2stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include<bits/stdc++.h>
using namespace std;
#define ll long long

class t2stack
{
ll *a;
ll size,top1,top2;
public:
t2stack(ll n)
{
size=n;
a=new ll[n];
top1=-1;
top2=size;
}

void push1(ll n)
{
if(top1+1<top2)
{
top1++;
a[top1]=n;
}
else{
cout<<"STACK OVERFLOW"<<endl;
}
}
void push2(ll n)
{
if(top1+1<top2)
{
top2--;
a[top2]=n;
}
else
{
cout<<"STACK OVERFLOW"<<endl;
}
}
void pop1()
{
if(top1>=0)
{
cout<<a[top1]<<endl;
top1--;
}
else
{
cout<<"STACK UNDERFLOW"<<endl;
}
}
void pop2()
{
if(top2<=size-1)
{
cout<<a[top2]<<endl;
top2++;
}
else
{
cout<<"STACK UNDERFLOW"<<endl;
}
}

};

int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif

t2stack pr(4);
pr.push1(2);
pr.push2(3);
pr.push1(3);
pr.push2(5);
pr.pop1();
pr.pop2();



return 0;
}
133 changes: 133 additions & 0 deletions AVLTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include<bits/stdc++.h>
using namespace std;

struct Node{
int data;
Node *left;
Node *right;
int height;
}*Root = NULL;

Node* new_Node(int DATA)
{
Node* node = new Node();
node->data = DATA;
node->left = NULL;
node->right = NULL;
node->height = 1;
return(node);
}

int height(Node *a)
{
if(a==NULL)
return 0;
return a->height;
}
int balance(Node *a)
{
if(a==NULL)
return 0;
int lh = height(a->left);
int rh = height(a->right);
return (lh-rh);
}
Node *RR(Node *y)
{
Node *x = y->left;
Node *T2 = x->right;
x->right = y;
y->left = T2;
y->height = max(height(y->left),
height(y->right)) + 1;
x->height = max(height(x->left),
height(x->right)) + 1;
return x;
}

Node *LL(Node *x)
{
Node *y = x->right;
Node *T2 = y->left;
y->left = x;
x->right = T2;
x->height = max(height(x->left),
height(x->right)) + 1;
y->height = max(height(y->left),
height(y->right)) + 1;
return y;
}
Node* insertToAVL(Node* root, int data)
{
if(root==NULL)
{
Node* Nodes = new_Node(data);
return(Nodes);
}
if(root->data>data)
{
root->left = insertToAVL(root->left,data);
}
else if(root->data<data)
{
root->right = insertToAVL(root->right,data);
}
else
return root;

root->height = max(height(root->left),height(root->right)) + 1;

int factor = balance(root);

if(factor > 1 && root->left->data > data)
{
return RR(root);
}
if(factor < -1 && root->right->data < data)
{
return LL(root);
}
if (factor > 1 && data > root->left->data)
{
root->left = LL(root->left);
return RR(root);
}
if (factor < -1 && data < root->right->data)
{
root->right = RR(root->right);
return LL(root);
}
return root;
}
void inorder(Node* root)
{
if(root != NULL)
{
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
}
int main()
{
int size;
cout << "\nEnter num of node :- ";
cin >> size;
vector<int> vec;
cout << "\nEnter values :- ";
for(int i = 0; i < size; i++)
{
int x;
cin >> x;
vec.push_back(x);
}
Root = insertToAVL(Root, vec[0]);
for(int i = 1; i < size; i++) {
Root = insertToAVL(Root, vec[i]);
}
cout << "\nInOrder Traversal is:- \n";
inorder(Root);
cout<< "\nRoot Node is:- \n";
cout<<Root->data<<"\n";
return 0;
}
77 changes: 77 additions & 0 deletions DynamicProgramming/BoardPath.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package DynamicProgramming;
/*
* this is an important Algo in which
* we have starting and ending of board and we have to reach
* we have to count no. of ways
* that help to reach end point i.e number by rolling dice
* which have 1 to 6 digits

Test Case:
here target is 10

int n=10;
startAlgo();
System.out.println(bpR(0,n));
System.out.println(endAlgo()+"ms");
int[] strg=new int [n+1];
startAlgo();
System.out.println(bpRS(0,n,strg));
System.out.println(endAlgo()+"ms");
startAlgo();
System.out.println(bpIS(0,n,strg));
System.out.println(endAlgo()+"ms");



*/
public class BoardPath {
public static long startTime;
public static long endTime;
public static void startAlgo() {
startTime=System.currentTimeMillis();
}
public static long endAlgo() {
endTime=System.currentTimeMillis();
return endTime-startTime;
}
public static int bpR(int start,int end){
if(start==end) {
return 1;
}
else if(start>end)
return 0;
int count=0;
for(int dice=1;dice<=6;dice++) {
count+=bpR(start+dice,end);
}
return count;
}
public static int bpRS(int curr,int end,int strg[]){
if(curr==end) {
return 1;
}
else if(curr>end)
return 0;
if(strg[curr]!=0)
return strg[curr];
int count=0;
for(int dice=1;dice<=6;dice++) {
count+=bpRS(curr+dice,end,strg);
}
strg[curr]=count;
return count;
}
public static int bpIS(int curr,int end,int[]strg){
strg[end]=1;
for(int i=end-1;i>=0;i--) {
int count=0;
for(int dice=1;dice<=6&&dice+i<strg.length;dice++) {
count+=strg[i+dice];
}
strg[i]=count;
}
return strg[0];
}


}
79 changes: 79 additions & 0 deletions DynamicProgramming/CoinChange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package DynamicProgramming;

/**
* @author Varun Upadhyay (https://github.com/varunu28)
*/
public class CoinChange {

// Driver Program
public static void main(String[] args) {

int amount = 12;
int[] coins = {2, 4, 5};

System.out.println("Number of combinations of getting change for " + amount + " is: " + change(coins, amount));
System.out.println("Minimum number of coins required for amount :" + amount + " is: " + minimumCoins(coins, amount));

}

/**
* This method finds the number of combinations of getting change for a given amount and change coins
*
* @param coins The list of coins
* @param amount The amount for which we need to find the change
* Finds the number of combinations of change
**/
public static int change(int[] coins, int amount) {

int[] combinations = new int[amount + 1];
combinations[0] = 1;

for (int coin : coins) {
for (int i = coin; i < amount + 1; i++) {
combinations[i] += combinations[i - coin];
}
// Uncomment the below line to see the state of combinations for each coin
// printAmount(combinations);
}

return combinations[amount];
}

/**
* This method finds the minimum number of coins needed for a given amount.
*
* @param coins The list of coins
* @param amount The amount for which we need to find the minimum number of coins.
* Finds the the minimum number of coins that make a given value.
**/
public static int minimumCoins(int[] coins, int amount) {
//minimumCoins[i] will store the minimum coins needed for amount i
int[] minimumCoins = new int[amount + 1];

minimumCoins[0] = 0;

for (int i = 1; i <= amount; i++) {
minimumCoins[i] = Integer.MAX_VALUE;
}
for (int i = 1; i <= amount; i++) {
for (int coin : coins) {
if (coin <= i) {
int sub_res = minimumCoins[i - coin];
if (sub_res != Integer.MAX_VALUE && sub_res + 1 < minimumCoins[i])
minimumCoins[i] = sub_res + 1;
}
}
}
// Uncomment the below line to see the state of combinations for each coin
//printAmount(minimumCoins);
return minimumCoins[amount];
}

// A basic print method which prints all the contents of the array
public static void printAmount(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
Loading