Two positive integers N and M are given. Integer N represents the number of chocolates arranged in a circle, numbered from 0 to N − 1.
You start to eat the chocolates. After eating a chocolate you leave only a wrapper.
You begin with eating chocolate number 0. Then you omit the next M − 1 chocolates or wrappers on the circle, and eat the following one.
More precisely, if you ate chocolate number X, then you will next eat the chocolate with number (X + M) modulo N (remainder of division).
You stop eating when you encounter an empty wrapper.
For example, given integers N = 10 and M = 4. You will eat the following chocolates: 0, 4, 8, 2, 6.
The goal is to count the number of chocolates that you will eat, following the above rules.
Write a function:
class Solution { public int solution(int N, int M); }
that, given two positive integers N and M, returns the number of chocolates that you will eat.
For example, given integers N = 10 and M = 4. the function should return 5, as explained above.
Write an efficient algorithm for the following assumptions:
- N and M are integers within the range [1..1,000,000,000].
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M) {
// write your code in Java SE 8
int answer =0;
HashSet<Integer , Integer> som = new HashSet<>();
boolean check = true;
int x =0;
while(check)
{
int m = (x+ N) % M ;
x = m;
if(som.containsKey(x))
check = false;
else
{
som.put(x,true);
answer++;
}
}
return answer;
}
}
Solution.java:12: error: cannot find symbol HashSet<Integer , Integer> som = new HashSet<>(); ^ symbol: class HashSet location: class Solution Solution.java:12: error: cannot find symbol HashSet<Integer , Integer> som = new HashSet<>(); ^ symbol: class HashSet location: class Solution 2 errors
// you can also use imports, for example:
import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M) {
// write your code in Java SE 8
int answer =0;
HashSet<Integer , Integer> som = new HashSet<>();
boolean check = true;
int x =0;
while(check)
{
int m = (x+ N) % M ;
x = m;
if(som.containsKey(x))
check = false;
else
{
som.put(x,true);
answer++;
}
}
return answer;
}
}
Solution.java:12: error: wrong number of type arguments; required 1 HashSet<Integer , Integer> som = new HashSet<>(); ^ 1 error
// you can also use imports, for example:
import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M) {
// write your code in Java SE 8
int answer =0;
HashMap<Integer , Integer> som = new HashMap<>();
boolean check = true;
int x =0;
while(check)
{
int m = (x+ N) % M ;
x = m;
if(som.containsKey(x))
check = false;
else
{
som.put(x,true);
answer++;
}
}
return answer;
}
}
Solution.java:23: error: no suitable method found for put(int,boolean) som.put(x,true); ^ method Map.put(Integer,Integer) is not applicable (argument mismatch; boolean cannot be converted to Integer) method AbstractMap.put(Integer,Integer) is not applicable (argument mismatch; boolean cannot be converted to Integer) method HashMap.put(Integer,Integer) is not applicable (argument mismatch; boolean cannot be converted to Integer) 1 error
// you can also use imports, for example:
import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M) {
// write your code in Java SE 8
int answer =0;
HashMap<Integer , Integer> som = new HashMap<>();
boolean check = true;
int x =0;
while(check)
{
int m = (x+ N) % M ;
x = m;
if(som.containsKey(x))
check = false;
else
{
som.put(x,0);
answer++;
}
}
return answer;
}
}
// you can also use imports, for example:
import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M) {
// write your code in Java SE 8
int answer =0;
HashMap<Integer , Integer> som = new HashMap<>();
boolean check = true;
int x =0;
while(check)
{
if(x!=0)
int m = (x+ N) % M ;
x = m;
if(som.containsKey(x))
check = false;
else
{
som.put(x,0);
answer++;
}
}
return answer;
}
}
Solution.java:18: error: variable declaration not allowed here int m = (x+ N) % M ; ^ 1 error
// you can also use imports, for example:
import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M) {
// write your code in Java SE 8
int answer =0;
HashMap<Integer , Integer> som = new HashMap<>();
boolean check = true;
int x =0;
int m =0;
while(check)
{
if(x!=0)
m = (x+ N) % M ;
x = m;
if(som.containsKey(x))
check = false;
else
{
som.put(x,0);
answer++;
}
}
return answer;
}
}
// you can also use imports, for example:
import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M) {
// write your code in Java SE 8
int answer =0;
HashMap<Integer , Integer> som = new HashMap<>();
boolean check = true;
int x =0;
int m =0;
while(check)
{
if(x!=0)
m = (x+ M) % N ;
x = m;
if(som.containsKey(x))
check = false;
else
{
som.put(x,0);
answer++;
}
}
return answer;
}
}
// you can also use imports, for example:
import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M) {
// write your code in Java SE 8
int answer =1;
HashMap<Integer , Integer> som = new HashMap<>();
boolean check = true;
int x =0;
som.put(0,0);
while(check)
{
m = (x+ M) % N ;
x = m;
if(som.containsKey(x))
check = false;
else
{
som.put(x,0);
answer++;
}
}
return answer;
}
}
Solution.java:19: error: cannot find symbol m = (x+ M) % N ; ^ symbol: variable m location: class Solution Solution.java:20: error: cannot find symbol x = m; ^ symbol: variable m location: class Solution 2 errors
// you can also use imports, for example:
import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M) {
// write your code in Java SE 8
int answer =1;
HashMap<Integer , Integer> som = new HashMap<>();
boolean check = true;
int x =0;
som.put(0,0);
while(check)
{
int m = (x+ M) % N ;
x = m;
if(som.containsKey(x))
check = false;
else
{
som.put(x,0);
answer++;
}
}
return answer;
}
}
// you can also use imports, for example:
import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M) {
// write your code in Java SE 8
int answer =1;
HashMap<Integer , Integer> som = new HashMap<>();
boolean check = true;
int x =0;
som.put(0,0);
while(check)
{
int m = (x+ M) % N ;
x = m;
if(som.containsKey(x))
check = false;
else
{
som.put(x,0);
answer++;
}
}
return answer;
}
}
// you can also use imports, for example:
import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N, int M) {
// write your code in Java SE 8
int answer =1;
HashMap<Integer , Integer> som = new HashMap<>();
boolean check = true;
int x =0;
som.put(0,0);
while(check)
{
int m = (x+ M) % N ;
x = m;
if(som.containsKey(x))
check = false;
else
{
som.put(x,0);
answer++;
}
}
return answer;
}
}
The following issues have been detected: timeout errors.
maximal and minimal values
running time: >9.00 sec., time limit: 3.83 sec.
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.HashMap.resize(HashMap.java:703) at java.util.HashMap.putVal(HashMap.java:662) at java.util.HashMap.put(HashMap.java:611) at Solution.solution(Solution.java:25) at wrapper.run(wrapper.java:29) at wrapper.main(wrapper.java:22)