We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
constlongdivide=pow(10,9)+7;// Return the number of ways to fill in the array.longcountArray(intn,intk,intx){if(n==3){if(x==1)returnlong(k-1)%divide;elsereturnlong(k-2)%divide;}else{// Array of answers for various n and x=1vector<long>A;// Array of answers for various n and x!=1 (does not depend on specifica value of x)vector<long>B;A.reserve(n-2);B.reserve(n-2);// special case n=3A.push_back((k-1)%divide);B.push_back((k-2)%divide);// increase n by 1 recurrentlyfor(inti=0;i<n-3;i++){longa=(k-1)*B.back();longb=A.back()+(k-2)*B.back();a=a%divide;b=b%divide;A.push_back(a);B.push_back(b);}if(x==1)returnA.back();elsereturnB.back();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Construct the Array
You are viewing a single comment's thread. Return to all comments →