Skip to content

Instantly share code, notes, and snippets.

View harshitkgupta's full-sized avatar

Harshit Kumar Gupta harshitkgupta

View GitHub Profile
@harshitkgupta
harshitkgupta / Git.md
Created May 15, 2020 21:47
Info about Git Tool
@harshitkgupta
harshitkgupta / NonBlockingCounter.java
Created May 15, 2020 21:37
NonBlockingDataStructure
mport java.util.concurrent.atomic.AtomicLong;
public class mport java.util.concurrent.atomic.AtomicLong;
public class NonBlockingCounter{
private AtomicLong counter = new AtomicLong();
public long getValue(){
return counter.get();
}
public long increment(){
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 29 15:51:29 2017
@author: hgupta
"""
import sys
import hashlib
import json
public class SegmentTree {
private int tree[];
private int leafCount;
public SegmentTree(int[] array) {
this.leafCount = array.length;
int heightOfTree = (int) (Math.ceil(Math.log(leafCount)/Math.log(2)));
int size = 2*(int)Math.pow(heightOfTree,2) -1;
this.tree = new int[size];
constructSTUtil(array, 0, leafCount-1, 0);
}
@harshitkgupta
harshitkgupta / FastReader.java
Created June 8, 2017 14:14
For fast reading in competitive coding
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
@harshitkgupta
harshitkgupta / GRID.java
Created March 5, 2017 13:54
Used for finding connected components and max cells in one component in GRID
class GRID {
private static final int eightDirections[][] = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -1 }, { 0, 1 }, { 1, -1 },
{ 1, 0 }, { 1, 1 } };
private static final int fourDirections[][] = { { -1, 0 }, { 0, -1 }, { 0, 1 }, { 1, 0 } };
private int rows, columns;
private int grid[][];
private boolean visited[][];
@harshitkgupta
harshitkgupta / PrimeGenerator.java
Created February 19, 2017 10:14
Generate prime numbers up to 10^6 and use them to check for prime number up to 10^9
class PrimeGenerator{
static boolean isprime[];
static List<Integer> primeList;
static int N = 1000000;
static{
isprime = new boolean[N];
Arrays.fill(isprime, true);
isprime[0] = isprime[1] = false;
primeList = new ArrayList<Integer>();
}
@harshitkgupta
harshitkgupta / ArrayTrie.java
Created February 19, 2017 06:38
Trie implementation using Array
class ArrayTrie {
private static class TrieNode {
public static final int NUMBER_OF_CHARACTERS = 26;
TrieNode children [];
boolean isLeafNode;
int count;
public TrieNode() {
children = new TrieNode[NUMBER_OF_CHARACTERS];
@harshitkgupta
harshitkgupta / HashedTrie.java
Last active March 3, 2019 04:49
Trie data structure implemetation using HashMap
class HashedTrie {
private static class TrieNode {
private Map<Character, TrieNode> children;
boolean isLeafNode;
int count;
public TrieNode() {
children = new HashMap<Character, TrieNode>();
count = 0;