Skip to content

Instantly share code, notes, and snippets.

@bparanj
Created August 11, 2020 17:44
Show Gist options
  • Save bparanj/61105e064525f0c6603370f9cd027f2d to your computer and use it in GitHub Desktop.
Save bparanj/61105e064525f0c6603370f9cd027f2d to your computer and use it in GitHub Desktop.
def bit_strings(index, array, result)
# Base case
if index == array.length
result << array.dup
else
# Make a choice to include
array[index] = 1
bit_strings(index+1, array, result)
# Make a choice to exclude
array[index] = 0
bit_strings(index+1, array, result)
end
end
p bit_strings(0, [0,0,0], [])
@bparanj
Copy link
Author

bparanj commented Aug 11, 2020

Without dup, you will get same subarrays in the result array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment