Skip to content

Instantly share code, notes, and snippets.

View arjunrao87's full-sized avatar
👋

Arjun Rao arjunrao87

👋
View GitHub Profile
@arjunrao87
arjunrao87 / ReverseString.java
Created December 22, 2014 03:25
Reverse a string
public String reverse( String s ){
char[] c = s.toCharArray();
int size = c.length;
for( int i = 0; i<size/2; i ++ ){
char temp = c[i];
c[i] = c[size - i - 1];
c[size - i - 1] = temp;;
}
return new String( c );
}
@arjunrao87
arjunrao87 / Fibonacci.java
Created December 22, 2014 04:02
Fibonacci sequence
public int recursiveFibonacci( int n ){
if ( n<0 ){
return -1;
}
if( n == 0 || n == 1 ){
return 1;
}
return recursiveFibonacci( n -1 ) + recursiveFibonnaci( n - 2 );
}
@arjunrao87
arjunrao87 / PrimeNumber.java
Created December 22, 2014 05:28
Find if a number n is prime
public boolean isPrime( int n ){
if( n<0 )
return false;
if( n>0 && n <= 3 ){
return true;
}
boolean isPrime = true;
for( int i = 2; i< n/2; i++ ){
if( n % i == 0 ){
isPrime = false;
@arjunrao87
arjunrao87 / LongestPalindrome.java
Created December 22, 2014 15:32
Find longest palindrome in string
//Method 1 : Time : O(n^3), Space : O(1)
public boolean findLongestPalindrome( String word ){
String longestWord = "";
for( int i = 0; i < word.length(); i++ ){
for( int j = 0; j<word.length(); j++ ){
if( i<= j ){
String currentWord = word.substring( i, j );
if( isPalindrome( currentWord ) && currentWord.length() > longestWord.length() ){
longestWord = currentWord;
}
@arjunrao87
arjunrao87 / GraphDFS.java
Last active August 29, 2015 14:12
Graph DFS
class Vertex{
private int id;
private List<Edge> adjacencies;
private boolean isVisited;
public Vertex( int id ){
this.id = id;
this.adjacencies = new ArrayList<Edge>();
}
@arjunrao87
arjunrao87 / GraphBFS.java
Created December 25, 2014 15:38
BFS for graph traversal
public class GraphBFS{
public void doBFS( Vertex source ){
Queue<Vertex> verticesToVisit = new LinkedList<Vertex>();
verticesToVisit.enqueue( source );
source.setVisited( true );
while( !verticesToVisit.isEmpty() ){
Vertex currentVertex = verticesToVisit.dequeue();
for( Edge edge : currentVertex.getAdjacencies() ) {
Vertex destinationVertex = edge.getDestinationVertex();
if( !destinationVertex.isVisited() ) {
@arjunrao87
arjunrao87 / gist:a97c181e88cd5bef48d4
Created December 25, 2014 23:45
Prims algorithm
public class PrimsAlgorithm{
public void findPrimsMST( Graph graph ){
//Step 1 : Initialize necessary arrays
int[] weights = new int[graph.getNumOfVertices()];
boolean[] visited = new boolean[graph.getNumOfVertices()];
int[] predecessors = new int[graph.getNumOfVertices()];
for( int i = 0; i<weights.length; i++ ){
weights[i] = Integer.MAX_VALUE;
@arjunrao87
arjunrao87 / BSTRange.java
Created December 26, 2014 13:12
Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Print all the keys of tree in range k1 to k2. i.e. print all x such that k1<=x<=k2 and x is a element of given BST.
public void findRange( Node node, int k1, int k2 ){
if( node == null || (k1>k2) ){
return;
}
int data = node.getData();
if( data >= k1 && data <= k2 ){
System.out.println(data);
}
if( k1 < data ){
findRange( node.getLeft(), k1, k2 );
@arjunrao87
arjunrao87 / LevelOrderBST.java
Created December 26, 2014 13:25
Level order traversal - BST
public void doLevelOrder( Node root ){
Queue<Node> nodes = new Queue<Node>();
nodes.queue( root );
while( !queue.isEmpty() ){
Node currentNode = queue.dequeue();
print( currentNode );
if( currentNode.getLeft != null ){
nodes.enqueue( currentNode.getLeft() );
}
@arjunrao87
arjunrao87 / SpiralOrderBST.java
Created December 26, 2014 13:46
Spiral traversal of binary tree
public void doSpiralOrder( Node root ){
Queue<Node> queue = new Queue<Node>();
Stack<Node> stack = new Stack<Node>();
stack.push( root );
boolean readStack = true;
while( (readStack && !stack.isEmpty()) || (!readStack && !queue.isEmpty()) ){
Node currentNode = null;
if( readStack ){
currentNode = stack.pop();