A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.
For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.
Write a function:
class Solution { public int solution(int N); }
that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.
For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5. Given N = 32 the function should return 0, because N has binary representation '100000' and thus no binary gaps.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..2,147,483,647].
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
//Stack<Integer> st = new Stack<Integer>();
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
//Stack<Integer> st = new Stack<Integer>();
fo
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
//Stack<Integer> st = new Stack<Integer>();
while(N/2==)
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
while(N/2==1){
st.push(N%2);
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer>
while(N/2==1){
st.push(N%2);
N=N/2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
for(int i=0; i<st.size(); i++){
list.add(st.peek());
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
for(int i=0; i<st.size(); i++){
list.add(st.peek());
} // 1
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
for(int i=0; i<st.size(); i++){
list.add(st.peek());
} // 1 0 0 1 0 0 0
int
for(int i=0; i<list.size(); i++){
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
for(int i=0; i<st.size(); i++){
list.add(st.peek());
} // 1 0 0 1 0 0 0
int bgLen = 0;
for(int i=0; i<list.size(); i++){
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
for(int i=0; i<st.size(); i++){
list.add(st.peek());
} // 1 0 0 1 0 0 0
int bgLen = 0;
int current = 0;
for(int i=0; i<list.size(); i++){
current = list.get(i);
if(current ==0){
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
for(int i=0; i<st.size(); i++){
list.add(st.peek());
} // 1 0 0 1 0 0 0
int bgLen = 0;
int current = 0;
for(int i=0; i<list.size(); i++){
current = list.get(i);
if(current ==0){
bgLen++;
}
else{
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
for(int i=0; i<st.size(); i++){
list.add(st.peek());
} // 1 0 0 1 0 0 0
int bgLen = 0;
int current = 0;
for(int i=0; i<list.size(); i++){
current = list.get(i);
if(current ==0){
bgLen++;
}
else{
//๋ฆฌ์คํธ์ ๋ด๊ธฐ
bgLen = 0;
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ใด
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
for(int i=0; i<st.size(); i++){
list.add(st.peek());
} // 1 0 0 1 0 0 0
int bgLen = 0;
int current = 0;
for(int i=0; i<list.size(); i++){
current = list.get(i);
if(current ==0){
bgLen++;
}
else{
//๋ฆฌ์คํธ์ ๋ด๊ธฐ
bgLen = 0;
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
String binary = 0;
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
for(int i=0; i<st.size(); i++){
list.add(st.peek());
} // 1 0 0 1 0 0 0
int bgLen = 0;
int current = 0;
for(int i=0; i<list.size(); i++){
current = list.get(i);
if(current ==0){
bgLen++;
}
else{
//๋ฆฌ์คํธ์ ๋ด๊ธฐ
bgLen = 0;
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
String binary = 0;
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
for(int i=0; i<st.size(); i++){
binary = binary + st.peek();
} // 1 0 0 1 0 0 0
int bgLen = 0;
int current = 0;
for(int i=0; i<list.size(); i++){
current = list.get(i);
if(current ==0){
bgLen++;
}
else{
//๋ฆฌ์คํธ์ ๋ด๊ธฐ
bgLen = 0;
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
String binary = 0;
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
for(int i=0; i<st.size(); i++){
binary = binary + st.peek();
} // 1 0 0 1 0 0 0
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = list.get(i);
if(current ==0){
bgLen++;
}
else{
//๋ฆฌ์คํธ์ ๋ด๊ธฐ
bgLen = 0;
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = list.get(i);
if(current ==0){
bgLen++;
}
else{
//๋ฆฌ์คํธ์ ๋ด๊ธฐ
bgLen = 0;
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
if(current ==0){
bgLen++;
}
else{
//๋ฆฌ์คํธ์ ๋ด๊ธฐ
bgLen = 0;
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
return Math.max(list);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
return Math.max(list);
}
}
Solution.java:30: error: no suitable method found for max(ArrayList<Integer>) return Math.max(list); ^ method Math.max(int,int) is not applicable (actual and formal argument lists differ in length) method Math.max(long,long) is not applicable (actual and formal argument lists differ in length) method Math.max(float,float) is not applicable (actual and formal argument lists differ in length) method Math.max(double,double) is not applicable (actual and formal argument lists differ in length) 1 error
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collection.sort(list);
return list.get();
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collection.sort(list);
return list.get(list.size()-1);
}
}
Solution.java:30: error: cannot find symbol Collection.sort(list); ^ symbol: method sort(ArrayList<Integer>) location: interface Collection 1 error
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collections.sort(list);
return list.get(list.size()-1);
}
}
example test n=1041=10000010001_2
tested program terminated with exit code 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.ArrayList.elementData(ArrayList.java:424) at java.util.ArrayList.get(ArrayList.java:437) at Solution.solution(Solution.java:31) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.ArrayList.elementData(ArrayList.java:424) at java.util.ArrayList.get(ArrayList.java:437) at Solution.solution(Solution.java:31) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.ArrayList.elementData(ArrayList.java:424) at java.util.ArrayList.get(ArrayList.java:437) at Solution.solution(Solution.java:31) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
S
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collections.sort(list);
return list.get(list.size()-1);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
System.out.println(st);
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collections.sort(list);
return list.get(list.size()-1);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
System.out.println(st);
/*
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collections.sort(list);
*/
return 0;
//return list.get(list.size()-1);
}
}
[]
[]
[]
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
/*
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collections.sort(list);
*/
return 0;
//return list.get(list.size()-1);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
System.out.println(st.peek());
N=N/2;
}
/*
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collections.sort(list);
*/
return 0;
//return list.get(list.size()-1);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collections.sort(list);
*/
return 0;
//return list.get(list.size()-1);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collections.sort(list);
return 0;
return list.get(list.size()-1);
}
}
Solution.java:34: error: unreachable statement return list.get(list.size()-1); ^ 1 error
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collections.sort(list);
return list.get(list.size()-1);
}
}
example test n=1041=10000010001_2
tested program terminated with exit code 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.ArrayList.elementData(ArrayList.java:424) at java.util.ArrayList.get(ArrayList.java:437) at Solution.solution(Solution.java:33) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.ArrayList.elementData(ArrayList.java:424) at java.util.ArrayList.get(ArrayList.java:437) at Solution.solution(Solution.java:33) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.ArrayList.elementData(ArrayList.java:424) at java.util.ArrayList.get(ArrayList.java:437) at Solution.solution(Solution.java:33) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collections.sort(list);
return list.;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N/2==1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collections.sort(list);
return list.size();
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N==1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collections.sort(list);
return list.size();
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N==1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collections.sort(list);
return list.size();
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N==1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
st.
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collections.sort(list);
return list.size();
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N==1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
st.pop()
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collections.sort(list);
return list.size();
}
}
Solution.java:23: error: ';' expected st.pop() ^ 1 error
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N==1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
st.pop();
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collections.sort(list);
return list.size();
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N==1){
list.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
st.pop();
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collections.sort(list);
return list.size();
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
ArrayList<Integer> list = new ArrayList<Integer>();
while(N==1){
list.add(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<st.size(); i++){
current = st.peek();
st.pop();
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collections.sort(list);
return list.size();
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
ArrayList<Integer> list = new ArrayList<Integer>();
while(N==1){
list.add(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<list.size(); i++){
current = st.peek();
st.pop();
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collections.sort(list);
return list.size();
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
ArrayList<Integer> list = new ArrayList<Integer>();
while(N==1){
list.add(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<list.size(); i++){
current = list.get(i);
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collections.sort(list);
return list.size();
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();
while(N==1){
list.add(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<list.size(); i++){
current = list.get(i);
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collections.sort(list);
return list.size();
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N==1){
list.add(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<list.size(); i++){
current = list.get(i);
if(current ==0){
bgLen++;
}
else{
list.add(bgLen);
bgLen = 0;
}
}
Collections.sort(list);
return list.size();
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N==1){
list.add(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<list.size(); i++){
current = list.get(i);
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist);
return list.size();
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N==1){
list.add(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<list.size(); i++){
current = list.get(i);
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist);
return bnlist.get(bnlist.size()-1);
}
}
example test n=1041=10000010001_2
tested program terminated with exit code 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.ArrayList.elementData(ArrayList.java:424) at java.util.ArrayList.get(ArrayList.java:437) at Solution.solution(Solution.java:33) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.ArrayList.elementData(ArrayList.java:424) at java.util.ArrayList.get(ArrayList.java:437) at Solution.solution(Solution.java:33) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.ArrayList.elementData(ArrayList.java:424) at java.util.ArrayList.get(ArrayList.java:437) at Solution.solution(Solution.java:33) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N==1){
list.add(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<list.size(); i++){
current = list.get(i);
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist);
return bnlist.get(bnlist.size()-1);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N==1){
list.add(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<list.size(); i++){
current = list.get(i);
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.get(0);
}
}
example test n=1041=10000010001_2
tested program terminated with exit code 1
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:659) at java.util.ArrayList.get(ArrayList.java:435) at Solution.solution(Solution.java:33) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:659) at java.util.ArrayList.get(ArrayList.java:435) at Solution.solution(Solution.java:33) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:659) at java.util.ArrayList.get(ArrayList.java:435) at Solution.solution(Solution.java:33) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N==1){
list.add(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<list.size(); i++){
current = list.get(i);
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.get(0);
}
}
example test n=1041=10000010001_2
tested program terminated with exit code 1
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:659) at java.util.ArrayList.get(ArrayList.java:435) at Solution.solution(Solution.java:33) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:659) at java.util.ArrayList.get(ArrayList.java:435) at Solution.solution(Solution.java:33) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:659) at java.util.ArrayList.get(ArrayList.java:435) at Solution.solution(Solution.java:33) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N==1){
list.add(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<list.size(); i++){
current = list.get(i);
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.get(0);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N=1){
list.add(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<list.size(); i++){
current = list.get(i);
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.get(0);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N>1){
list.add(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<list.size(); i++){
current = list.get(i);
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.get(0);
}
}
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:659) at java.util.ArrayList.get(ArrayList.java:435) at Solution.solution(Solution.java:32) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
S<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N>1){
list.add(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<list.size(); i++){
current = list.get(i);
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.get(0);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N>1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
for(int i=0; i<list.size(); i++){
current = list.get(i);
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.get(0);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N>1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
while(st.size()==0){
current = list.get(i);
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.get(0);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N>1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
while(st.size()==0){
current = st.peek(i);
st.pop();
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.get(0);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N>1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
while(st.size()==0){
current = st.peek(i);
st.pop();
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.get(0);
}
}
Solution.java:21: error: cannot find symbol current = st.peek(i); ^ symbol: variable i location: class Solution 1 error
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N>1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
while(st.size()==0){
current = st.peek();
st.pop();
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.get(0);
}
}
example test n=1041=10000010001_2
tested program terminated with exit code 1
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:659) at java.util.ArrayList.get(ArrayList.java:435) at Solution.solution(Solution.java:33) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:659) at java.util.ArrayList.get(ArrayList.java:435) at Solution.solution(Solution.java:33) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:659) at java.util.ArrayList.get(ArrayList.java:435) at Solution.solution(Solution.java:33) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N>=1){
= st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
while(st.size()==0){
current = st.peek();
st.pop();
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.get(0);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N>=1){
= st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
while(st.size()==0){
current = st.peek();
st.pop();
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.get(0);
}
}
Solution.java:13: error: illegal start of expression = st.push(N%2); ^ 1 error
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N>=1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
while(st.size()==0){
current = st.peek();
st.pop();
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.get(0);
}
}
example test n=1041=10000010001_2
tested program terminated with exit code 1
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:659) at java.util.ArrayList.get(ArrayList.java:435) at Solution.solution(Solution.java:33) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:659) at java.util.ArrayList.get(ArrayList.java:435) at Solution.solution(Solution.java:33) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:659) at java.util.ArrayList.get(ArrayList.java:435) at Solution.solution(Solution.java:33) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N>=1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
while(st.size()==0){
current = st.peek();
st.pop();
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.get(0);
}
}
example test n=1041=10000010001_2
tested program terminated with exit code 1
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:659) at java.util.ArrayList.get(ArrayList.java:435) at Solution.solution(Solution.java:33) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:659) at java.util.ArrayList.get(ArrayList.java:435) at Solution.solution(Solution.java:33) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:659) at java.util.ArrayList.get(ArrayList.java:435) at Solution.solution(Solution.java:33) at Exec.run(exec.java:44) at Exec.main(exec.java:33)
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N>=1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
while(st.size()==0){
current = st.peek();
st.pop();
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.s;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N>=1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
while(st.size()==0){
current = st.peek();
st.pop();
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.size();
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N>=1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
while(st.size()0){
current = st.peek();
st.pop();
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.size();
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N>=1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
while(st.size()<>0){
current = st.peek();
st.pop();
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.size();
}
}
Solution.java:20: error: illegal start of expression while(st.size()<>0){ ^ 1 error
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N>=1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
while(st.size()!=0){
current = st.peek();
st.pop();
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.size();
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N>=1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
while(st.size()!=0){
current = st.peek();
st.pop();
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.get(0);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N>=1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
while(st.size()!=0){
current = st.peek();
st.pop();
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.get(0);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.*;
class Solution {
public int solution(int N) {
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> bnlist = new ArrayList<Integer>();
while(N>=1){
st.push(N%2);
N=N/2;
}
int bgLen = 0;
int current = 0;
while(st.size()!=0){
current = st.peek();
st.pop();
if(current ==0){
bgLen++;
}
else{
bnlist.add(bgLen);
bgLen = 0;
}
}
Collections.sort(bnlist, Collections.reverseOrder());
return bnlist.get(0);
}
}
The solution obtained perfect score.