Counting Triangles

	class Sides {
		int a;
		int b;
		int c;

		Sides(int a, int b, int c) {
			this.a = a;
			this.b = b;
			this.c = c;
		}

	}

	public int compare(Sides o1, Sides o2) {
		int[] o1Sides = new int[] { o1.a, o1.b, o1.c };
		Arrays.sort(o1Sides);
		int[] o2Sides = new int[] { o2.a, o2.b, o2.c };
		Arrays.sort(o2Sides);
		for (int i = 0; i < o1Sides.length; i++)
			if (o1Sides[i] != o2Sides[i])
				return -1;

		return 0;
	}

	public int countDistinctTriangles(ArrayList<Sides> arr) {
		if (arr.size() == 0)
			return 0;
		Collections.sort(arr, new Comparator<Sides>() {

			@Override
			public int compare(Sides o1, Sides o2) {
				int[] o1Sides = new int[] { o1.a, o1.b, o1.c };
				Arrays.sort(o1Sides);
				int[] o2Sides = new int[] { o2.a, o2.b, o2.c };
				Arrays.sort(o2Sides);
				for (int i = 0; i < o1Sides.length; i++)
					if (o1Sides[i] != o2Sides[i])
						return -1;

				return 0;
			}
		});
		int count = 1;
		for (int i = 1; i < arr.size(); i++) {
			if (compare(arr.get(i), arr.get(i - 1)) != 0)
				count++;
		}
		return count;
	}

Number of Visible Nodes

	  class Node {
		    int data; 
		    Node left; 
		    Node right; 
		    Node() {
		      this.data = 0; 
		      this.left = null; 
		      this.right = null; 
		    }
		    Node(int data) {
		      this.data = data; 
		      this.left = null; 
		      this.right = null; 
		    }
		  }
		  
	
	int visibleNodes(Node root) {
       Queue<Node> q = new LinkedList<Node>();
       Queue<Integer> level = new LinkedList<Integer>();
       q.add(root);
       level.add(1);
       int count = 0;
       while(!q.isEmpty()) {
    	   Node n = q.poll();
    	    count = level.poll();
    	   if(n.right != null ) {
    		   q.add(n.right);
    		   level.add(count+1);
    	   }
    	   if(n.left != null ) {
    		   q.add(n.left);
    		   level.add(count+1);
    	   }
       }
       return count;
	}

Minimum Length Substrings

	public static int minLengthSubstring(String s, String t) {
		int min = Integer.MAX_VALUE;
		boolean found = false;
		for (int i = 0; i <= s.length(); i++) {
			for (int j = i; j <= s.length(); j++) {
				String takedString = s.substring(i, j);
				boolean contained = true;
				HashMap<Character, Integer> h = new HashMap<Character, Integer>();
				for (Character c : t.toCharArray()) {
					h.put(c, h.getOrDefault(c, 0) + 1);
				}
				for (Character c : takedString.toCharArray()) {
					h.put(c, h.getOrDefault(c, 0) - 1);
				}

				for (Character c : h.keySet()) {
					if (h.get(c) > 0)
						contained = false;
				}
				if (contained) {
					found = true;
					min = Integer.min(min, takedString.length());
				}
			}
		}
		if (!found)
			return -1;
		return min;
	}

Revenue Milestones

	public static int[] getMilestoneDays(int[] revenues, int[] milestones) {
		int[] output = new int[milestones.length];
		Arrays.fill(output, -1);
		int sum = 0;
		for (int i = 0; i < revenues.length; i++) {
			revenues[i] += sum;
			sum = revenues[i];
		}
		for (int i = 0; i < milestones.length; i++) {
			int searchFor = milestones[i];
			int low = 0;
			int high = revenues.length - 1;
			int mid = 0;

			while (low <= high) {
				mid = (high + low) / 2;

				if (searchFor == revenues[mid]) {
					output[i] = mid;
					break;
				} else if (searchFor < revenues[mid] && (mid - 1 < revenues.length && searchFor > revenues[mid - 1])) {
					output[i] = mid - 1;
					break;
				} else if (searchFor < revenues[mid])
					high = mid - 1;
				else if (searchFor > revenues[mid] && (mid + 1 < revenues.length && searchFor < revenues[mid + 1])) {
					output[i] = mid + 1;
					break;
				} else if (searchFor > revenues[mid])
					low = mid + 1;

			}
		}
		for (int i = 0; i < output.length; i++)
			output[i]++;
		return output;
	}

Queue Removals

	public static class Pair {
		int index;
		int value;

		public Pair(int index, int value) {
			super();
			this.index = index;
			this.value = value;
		}

		public int getIndex() {
			return index;
		}

		public void setIndex(int index) {
			this.index = index;
		}

		public int getValue() {
			return value;
		}

		public void setValue(int value) {
			this.value = value;
		}

	}

	public static int[] findPositions(int[] arr, int x) {
		Queue<Pair> q = new LinkedList<Pair>();
		for (int i = 0; i < arr.length; i++)
			q.add(new Pair(i + 1, arr[i]));
		ArrayList<Integer> removed = new ArrayList<Integer>();
		for (int i = x; i >= 1; i--) {
			int pops = x;
			PriorityQueue<Pair> maxHeap = new PriorityQueue<Pair>(new Comparator<Pair>() {
				@Override
				public int compare(Pair o1, Pair o2) {
					if (o1.getValue() == o2.getValue())
						return Integer.compare(o1.getIndex(), o2.getIndex());
					return -Integer.compare(o1.getValue(), o2.getValue());
				}
			});
			Queue<Pair> temp = new LinkedList<Pair>();
			while (pops != 0 && !q.isEmpty()) {
				Pair c = q.poll();
				maxHeap.add(c);
				temp.add(c);
				pops--;
			}
			Pair ToRemove = maxHeap.remove();
			int ToRemoveIndex = ToRemove.getIndex();
			removed.add(ToRemoveIndex);
			while (!temp.isEmpty()) {
				Pair a = temp.poll();
				if (a.getIndex() != ToRemoveIndex) {
					q.add((a.getValue() > 0) ? new Pair(a.getIndex(), a.getValue() - 1) : a);
				}
			}
		}
		int[] out = new int[removed.size()];
		for (int i = 0; i < out.length; i++)
			out[i] = removed.get(i);
		return out;
	}