Skip to content

Instantly share code, notes, and snippets.

#include <iostream>
using namespace std;
int maxSubarrProd(int arr[], int size)
{
int maxProd = 0;
int currProd = 0;
int i;
for (i = 0; i < size; i++)
@kodebinary
kodebinary / house-thief-problem.cpp
Created May 30, 2020 10:46
House thief problem using recursion and dynamic programming approach, For full article: https://kodebinary.com/house-thief-problem-using-recursion-and-dynamic-programming
#include <iostream>
using namespace std;
/*
House thief using recursive approach.
either pick current house or pick next house.
maximize both results.
*/
int houseThiefRec(int inp[], int size, int curIdx)
@kodebinary
kodebinary / zero-one-knapsack-problem.cpp
Last active May 29, 2020 19:41
0/1 Knapsack problem, both recursion and dynamic programming approaches. Full article is at: https://kodebinary.com/0-1-knapsack-problem-example-with-recursion-and-dynamic-programming-solutions/
#include <iostream>
#include <vector>
using namespace std;
struct Item
{
int weight;
int cost;
Item()
#include <iostream>
#include <vector>
using namespace std;
/*
Activity selection (or)
Interval scheduling maximization problem.
*/
@kodebinary
kodebinary / coin-change-problem.cpp
Created May 27, 2020 15:36
Coin change problem (Given a set of coins and find all number of combinations to make given sum amount). Full article https://kodebinary.com/coin-change-problem-find-all-number-of-combinations-that-formed-a-sum-amount-with-given-denominations
#include <iostream>
using namespace std;
int findCombinations(int coins[], int n, int amount, int currentCoin)
{
if (amount == 0)
{
return 1;
}
@kodebinary
kodebinary / find-most-frequent-integer.cpp
Created May 26, 2020 09:08
Find most frequent integer in the given unsorted integer array. Full article can be found at: https://kodebinary.com/find-the-most-frequent-element-in-an-unsorted-array
#include <iostream>
#include <unordered_map>
using namespace std;
int findMaxOccurMap(int arr[], int n)
{
unordered_map<int, int> map;
for (int i = 0; i < n; i++)
{
@kodebinary
kodebinary / topological-sort.cpp
Created May 23, 2020 20:54
Topological sort of an acyclic directed graph. Full article is at https://kodebinary.com/topological-sort-of-an-acyclic-directed-graph
#include <iostream>
#include <list>
#include <stack>
using namespace std;
class Vertex
{
public:
Vertex(int val)
{
@kodebinary
kodebinary / edit-distance-recur-dp.cpp
Created May 22, 2020 21:47
Edit distance problem with Recursion and Dynamic Programming. Full article can find at: https://kodebinary.com/best-complexity-edit-distance-algorithm-with-an-example
#include <iostream>
using namespace std;
int min(int a, int b, int c)
{
int min = a;
if (min > b)
{
min = b;
@kodebinary
kodebinary / kth-largest-or-smallest-integer.cpp
Last active May 22, 2020 03:06
Find Kth largest or smallest integer in the given array of size 'N'. Full article at https://kodebinary.com/k-th-largest-or-smallest-element-in-an-unsorted-array-of-integers/
#include <iostream>
using namespace std;
int quickSortUtil(int inp[], int start, int end)
{
int elm = inp[end];
int result = start - 1;
for (int i = start; i < end; i++)
{
@kodebinary
kodebinary / maximum-subarray-sum.cpp
Created May 20, 2020 20:46
Maximum subarray sum of the given unsorted array of integers. Full article is in https://kodebinary.com/find-the-maximum-subarray-sum-in-an-unsorted-integer-array
#include <iostream>
using namespace std;
int maxSubArraySum(int arr[], int size)
{
int sum = 0;
int maxSum = INT_MIN;
int i;
for (i = 0; i < size; i++)