Skip to content

Instantly share code, notes, and snippets.

@gabhi
gabhi / gist:477190073fbe4b37f1f6
Created June 17, 2015 07:00
Pythagorean Triplet in an array
We can solve this in O(n2) time by sorting the array first.
1) Do square of every element in input array. This step takes O(n) time.
2) Sort the squared array in increasing order. This step takes O(nLogn) time.
3) To find a triplet (a, b, c) such that a = b + c, do following.
Fix ‘a’ as last element of sorted array.
Now search for pair (b, c) in subarray between first element and ‘a’. A pair (b, c) with given sum can be found in O(n) time using meet in middle algorithm discussed in method 1 of this post.
@gabhi
gabhi / bogglesolver.java
Last active October 19, 2020 00:53
boggle solver java
package com.interview.graph;
import java.util.HashSet;
import java.util.Set;
/**
* http://www.careercup.com/question?id=14942063
*/
public class Boggle {
package com.interview.algorithms.tree;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
var fs = require('fs');
var csv = require("fast-csv");
var stream = fs.createReadStream("data.csv");
var csvStream = csv()
.on("data", function(data){
console.log(data);
})
.on("end", function(){
console.log("done");
@gabhi
gabhi / gist:7a331fa8e0e0955c4372
Created May 28, 2015 21:40
node js - how to switch between io js and node js
nvm ls
nvm install 0.10.13
nvm use 0.10.13
pio template get whhone/template-sentiment-analysis senti
pio app new MyApp1
@gabhi
gabhi / gist:026a8c9a8332459de2fb
Created April 26, 2015 01:28
javascript singleton
var Singleton = (function () {
var instance;
function createInstance() {
var object = new Object("I am the instance");
return object;
}
return {
getInstance: function () {
var pubnub = require("pubnub").init({ publish_key: "demo", subscribe_key: "demo" });
var tessel = require("tessel");
pubnub.subscribe({ channel: "tessel-light", message: function(m) {
tessel.led[1].toggle();
}});
var seriesData = [ [{ x: 0, y: 40 }, { x: 1, y: 49 }, { x: 2, y: 17 }, { x: 3, y: 42 }] ];
var graph = new Rickshaw.Graph( {
element: document.getElementById("chart"),
width: 940,
height: 250,
renderer: 'area',
stroke: true,
series: [
{
color: 'steelblue',
https://code.google.com/p/elements-of-programming-interviews/wiki/Programs