diff --git a/Mathematics_Algo/fibonacci_sum_squares.cpp b/Mathematics_Algo/fibonacci_sum_squares.cpp new file mode 100755 index 0000000..2e4e67e --- /dev/null +++ b/Mathematics_Algo/fibonacci_sum_squares.cpp @@ -0,0 +1,32 @@ +#include + +//Computes the last digit of Fibonacci series square sum- F0^2 + F1^2 + · · · + Fn^2 in constant time. +long long fibonacci_fast(int n) +{ + + long long f[n]; + f[0] = 0; + f[1] = 1; + + for (int i = 2; i <= n; i++) + { + f[i] = (f[i - 1] + f[i - 2]) % 10; + } + + return f[n]; +} +long long fib_square(long long n) +{ + if (n < 1) + return 0; + long long x = fibonacci_fast(n % 60); + long long y = fibonacci_fast((n + 1) % 60); + long long z = (x * y) % 10; + return z; +} +int main() +{ + long long n = 0; + std::cin >> n; + std::cout << fib_square(n); +} diff --git a/README.md b/README.md index 39cb373..5970627 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ The purpose of this repository is to get all the Algorithms required for Competi 13) totient :- Total numbers from 1 to N whose gcd with N is 1. 14) fibonacciNumber(n) :- Returns the nth Fibonacci Number in constant time. 15)Spiral Search in 2d array (C++ code) + 16) Computes the last digit of fibonacci square series F0^2 + F1^2 + · · · + Fn^2 in constant time. ## String_Algo 1) isVowel :- Check whether a character is vowel or not. @@ -34,3 +35,8 @@ The purpose of this repository is to get all the Algorithms required for Competi 1)Spiral search in 2d array +## Trees_Algo +1) LCA.cpp +2) Topview.cpp +3) Bottomview.cpp +4) Common_tree_traversal.cpp \ No newline at end of file diff --git a/Trees_Algo/common_tree_traversal_methods.cpp b/Trees_Algo/common_tree_traversal_methods.cpp new file mode 100755 index 0000000..05b2cfa --- /dev/null +++ b/Trees_Algo/common_tree_traversal_methods.cpp @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct node +{ + int data; + struct node *left; + struct node *right; +}; + +struct node *newnode(int data) +{ + struct node *p = new (struct node); + //struct node* p = (struct node*)malloc(sizeof(struct node)); + + p->data = data; + p->left = NULL; + p->right = NULL; + return p; +} + +//Inorder (Left, Root, Right) +//Preorder (Root, Left, Right) +//Postorder (Left, Right, Root) +//Levelorder +int height(node *root) +{ + // Write your code here. + if (root == NULL) + { + return 0; + } + if (root->left == NULL && root->right == NULL) + { + return 0; + } + return std::max(height(root->left) + 1, height(root->right) + 1); +} + +int printlevel(struct node *root, int level) +{ + if (root == NULL) + return 0; + if (level == 0) + cout << root->data << " "; + else + { + printlevel(root->left, level - 1); + printlevel(root->right, level - 1); + } +} + +void levelOrder(struct node *root) + +{ + int h = height(root); + for (int i = 0; i <= h; i++) + { + printlevel(root, i); + } +} + +int preorder(struct node *root) +{ + if (root == NULL) + return 0; + cout << root->data << " "; + preorder(root->left); + preorder(root->right); +} + +int inorder(struct node *root) +{ + if (root == NULL) + return 0; + inorder(root->left); + cout << root->data << " "; + inorder(root->right); +} +int postorder(struct node *root) +{ + if (root == NULL) + return 0; + postorder(root->left); + postorder(root->right); + cout << root->data << " "; +} + +int main() +{ + struct node *root = newnode(3); + root->left = newnode(7); + root->right = newnode(5); + root->right->right = newnode(11); + root->left->left = newnode(9); + + printf("\nTree traversed in preorder=\n"); + preorder(root); + printf("\nTree traversed in inorder=\n"); + inorder(root); + printf("\nTree traversed in postorder=\n"); + postorder(root); + printf("\nTree traversed in levelorder=\n"); + levelOrder(root); + + return 0; +}