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.
C#. This is a no loop solution. It uses arithmetic sequence sum and solves quadratic equations of one variable.
publicstaticintRun(intinit,intdiscount,intmin,intbudget){varcount=(int)Math.Ceiling((min-init)/-(double)discount);// if decreased to zero, set to zerovarsum=CalculateSum(init,-discount,count);// min = 2; diff = 8; a[k]=4, a[k+1]=-4varminPrice=init-discount*(count-1);if(minPrice<0){sum-=minPrice;count-=1;}varremaining=budget-sum;if(remaining>0){// increase countvarremainingCount=(int)Math.Floor(remaining/(double)min);sum+=min*remainingCount;count+=remainingCount;}elseif(remaining<0){// decrease countvarestimatedCounts=CalculateSolutions(-discount,2*init+discount,-2*budget);varestimatedCount=(int)Math.Floor(estimatedCounts.First(x=>x>0&&x<=count));sum=CalculateSum(init,-discount,estimatedCount);count=estimatedCount;}// number of gamesreturncount;}privatestaticdoubleCalculateSum(intinit,intdiff,doublecount)=>(init*2+diff*(count-1))*count/2;privatestaticList<double>CalculateSolutions(doublea,doubleb,doublec){varsqrt=Math.Sqrt(Math.Pow(b,2)-4*a*c);return[(-b+sqrt)/(2*a),(-b-sqrt)/(2*a),];}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Halloween Sale
You are viewing a single comment's thread. Return to all comments →
C#. This is a no loop solution. It uses arithmetic sequence sum and solves quadratic equations of one variable.