Skip to content

Instantly share code, notes, and snippets.

public class Solution {
public int[] solution(int[] A, int[] B) {
int n = A.length;
int[] ladder = new int[n + 1];
ladder[0] = ladder[1] = 1;
for (int i = 2; i <= n; i++) {
ladder[i] = ladder[i - 1] + ladder[i - 2];
}
for (int i = 0; i < n; i++) {
A[i] = ladder[A[i]] & ((1 << B[i]) - 1);
@stephen-maina
stephen-maina / Ladder
Created May 6, 2015 06:19
using binet's formula it fails for some tests
class Solution {
public int[] solution(int[] A, int[] B) {
// write your code in Java SE 8
int length=A.length;
int result[]=new int[length];
for(int index=0;index<length;index++){
//using Binet's formula
double num=A[index]+1;
double phi= (1+Math.sqrt(5))/2;
@stephen-maina
stephen-maina / Common prime divisors
Created May 6, 2015 05:04
did some online research
class Solution {
public int solution(int[] A, int[] B) {
// write your code in Java SE 8
int count=0;
for(int index=0;index<B.length;index++){
int gcd=0;
int extra=0;
if(A[index]==0||B[index]==0){
continue;
class Solution {
public int solution(int N, int M) {
// write your code in Java SE 8
return N/gcd(N,M);
}
public int gcd(int N,int M){
if(N%M==0){
return M;
}else{
return gcd(M,N%M);
@stephen-maina
stephen-maina / Chocolate by numbers
Created May 5, 2015 19:06
O(N+M) out of memory error
import java.util.stream.IntStream;
class Solution {
public int solution(int N, int M) {
// write your code in Java SE 8
int index=0;
int count=0;
int [] test=IntStream.rangeClosed(0,N-1).toArray();
while(test[index]!=-1){
test[index]=-1;
count++;
import java.util.stream.IntStream;
import java.util.stream.Collectors;
import java.util.List;
import java.util.Arrays;
class Solution {
public int[] solution(int[] A) {
// write your code in Java SE 8
int result[]=new int[A.length];
List<Integer> sorted=IntStream.of(A).sorted().boxed().collect(Collectors.toList());
import java.util.Set;
import java.util.HashSet;
class Solution {
public int[] solution(int N, int[] P, int[] Q) {
// write your code in Java SE 8
int result[]=new int [Q.length];
int semi[]=sieve(N);
for(int index=0;index<P.length;index++){
result[index]=semi[Q[index]]-semi[P[index]-1];
@stephen-maina
stephen-maina / CountSemiprimes
Created May 5, 2015 08:14
not yet fast enough , cut speed by half
import java.util.Set;
import java.util.HashSet;
class Solution {
public int[] solution(int N, int[] P, int[] Q) {
// write your code in Java SE 8
int result[]=new int [Q.length];
for(int index=0;index<P.length;index++){
result[index]=sieve(P[index],Q[index]);;
@stephen-maina
stephen-maina / flags
Created May 4, 2015 21:05
still not fully working 40%
import java.util.ArrayList;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if(A.length<=2){
return 0;
}
ArrayList<Integer>peak=getPeaksIndex(A);
if(peak.size()==0){
return 0;
@stephen-maina
stephen-maina / peaks
Created May 4, 2015 17:16
not working only works for sample
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if(A.length<=2){
return 0;
}
int []peak=getPeaks(A);
int blocks=0;
for(int blk=1;blk<A.length;blk++){
int index=1;