Click here to Skip to main content
15,886,786 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The question is as follow--
A string contains only 0's and 1's;
Given n(length of the string) find how many permutations of string are possible where every prime length contagious subsequence of the string has greater or equal number of 0's than number of 1's.

My approach--
I have followed a pattern such that for n=2 answer is 3,for n=3 the answer is 4,n=4 answer=6,n=5 answer=9....
But my answer is not correct.
Following is my c code.

C++
#include <stdio.h>

int main()
{
    
    
    
        long int n,i;
        scanf("%ld",&n);
        if(n<3)
        {
           
         if(n=2)
            printf("3");
        }
        else
        {
        long int a[n];
        
        a[0]=3;
        for(i=1;i<n-1;i++)
        a[i]=a[i-1]+i;
        long int temp=a[n-2]%1000000007;
        printf("%ld",temp);
        }
        printf("\n");
   
    return 0;
}
Posted
Updated 6-Aug-14 20:23pm
v2
Comments
[no name] 7-Aug-14 13:43pm    
What is a "contagious subsequence" ?

Also, your if statement is missing an equal sign ("=" vs. "==").
Andreas Gieriet 11-Aug-14 9:54am    
You talk about "string" but in your program there is nothing about characters or strings nor about zeros and ones. I do not understnd your wording of the problem. You might re-phrase the problem in other words.
Andi
nv3 11-Aug-14 11:38am    
With contagious you probably mean contiguous. But this smells like homework. Your code does not even compile.

long int a[n];

does not work in C nor in C++, as the compiler must know the array length at compiler time.

1 solution

your logic looks suspiciuos too.

Rewrite your code!

Write a function like

C++
int runTheTest(int input)


and work with some sample data with output
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900