Bulbs and Wires || September Cook-Off || Java language

Hi Guys,

I’ve been trying to solve the Bulbs and Wires problem from the September Cook-Off challenge. I was initially getting Run Time Error (NZEC), after trying exception handling I’m not getting the “wrong answer” error please help.

Code link:

Line: Logic I used:
10-25 sum() which returns the sum of elements of a list.
27-35 count() which counts no of chars in the string
48-62 loops through the given string and counts no. of
0’s that are in sequence.
63-66 checks for ‘10’ at the end, as this is a corner case
not covered by the above mentioned for loop
67-78 adds elements to cut2 (a list containing 0’s that
require 2 cuts) and cut1(a list containing 0’s that
require 1 cut)
80-83 if k = 0 prints 0
84-105 if k=1 removes the max element from cut1 list
the sum of cut2 and remaining element in cut1
106-134 if k>=2 it keeps removing elements until k<=0 and
prints the sum of elements present in cut1 and

Overall Logic:

  1. from the given string, I want to find the number of 0’s occurring sequentially and store it in cut2.
  2. From cut2, I want to check whether the string is starting or ending from 0, and adding the number of such 0’s in sequence to cut1 i.e no. of zeroes that need 1 cut to get isolated.
  3. As per k value, keep removing the maxing number in either of the lists so that the minimum number of bulbs needs to be disconnected.

P.S.: I’m an amateur coder, I’d be grateful if you provide me any suggestions to improve my coding skills.

Hello there, Firstly let me clarify that I’m no better than you are as a coder but I’ll share some adjustments to your code. First, use a faster I/O library as Scanner is very slow and some problems will give you TLE. Second, use Java 8 libraries like Stream to calculate the sum of an array or ArrayList in one line I’m not sure if it’s time-costly or not but I just experienced TLE in one problem. Lastly, if you want to use Java as the main language for CP I’d suggest using IntelliJ IDEA as it improves your code with suggestions.

1 Like

Thank you @a3000 Will definitely try them.