If you see the below code, I reduced few of statements which you used and that will improve the code performance. I
I can see you have used multiple ToString
calls which will highly consumes theconsume memory.
We
We can use HashSet
instead theof List
to store the counted binary gaps which will avoid the duplicated counts.
for
loop isloops are better instead theof foreach
loop - for
loop works faster
I.
I believe this code block givesis more clear, easier to understand and more efficient.
This is working 100%, verified and test cases all passed
The solution in C#:
public static int FindGap(int N) {
var binaryValue = Convert.ToString(N, 2);
var L = binaryValue.Length;
var bLength = 0;
var bArray = new List<int>(); //new HashSet<int>()
for (var i = 0; i < L; i++) {
if (binaryValue[i] == '0') {
bLength++;
if ((L > i + 1) && binaryValue[i + 1] == '1'){ //Check the next index value is one
bArray.Add(bLength);
bLength = 0;
}
//Ex: 1000 (binary gaps is zero)
if (L - 1 == i) { //Check the last index value is zero
bLength = 0;
bArray.Add(bLength);
}
}
}
return bArray.Count > 0 ? bArray.Max() : 0;
}
```
If you see the below code, I reduced few of statements which you used and that will improve the code performance.
I can see you have used multiple ToString
which will highly consumes the memory.
We can use HashSet
instead the List
to store the counted binary gaps which will avoid the duplicated countsfor
loop is better instead the foreach
loop - for
loop works faster
I believe this code block gives clear understand and more efficient.
This is working 100%, verified and test cases all passed
The solution in C#:
public static int FindGap(int N) {
var binaryValue = Convert.ToString(N, 2);
var L = binaryValue.Length;
var bLength = 0;
var bArray = new List<int>(); //new HashSet<int>()
for (var i = 0; i < L; i++) {
if (binaryValue[i] == '0') {
bLength++;
if ((L > i + 1) && binaryValue[i + 1] == '1'){ //Check the next index value is one
bArray.Add(bLength);
bLength = 0;
}
//Ex: 1000 (binary gaps is zero)
if (L - 1 == i) { //Check the last index value is zero
bLength = 0;
bArray.Add(bLength);
}
}
}
return bArray.Count > 0 ? bArray.Max() : 0;
}
```
If you see the below code, I reduced few of statements which you used and that will improve the code performance.
I can see you have used multiple ToString
calls which will highly consume memory.
We can use HashSet
instead of List
to store the counted binary gaps which will avoid the duplicated counts.
for
loops are better instead of foreach
loop - for
loop works faster.
I believe this code block is more clear, easier to understand and more efficient.
This is working 100%, verified and test cases all passed
public static int FindGap(int N) {
var binaryValue = Convert.ToString(N, 2);
var L = binaryValue.Length;
var bLength = 0;
var bArray = new List<int>(); //new HashSet<int>()
for (var i = 0; i < L; i++) {
if (binaryValue[i] == '0') {
bLength++;
if ((L > i + 1) && binaryValue[i + 1] == '1'){ //Check the next index value is one
bArray.Add(bLength);
bLength = 0;
}
//Ex: 1000 (binary gaps is zero)
if (L - 1 == i) { //Check the last index value is zero
bLength = 0;
bArray.Add(bLength);
}
}
}
return bArray.Count > 0 ? bArray.Max() : 0;
}
If you see the below code, I reduced few of statements which you used and that will improve the code performance.
I can see you have used multiple ToString
which will highly consumes the memory.
We can use HashSet
instead the List
to store the counted binary gaps which will avoid the duplicated counts
for
loop is better instead the foreach
loop - for
loop works faster
I believe this code block gives clear understand and more efficient.
This is working 100%, verified and test cases all passed
The solution in C#:
public static int solutionFindGap(int N) {
var binaryVbinaryValue = Convert.ToString(N, 2);
var L = binaryVbinaryValue.Length;
var bLength = 0;
var bArray = new List<int>(); //new HashSet<int>()
for (var i = 0; i < L; i++) {
if (binaryV[i]binaryValue[i] == '0') {
bLength++;
if ((L > i + 1) && binaryV[ibinaryValue[i + 1] == '1'){ //Check the next index value is one
bArray.Add(bLength);
bLength = 0;
}
//Ex: 1000 (binary gaps is zero)
if (L - 1 == i && binaryV[i] == '0') { //Check the last index value is zero
bLength = 0;
bArray.Add(bLength);
}
}
}
return bArray.Count > 0 ? bArray.Max() : 0;
}
```
This is working 100%, verified and test cases all passed
The solution in C#:
public int solution(int N) {
var binaryV = Convert.ToString(N, 2);
var L = binaryV.Length;
var bLength = 0;
var bArray = new List<int>();
for (var i = 0; i < L; i++) {
if (binaryV[i] == '0') {
bLength++;
if ((L > i + 1) && binaryV[i + 1] == '1'){ //Check the next index value is one
bArray.Add(bLength);
bLength = 0;
}
if (L - 1 == i && binaryV[i] == '0') { //Check the last index value is zero
bLength = 0;
bArray.Add(bLength);
}
}
}
return bArray.Count > 0 ? bArray.Max() : 0;
}
```
If you see the below code, I reduced few of statements which you used and that will improve the code performance.
I can see you have used multiple ToString
which will highly consumes the memory.
We can use HashSet
instead the List
to store the counted binary gaps which will avoid the duplicated counts
for
loop is better instead the foreach
loop - for
loop works faster
I believe this code block gives clear understand and more efficient.
This is working 100%, verified and test cases all passed
The solution in C#:
public static int FindGap(int N) {
var binaryValue = Convert.ToString(N, 2);
var L = binaryValue.Length;
var bLength = 0;
var bArray = new List<int>(); //new HashSet<int>()
for (var i = 0; i < L; i++) {
if (binaryValue[i] == '0') {
bLength++;
if ((L > i + 1) && binaryValue[i + 1] == '1'){ //Check the next index value is one
bArray.Add(bLength);
bLength = 0;
}
//Ex: 1000 (binary gaps is zero)
if (L - 1 == i) { //Check the last index value is zero
bLength = 0;
bArray.Add(bLength);
}
}
}
return bArray.Count > 0 ? bArray.Max() : 0;
}
```
This is working 100%, verified and test cases all passed
The solution in C#:
public int solution(int N) {
var binaryV = Convert.ToString(N, 2);
var L = binaryV.Length;
var bLength = 0;
var bArray = new List<int>();
for (var i = 0; i < L; i++) {
if (binaryV[i] == '0') {
bLength++;
if ((L > i + 1) && binaryV[i + 1] == '1'){ //Check the next index value is one
bArray.Add(bLength);
bLength = 0;
}
if (L - 1 == i && binaryV[i] == '0') { //Check the last index value is zero
bLength = 0;
bArray.Add(bLength);
}
}
}
return bArray.Count > 0 ? bArray.Max() : 0;
}
```