Skip to content

Instantly share code, notes, and snippets.

View vbanurag's full-sized avatar
:octocat:

anurag sharma vbanurag

:octocat:
View GitHub Profile
//arr=000110010
let arr = [0,0,0,0,0,1,1,0,0,0,0,0,0,1];
//let arr = [0,0,0,0,0]
const getMaxZero = (arr) => {
if(arr && arr[0] == 0 && arr[arr.length-1] == 0) {
//first non one
public class Main {
public static void main(String[] args) {
//String[] nums = {"1", "2", "4", "4", "5"};
String[] nums = {"4", "4", "4"};
//String[] nums = {"-214748364801","-214748364802"};
int k = 2; //second largest element
long l = findKthLargest( getUniqArray(nums), k);
System.out.println(l);
let double = x => true
let triple = x => x ? 6 : 7
let quadruple = x => x * 4
let pipe = (...funs) => input => funs.reduce(
(total, fn) => {
console.log(fn, '-----',total)
return fn(total)
},
input
@vbanurag
vbanurag / LRUCache.js
Created October 20, 2020 07:19
LRU cache using JS
class Node {
constructor(key, value) {
this.key = key;
this.value = value;
this.next = null;
this.prev = null;
}
}
class LRU {
@vbanurag
vbanurag / Sort.js
Created September 10, 2020 02:50
Sort Array in linear time
/*
* Given an array of strictly the characters 'R', 'G', and 'B',
* segregate the values of the array so that all the Rs come first,
* the Gs come second, and the Bs come last.
* You can only swap elements of the array.
*
* Do this in linear time and in-place.
*
* For example, given the array ['G', 'B', 'R', 'R', 'B', 'R', 'G'],
* it should become ['R', 'R', 'R', 'G', 'G', 'B', 'B'].
@vbanurag
vbanurag / climb_stairs.js
Created September 7, 2020 08:52
climb stairs prob without memoziation [Leetcode problem]
/*
Case: 1
Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
Case: 2
@vbanurag
vbanurag / partition.js
Created September 3, 2020 03:23
Given a pivot x, and a list lst, partition the list into three parts. The first part contains all elements in lst that are less than x The second part contains all elements in lst that are equal to x The third part contains all elements in lst that are larger than x
//For example, given x = 10 and lst = [9, 12, 3, 5, 14, 10, 10]
let lst = [9, 12, 3, 5, 14, 10, 10]
const sanitizePivot = (lst, pivot) => {
let start = 0, end = lst.length -1
while(start<end) {
if(lst[start] == pivot) {
let _e = end;
@vbanurag
vbanurag / pascal.js
Last active August 24, 2020 01:13
Print Pascal Triangle
const printPascal= (noOfRows) => {
let coef = 1
let ret = ''
for (let i = 0; i< noOfRows; i++) {
for (let space = 1 ; space <= noOfRows-i; space++) {
ret += ' '
}
for (let j =0; j<= i; j++) {
if ( j ==0 || i == 1) {
func GetAllUsers(c *fiber.Ctx) {
var users []models.User
//Connection mongoDB with helper class
collection := helper.ConnectDB("users")
// bson.M{}, we passed empty filter. So we want to get all data.
cur, err := collection.Find(context.TODO(), bson.M{})
if err != nil {
package controllers
import (
"context"
"log"
"github.com/gofiber/fiber"
"github.com/vbanurag/go-fiber/helper"
"github.com/vbanurag/go-fiber/models"
"go.mongodb.org/mongo-driver/bson"