Skip to content

Instantly share code, notes, and snippets.

@stephen-maina
stephen-maina / disc intersections
Created April 18, 2015 16:05
from research online
class Solution {
public int solution(int[] a) {
int result = 0;
int[] dps = new int[a.length];
int[] dpe = new int[a.length];
for (int i = 0; i < a.length; i++)
{
dps[Math.max(0, i - a[i])]++;
dpe[Math.min(a.length - 1, i + a[i])]++;
import java.util.stream.IntStream;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int[]sorted=IntStream.of(A).sorted().toArray();
for(int index=2;index<sorted.length;index++){
long ind1=(long)sorted[index];
long ind2=(long)sorted[index-1];
long ind3=(long)sorted[index-2];
if(ind3+ind2>ind1&&ind1+ind2>ind3&&ind3+ind1>ind2){
import java.util.stream.IntStream;
import java.util.Set;
import java.util.stream.Collectors;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
Set<Integer> result=IntStream.of(A).boxed().collect(Collectors.toSet());
return result.size();
}
}
@stephen-maina
stephen-maina / MaxProductOfThree lesson 4
Created April 14, 2015 08:25
MaxProductOfThree lesson 4
import java.util.stream.IntStream;
import java.lang.Math;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int [] sorted=IntStream.of(A).sorted().toArray();
return Math.max(sorted[0]*sorted[1]*sorted[sorted.length-1],sorted[sorted.length-1]*sorted[sorted.length-2]*sorted[sorted.length-3]);
}
}
import java.util.Arrays;
import java.util.HashMap;
class Solution {
public int[] solution(String S, int[] P, int[] Q) {
// write your code in Java SE 8
int []result=new int[P.length];
for(int index=0;index<P.length;index++){
String check= S.substring(P[index],Q[index]+1);
if(check.contains("A")){
result[index]=1;
class Solution {
public int solution(int A, int B, int K) {
// write your code in Java SE 8
int first=0;
int last=0;
if(A%K==0){
first=A/K;
}else{
first=(A-A%K+K)/K;
}
@stephen-maina
stephen-maina / Passing cars codility fast
Last active August 29, 2015 14:19
Passing cars codility fast
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int count=0;
int pair=0;
for(int index=0;index<A.length;index++){
if(A[index]==0){
count++;
}else{
pair+=count;
@stephen-maina
stephen-maina / slow Passing cars lesson3
Created April 13, 2015 16:36
Slow passing cars lesson 3
import java.util.BitSet;
import java.util.stream.IntStream;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
BitSet bs=new BitSet(A.length+1);
//IntStream.of(A).filter(p->p==1).forEach(i->bs.set(i));
for(int set=0;set<A.length;set++){
if(A[set]==1){
bs.set(set,true);
@stephen-maina
stephen-maina / MaxCounters Codility lesson 2
Created April 13, 2015 14:48
MaxCounters Codility...not fast enough though.
class Solution {
public int[] solution(int N, int[] A) {
// write your code in Java SE 8
int length=A.length;
int max=N+1;
int []result=new int[N];
int maxValue=0;
for(int index=0;index<length;index++){
@stephen-maina
stephen-maina / MissingInteger Codility Lesson 2
Created April 13, 2015 12:35
MissingInteger Codility Lesson 2
import java.util.stream.IntStream;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if(A.length==1){
return A[0]+1;
}
int[] sorted=IntStream.of(A).sorted().toArray();
int temp=sorted[0];
for(int index=1;index<sorted.length;index++){