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
35 changes: 35 additions & 0 deletions Ashu Yadav/Perm of a string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//This program will solve the mentioned issue number #72
//Credits:Ashu Yadav
//Finding all permutation that can be formed by a user entered string.
//Im considering small input string for the current moment of time

#include<bits/stdc++.h>
using namespace std;

void ans(string s,int beg,int end){
//if the beg and ending index of the string are same in our passed rec fun then print the string
//this is our base case that will stop the rec call
if(beg==end){
cout<<s<<endl;
}else{
//traversing the string from beg index to last
for(int i=beg;i<=end;i++){
//using ibuilt swap function,you can create your own swap function also using a temp variable
swap(s[beg],s[i]);
//call to our fun
ans(s,beg+1,end);
swap(s[beg],s[i]);
}
}
}

int main(){
//user entered string is stored
string s;
cin>>s;
//length of the string
int len=s.length();
//our recursive function called
ans(s,0,len-1);
return 0;
}
34 changes: 34 additions & 0 deletions Ashu Yadav/count of pairs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// We have to count the number of pairs whose sum is divisible by the number pased by the user
//iterative search can give TLE so we will be using another approach here
//Credits:Ashu Yadav

#include<bits/stdc++.h>
using namespace std;
//logic
int ans(int a[],int n,int k){
//array to store remainder
int occ[k]={0};
//remainder
for(int i=0;i<n;i++)occ[a[i]%k]++;
//count initiated
int count=occ[0]*(occ[n-1])/2;
//counting all apirs using n*n-1/2 as number of pairs
for(int i=1;i<=k/2 && i!=(k-i);i++){
count+=occ[i]*occ[k-i];
if(k%2==0)count+=(occ[k/2]*occ[k/2-1])/2;
}
return count;
}

int main(){
//size of array
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)cin>>a[i];
int k;
cin>>k;
//ans
cout<<ans(a,n,k)<<endl;
return 0;
}
50 changes: 50 additions & 0 deletions Ashu Yadav/max height of tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//we have to find the max height of the binary tree
//We will use the recursive aproach to find the height of the tree
//Credits:Ashu Yadav

#include<bits/stdc++.h>
using namespace std;
//Finding the height of the tree
int height(node* node)
{
//base case to terminate the rec call
if (node == NULL)
return 0;
else
{
int lefth = height(node->left);
int righth = height(node->right);

if (lefth > righth)
return(lefth + 1);
else return(righth + 1);
}
}
//A node class to define the char of a node
class node
{
public:
int data;
//pointers for pointing the left subtree and rightsubtree nodes
node* left;
node* right;
};

//for making new nodes
node* nnode(int value)
{
node* Node = new node();
Node->data = value;
Node->left = NULL;
Node->right = NULL;

return(Node);
}
int main(){
//Here we can take the data from the user of can pre define the data
//use "nnode" function to make new node and assigning values to it

//finally we can call height function defined to get the height of the tree
cout<<height(root)<<endl;
return 0;
}
63 changes: 63 additions & 0 deletions Ashu Yadav/traversal in tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//Implementation of various tree traversal techniques
//Credtis:Ashu Yadav

#include<bits/stdc++.h>
using namespace std;
//Char of node
struct Node
{
int data;
struct Node* left, *right;
Node(int data)
{
this->data = data;
left = right = NULL;
}
};
//function to print postorder
void Postorder(struct Node *node){
//base case to terminate rec
if(node==NULL)return;
//left subtree
Postorder(node->left);
//right subtree
Postorder(node->right);
//print data of node
cout<<node->data<<" ";
}

//function to print preorder
void Preorder(struct Node *node){
//base case to terminate rec
if(node==NULL)return;
//print data of node
cout<<node->data<<" ";
//left subtree
Preorder(node->left);
//right subtree
Preorder(node->right);
}

//function to print inorder
void Inorder(struct Node *node){
//base case to terminate rec
if(node==NULL)return;
//left subtree
Inorder(node->left);
//print data of node
cout<<node->data<<" ";
//right subtree
Inorder(node->right);
}
int main(){
//Here we can take the data from the user of can pre define the data
//use "nnode" function to make new node and assigning values to it

//prints preorder traversal
cout<<Preorder(root)<<endl;
//prints postorder traversal
cout<<Postorder(root)<<endl;
//prints inorder traversal of the tree
cout<<Inorder(root)<<endl;
return 0;
}