OpenGenus / cosmos

World's largest Contributor driven code dataset | Used in Quark Search Engine, @OpenGenus IQ, OpenGenus Visual Project
http://internship.opengenus.org
GNU General Public License v3.0
13.57k stars 3.68k forks source link

Implement "No consecutive 1s" in C++ #2425

Open jainnipun opened 6 years ago

jainnipun commented 6 years ago

This is a(n):

Details:

Details: Solves #2288

Added in new folder code/dynamic_programming/no_consec_ones

mudit8560 commented 6 years ago

I am interested in it.Please guide me what I have to do in this.

ghost commented 6 years ago

hello , I'm interested in implementing this algorithm. please guide me how to do it.

AdiChat commented 6 years ago

@mudit8560 @arcade0 You need to clone Cosmos, commit changes to your local working copy and create a pull request 👍

jainnipun commented 6 years ago

@mudit8560 @arcade0
This is a simple algo counting the number of all binary representations having non - consequtive 1s for given length. e.g. for for given length 3

All possible binary representations: 000 001 010 011 100 101 110 111

but of these 3 representations have consecutive 1s (011, 110 , 111) so for input=3 output =5.

Logic followed here is that we start building our vector from base for n=1 we can have strings 0 and 1 so output =2 now at each step proceeding if we count strings if we add 0 and strings if we add 1 we can add 0 to all previous as it will not result in consecutive 1s but we can add 1 to only those which do not end in 1.

For given length n we just need to add all strings ending at 0 and 1 at n-1.

This was the algo. Code can be seen in my commit.

Link: http://www.geeksforgeeks.org/count-number-binary-strings-without-consecutive-1s/

Priya-Raut commented 4 years ago

@AdiChat I would like to solve this issue in Java. Please assign it to me.