Skip to content

Instantly share code, notes, and snippets.

@stephen-maina
Created May 5, 2015 19:06
Show Gist options
  • Save stephen-maina/951a339c964469e7894b to your computer and use it in GitHub Desktop.
Save stephen-maina/951a339c964469e7894b to your computer and use it in GitHub Desktop.
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++;
index=(index+M)%N;
if(index>N-1){
index=index-N-2;
}
}
return count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment