Skip to content

Instantly share code, notes, and snippets.

View tiagopereira17's full-sized avatar

Tiago Pereira tiagopereira17

View GitHub Profile
@tiagopereira17
tiagopereira17 / BinaryGap.java
Last active June 22, 2016 02:03
Find the longest sequence of zeros in binary representation of an integer.
public class BinaryGap {
public int solution(int N) {
int max = 0;
int counter = 0;
boolean isCounting = false;
while(N > 0) {
if((N & 1) == 1) {
if(isCounting) {
@tiagopereira17
tiagopereira17 / CyclicRotation.java
Last active June 22, 2016 00:09
Rotate the array A, K times. Each element of A will be shifted to the right by K indexes.
public class CyclicRotation {
public int[] rotate(int[] A, int K) {
if(A == null || A.length == 0) {
return new int[0];
}
int length = A.length;
if(K > length) {
@tiagopereira17
tiagopereira17 / OddOccurrencesInArray.java
Created June 22, 2016 00:42
A non-empty zero-indexed array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired.
public class OddOccurrencesInArray {
public int solution(int[] A) {
if(A == null || A.length == 0) {
return 0;
}
if(A.length == 1) {
return A[0];
}
@tiagopereira17
tiagopereira17 / FrogJump.java
Last active June 22, 2016 01:20
The frog is located at position X and wants to get to a position greater than or equal to Y. The frog always jumps a fixed distance, D. Count the minimal number of jumps that the frog must perform to reach its target.
public class FrogJump {
public int countJumps(int X, int Y, int D) {
if(X > Y) {
return 0;
}
return (int) Math.ceil((Y - X) / (double) D);
}
}
@tiagopereira17
tiagopereira17 / TapeEquilibrium.java
Created June 22, 2016 01:59
Minimize the value |(A[0] + ... + A[P-1]) - (A[P] + ... + A[N-1])|.
public class TapeEquilibrium {
public int solution(int[] A) {
int start = A[0];
int end = sum(A) - start;
int minDif = Math.abs(start - end);
for(int i = 1; i < A.length - 1; i++) {
start += A[i];
end -= A[i];
@tiagopereira17
tiagopereira17 / PermMissingElem.java
Last active July 19, 2023 12:43
Find the missing element in a given permutation.
public class PermMissingElem {
public int findMissingElement(int[] A) {
if(A == null || A.length == 0) {
return 1;
}
long N = A.length + 1;
long total = N * (N + 1) / 2;
@tiagopereira17
tiagopereira17 / FrogRiverOne.java
Created June 25, 2016 17:00
Find the earliest time when a frog can jump to the other side of a river.
public class FrogRiverOne {
public int solution(int X, int[] A) {
if(A == null || A.length == 0) {
return -1;
}
int[] passed = new int[X];
for(int i = 1; i <= passed.length; i++) {
passed[i-1] = -1;
}
@tiagopereira17
tiagopereira17 / PermCheck.java
Created June 25, 2016 17:37
Check whether array A is a permutation.
public class PermCheck {
public int checkIfIsPermutation(int[] A) {
if(A == null || A.length == 0) {
return -1;
}
int[] passed = new int[A.length];
for(int i = 0; i < passed.length; i++) {
passed[i] = -1;
}
@tiagopereira17
tiagopereira17 / MissingInteger.java
Created June 28, 2016 14:37
Find the minimal positive integer not occurring in a given sequence.
public class MissingInteger {
public int minMissingInteger(int[] A) {
if(A == null || A.length == 0) {
return -1;
}
int[] items = new int[A.length + 1];
for(int i = 0; i < items.length; i++) {
items[i] = -1;
}
@tiagopereira17
tiagopereira17 / PassingCars.java
Created July 4, 2016 18:25
Count the number of passing cars on the road.
public class PassingCars {
public int solution(int[] A) {
// 0 - east, 1 - west
int west = 0;
int passing = 0;
for(int i = A.length - 1; i >= 0; i--) {
if(A[i] == 0) {
passing += west;
if(passing > 1000000000) {