Skip to content

Instantly share code, notes, and snippets.

View mekilis's full-sized avatar
🎯
Focusing

Smart Mekiliuwa mekilis

🎯
Focusing
  • Lagos
View GitHub Profile
@mekilis
mekilis / subset_sum_backtracking.java
Created March 25, 2020 09:59
This function generates the subsets of a set that sums up to K using the naive brute force approach but also backtracking when necessary
private static List<Integer> elements;
private static List<List<Integer>> subsetsWithSumK;
private static List<Integer> currentSubset;
private static int numberOfElements, methodCalls;
private static List<List<Integer>> subsetSum(int sumK, List<Integer> elementsArg) {
elements = elementsArg;
numberOfElements = elements.size();
subsetsWithSumK = new ArrayList<>();
@mekilis
mekilis / subset_sum_bruteforce.java
Created March 25, 2020 09:31
This function generates the subsets of a set that sums up to K using the naive brute force approach
private static List<Integer> elements;
private static List<List<Integer>> subsetsWithSumK;
private static List<Integer> currentSubset;
private static int numberOfElements, methodCalls;
private static List<List<Integer>> subsetSum(int sumK, List<Integer> elementsArg) {
elements = elementsArg;
numberOfElements = elements.size();
subsetsWithSumK = new ArrayList<>();
@mekilis
mekilis / subsets_recursive.java
Created March 21, 2020 17:49
This function generates and returns all subsets of a list using recursion
private static int numberOfElements;
private static List<List<Integer>> subsets;
private static List<Integer> currentSubset, elements;
private static List<List<Integer>> subsetRecursive(List<Integer> elementsArg) {
elements = elementsArg;
numberOfElements = elements.size();
subsets = new ArrayList<>();
currentSubset = new ArrayList<>();
@mekilis
mekilis / subsets_iterative.java
Last active March 21, 2020 17:52
This function generates and returns all subsets of a list using iteration
private static List<List<Integer>> subsetIterative(List<Integer> elements) {
System.out.println("Subsets of " + elements);
List<List<Integer>> subsets = new ArrayList<>();
List<Integer> subset;
String subsetBinary;
int numberOfElements = elements.size();
for (int i = 0; i < (1 << numberOfElements); i++) {
subsetBinary = convertToZeroPaddedBinaryString(i, numberOfElements);
@mekilis
mekilis / factorial_recursive.java
Last active March 21, 2020 16:25
The factorial function using recursion
private static long factorialTopDownRecursion(long n) {
// base case
if (n <= 1)
return 1;
return n * factorialTopDownRecursion(n-1);
}
private static long factorialBottomUpRecursion(long n) {
return _factorialBottomUpRecursion(1, n);
@mekilis
mekilis / factorial_iterative.java
Last active March 21, 2020 16:11
The factorial function using the iterative technique
private static long factorialForLoop(long n) {
long result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
@mekilis
mekilis / demostdinout2.go
Last active March 21, 2020 17:55
Demo StdIO in Go
package main
import (
"fmt"
"strings"
)
func main() {
// assuming user inputs 10, 5, 7
var i, j int