Skip to content

Instantly share code, notes, and snippets.

@mycodeschool
mycodeschool / gcd.cpp
Last active September 10, 2023 08:58
GCD: Brute force and Euclid's algorithm
View gcd.cpp
#include<algorithm>
using namespace std;
int euclid_gcd(int a, int b)
{
while (b != 0)
{
int r = a % b;
a = b;
View FindMergePointOfLinkedList.cpp
#include<iostream>
#include<cstdlib>
#include<set>
using namespace std;
struct Node {
int data;
struct Node *next;
};
int length(struct Node *head) {
View MergeSort_C.C
/* Merge sort in C */
#include<stdio.h>
#include<stdlib.h>
// Function to Merge Arrays L and R into A.
// lefCount = number of elements in L
// rightCount = number of elements in R.
void Merge(int *A,int *L,int leftCount,int *R,int rightCount) {
int i,j,k;
View MaximumSumSubarray_bruteforce01.cpp
#include<cmath>
#include<iostream>
#include<climits>
using namespace std;
int Maximum_Sum_Subarray(int arr[],int n) //Overall Time Complexity O(n^3)
{
int ans = INT_MIN; // #include<climits>
for(int sub_array_size = 1;sub_array_size <= n; ++sub_array_size) //O(n)
{
View MaximumSumSubarray_Bruteforce02.cpp
#include<cmath>
#include<iostream>
#include<climits>
using namespace std;
int Maximum_Sum_Subarray(int arr[],int n) //Overall Time Complexity O(n^2)
{
int ans = INT_MIN;
for(int start_index = 0;start_index < n; ++start_index) //O(n)
{
View MaximumSumSubarray_NlogN.cpp
#include<cmath>
#include<iostream>
#include<climits>
using namespace std;
int Max_Subarray_Sum(int arr[],int n)
{
if(n==1)
{
return arr[0];
View MaximumSumSubarray_Kadane.cpp
#include<cmath>
#include<iostream>
#include<climits>
using namespace std;
int Maximum_Sum_Subarray(int arr[],int n) //Overall Time Complexity O(n)
{
int ans = A[0],sum = 0;
for(int i = 1;i < n; ++i) //Check if all are negative
ans = max(ans,arr[i]);
View PostfixEvaluation.cpp
/*
Evaluation Of postfix Expression in C++
Input Postfix expression must be in a desired format.
Operands must be integers and there should be space in between two operands.
Only '+' , '-' , '*' and '/' operators are expected.
*/
#include<iostream>
#include<stack>
#include<string>
@mycodeschool
mycodeschool / InfixToPostfix.cpp
Created December 9, 2013 05:34
Infix to Postfix conversion in C++ using stack. We are assuming that both operators and operands in input will be single character.
View InfixToPostfix.cpp
/*
Infix to postfix conversion in C++
Input Postfix expression must be in a desired format.
Operands and operator, both must be single character.
Only '+' , '-' , '*', '/' and '$' (for exponentiation) operators are expected.
*/
#include<iostream>
#include<stack>
#include<string>
@mycodeschool
mycodeschool / PreorderInorderPostorder_CPP.cpp
Last active October 29, 2023 09:20
Binary tree traversal: Preorder, Inorder, Postorder
View PreorderInorderPostorder_CPP.cpp
/* Binary Tree Traversal - Preorder, Inorder, Postorder */
#include<iostream>
using namespace std;
struct Node {
char data;
struct Node *left;
struct Node *right;
};