Skip to content

Instantly share code, notes, and snippets.

View jordanterry's full-sized avatar

Jordan Terry jordanterry

View GitHub Profile
@jordanterry
jordanterry / solution.java
Last active August 29, 2015 14:15
First attempt at MaxProductOfThree task
import java.util.Arrays;
public class Solution {
public int solution(int[] A) {
Arrays.sort(A);
int l = A.length;
return A[l - 1] * A[l - 2] * A[l - 3];
}
}
@jordanterry
jordanterry / MaxProductOfThree
Created February 22, 2015 09:55
The question for the MaxProductOfThree task.
A non-empty zero-indexed array A consisting of N integers is given. The product of triplet (P, Q, R) equates to A[P] * A[Q] * A[R] (0 ≤ P < Q < R < N).
For example, array A such that:
A[0] = -3
A[1] = 1
A[2] = 2
A[3] = -2
A[4] = 5
A[5] = 6
contains the following example triplets:
(0, 1, 2), product is −3 * 1 * 2 = −6
@jordanterry
jordanterry / image_name.xml
Last active August 29, 2015 14:10
RelativeLayout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/description"
android:layout_below="@id/image"
public class IsPermutation {
public int solution(int[] A) {
// Create a boolean array that will be used to store whether a number has been visited yet.
boolean[] counter = new boolean[A.length];
// Loop throught each element in array A
for(int i = 0; i < A.length; i++) {
/* Does the number being visited fit between 1...N? Return 0 if it is beyond array bounds.
* e.g. [1, 2, 4] Clearly 3 is missing and this is not a permutation, this can be confirmed as
* 4 is grater than N, the length og the array. The order of the following IF statement is important;
* Not checking if a value is within bounds will cause an out of bounds error if it is.
A non-empty zero-indexed array A consisting of N integers is given.
A permutation is a sequence containing each element from 1 to N once, and only once.
For example, array A such that:
A[0] = 4
A[1] = 1
A[2] = 3
A[3] = 2
is a permutation, but array A such that:
A[0] = 4
A[1] = 1
public class LowestMissingInt {
public int solution(int[] A) {
// Create an array of booleans to represent if a number has been visited yet.
boolean[] counter = new boolean[A.length + 1];
// Loop through setting each value as false. It has not been visited yet.
for(int i = 0; i < counter.length; i++) counter[i] = false;
// Loop through each value in the array, if the value is less than the length of A and greater than 0
// It is a valid value, set it's value in counter, it is a 0 indexed array so subtract 1!
for(int a : A) if (a > 0 && a < counter.length) counter[a - 1] = true;
// Loop through the counter, if an element is false, return element index + 1 (it's a 0 index array!)
Write a function:
int solution(int A[], int N);
that, given a non-empty zero-indexed array A of N integers, returns the minimal positive integer that does not occur in A.
For example, given:
A[0] = 1
A[1] = 3
A[2] = 6
A[3] = 4
A[4] = 1
A[5] = 2
import java.util.Arrays;
/**
* Created by jordanterry on 26/10/14.
*/
public class PermMissingElem {
public int Solution(int[] A) {
// If it is empty, return one, each permutation must start with 1
if(A.length == 0) return 1;
A zero-indexed array A consisting of N different integers is given. The array contains integers in the range [1..(N + 1)], which means that exactly one element is missing.
Your goal is to find that missing element.
Write a function:
class Solution { public int solution(int[] A); }
that, given a zero-indexed array A, returns the value of the missing element.
For example, given array A such that:
A[0] = 2
A[1] = 3
A[2] = 1
A[3] = 5
A small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to a position greater than or equal to Y. The small frog always jumps a fixed distance, D.
Count the minimal number of jumps that the small frog must perform to reach its target.
Write a function:
class Solution { public int solution(int X, int Y, int D); }
that, given three integers X, Y and D, returns the minimal number of jumps from position X to a position equal to or greater than Y.
For example, given:
X = 10
Y = 85
D = 30
the function should return 3, because the frog will be positioned as follows: