Skip to content

Instantly share code, notes, and snippets.

View beingmartinbmc's full-sized avatar
🏠
Working from home

Ankit Sharma beingmartinbmc

🏠
Working from home
  • Games24x7
  • Bengaluru
  • 15:01 (UTC -12:00)
View GitHub Profile
@beingmartinbmc
beingmartinbmc / KMP.java
Created May 17, 2019 06:10
Easy implementation of KMP algorithm
package strings;
public class KMP {
private static int[] buildLPS(String pattern){
int[] lps = new int[pattern.length()];
int j = 0;
int i = 1;
while(i < pattern.length()){
if(pattern.charAt(j) == pattern.charAt(i)){
@beingmartinbmc
beingmartinbmc / SegmentTree.java
Last active October 15, 2019 05:43
An efficient segment tree based on this explanation: https://www.youtube.com/watch?v=Oq2E2yGadnU&t=619s
public class SegmentTree {
private int[] tree;
private int n;
SegmentTree(int[] a) {
int n = a.length;
tree = new int[n << 1];
construct(a);
}
@beingmartinbmc
beingmartinbmc / RotateNumber.java
Last active June 17, 2019 13:15
Rotations of a number. An efficient program to print all rotations of a number (left and right)
import java.io.*;
class GFG{
private static int getDigits(int n){
return (int)(Math.log10(n)) + 1;
}
private static int getFirst(int n){
int digits = getDigits(n);
return n / ((int)Math.pow(10, digits-1));
}
@beingmartinbmc
beingmartinbmc / Factors.java
Created June 17, 2019 13:43
Efficient program to print all factors and prime factors of a number.
import java.util.*;
class GFG{
private static List<Integer> getFactors(int n){
List<Integer> list = new ArrayList<>();
list.add(1); list.add(n);
for(int i=2; i*i <= n; i++){
if(n % i == 0) list.add(i);
if(i*i != n) list.add(n / i);
}
@beingmartinbmc
beingmartinbmc / SuffixArray.py
Created September 22, 2019 07:14
O(nlogn) Suffix Array implementation
class SuffixArray:
def __init__(self, s):
self.sa = self.build_sa(s)
self.lcp = self.build_lcp(s, self.sa)
@staticmethod
def count_sort(k, sa, ra):
n = len(sa)
N = n + 256
nsa = [i for i in sa]
@beingmartinbmc
beingmartinbmc / FileUploader.java
Last active March 14, 2020 07:22
FileUploader for multipart files
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.Calendar;
@beingmartinbmc
beingmartinbmc / MinStack.java
Created June 24, 2020 10:44
Stack that gives you minimum in O(1) time
public class MinStack {
private static class Node {
private int data;
private int min;
private Node next;
Node(int data) {
this.data = data;
this.min = data;
this.next = null;