Array Recursion

Posted by GeeYouAreYou on Stack Overflow See other posts from Stack Overflow or by GeeYouAreYou
Published on 2010-05-30T16:17:23Z Indexed on 2010/05/30 16:22 UTC
Read the original article Hit count: 362

Filed under:
|
|

I've got an assignment I can't figure out, any pointers will be much appreciated, it goes like so:

There's a series of light bulbs represented as an array of true/false, there's a switch for every light bulb, by clicking it for any light bulb, you toggle it as well as 2 adjacent ones (1 from left & another 1 from right; if clicked switch's bulb on edge -- only 1 adjacent toggled of course).

What is needed to accomplish is a method that accepts an array of a series of turned on/off light bulbs and another one representing another state of supposedly the 1st array after some switches have been clicked..! So recursion must be used to find out whether there's a combination of switch clicks that will transform array 1 to array 2.

Here's the signature of the method:

public static boolean disco(boolean[] init, boolean[] target)

Will return true if array init can be transformed to target, false otherwise. Method must be static and not use any other static and global variables, only local.

Example:

boolean[] init = {true, false, true, false, true, false};
boolean[] target = {false, true, false, true, false, true};

For above 2 arrays, disco(init, target) will return true because toggling the 1st and 4th bulbs would yield the target state (remember adjacent bulbs being toggled as well).

Any help much appreciated..!!

© Stack Overflow or respective owner

Related posts about java

Related posts about arrays