Skip to content

Instantly share code, notes, and snippets.

@dmnugent80
dmnugent80 / UniqueCount.java
Created January 11, 2015 21:06
Return count of unique elements in an array
public int getUniqueCount(int[] array){
ArrayList<Integer> arrUnique = new ArrayList<Integer>();
for (int i=0; i < array.length; i++){
if (!arrUnique.contains(array[i]){
arrUnique.add(array[i]);
}
}
return arrUnique.size();
}
@dmnugent80
dmnugent80 / SumLargestSubset.java
Last active August 29, 2015 14:13
Given a list of numbers, find the contiguous sublist that has the largest sum.
public int getSumLargestSubset(int[] array){
int sumSoFar = 0;
int largestSum = 0;
for (int i=0; i < array.length; i++){
sumSoFar = sumSoFar + array[i];
if (sumSoFar < 0){
sumSoFar = 0;
}
else if (sumSoFar > largestSum){
largestSum = sumSoFar;
@dmnugent80
dmnugent80 / RotatedBinarySearch.java
Last active August 29, 2015 14:13
Rotated Array Binary Search: Given a sorted list of integers, an arbitrary split is made such that the end of the first list is appended to the first, i.e.: 1 2 3 4 5 6 7 8 becomes: 6 7 8 1 2 3 4 5 Find the index of a number N in the array, returning -1 if it does not exist.
public int rotatedBinarySearch(int[] array, int n, int low, int high){
if (low > high){
return -1;
}
int mid = (low + high) / 2;
if (array[mid] == n){
return mid;
}
if (array[low] < array[mid]){
@dmnugent80
dmnugent80 / BinaryTreeReturnOrdered.java
Last active August 29, 2015 14:13
Traverse a binary there so that the order returned is ordered from smallest to greatest.
public class TreeNode{
public int data;
public TreeNode left;
public TreeNode right;
public TreeNode(int d){
this.data = d;
this.left = null;
this.right = null;
}
}
@dmnugent80
dmnugent80 / IsBalancedBST.java
Created January 12, 2015 07:38
Is BST balanced- heights of the two sub-trees of any node differs by no more than one
public class TreeNode{
int data;
TreeNode left;
TreeNode right;
public TreeNode(int d){
data = d;
left = null;
@dmnugent80
dmnugent80 / MergeIntervals.java
Last active August 29, 2015 14:13
Merge Intervals
/*
[(1,4), (-10,0), (2,6), (7,10)] --> [(-10,0), (1,6), (7,10)]
[(-1,2), (0,3), (10,20), (-5,5)] --> [(-5,5), (10,20)]
*/
import java.lang.Math;
import java.util.*;
@dmnugent80
dmnugent80 / DoLinkedListsIntersect.java
Created January 15, 2015 05:20
Write an algorithm to determine if 2 linked lists intersect
public class Node{
int data;
Node next;
public Node(int d){
this.data = d.
this.next = null;
}
}
public boolean doLinkedListsIntersect(Node root1, Node root2){
@dmnugent80
dmnugent80 / PrintPermutations.java
Last active August 29, 2015 14:13
Print Permutations
import java.util.*;
public class permutations
{
public static void main(String[] args)
{
String str = new String("abc");
StringBuffer output = new StringBuffer();
boolean used[] = {false, false, false};
@dmnugent80
dmnugent80 / StringCombinations.java
Created January 19, 2015 22:53
Combinations of a String
public class StringLib {
public static void combine(String str){
int length=str.length();
StringBuffer output=new StringBuffer();
combination(str,length,output,0);
}
@dmnugent80
dmnugent80 / SetsSumZero.java
Last active August 29, 2015 14:13
All sub-sets that sum to zero
/*
Print all subset of a given set which sums up to ZERO
{8,3,5,1,-4,-8}
so ans will be : {8,-8}
{3,5,-8}
{3,1,-4}
*/
import java.util.*;