Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# help creating algorithm for a problem

Jorge Vazquez
Greenhorn
Posts: 9
I've been doing some exercises from codingbat, and I haven't figure out a way to solve this particular problem; may be someone could point me how to approach this. Thanks

http://codingbat.com/prob/p155405

Frank Hanner
Greenhorn
Posts: 4
Jorge Vazquez wrote:I've been doing some exercises from codingbat, and I haven't figure out a way to solve this particular problem; may be someone could point me how to approach this. Thanks

http://codingbat.com/prob/p155405

Do you see a pattern at all?

It's divided into n sections. Inside each section is n numbers (n*n). Each section is essentially counting down. With each section progression the number of zeros decreases by 1 and the appropriate number for the count down is inserted. If that get you thinking about it the right way, I'll show you some example code that might help.

Matthew Brown
Bartender
Posts: 4568
9
Is the problem that you don't see the pattern, or is it that you see the pattern but are having trouble translating it to code?

Jorge Vazquez
Greenhorn
Posts: 9
Matthew Brown wrote:Is the problem that you don't see the pattern, or is it that you see the pattern but are having trouble translating it to code?

yeah, I see the pattern, I'm just having difficulty putting into code...

Matthew Brown
Bartender
Posts: 4568
9
Well, this is an outline of my first thoughts of how to solve it.

Firstly, break the array up into n groups. So that means we've got an outer loop from 1 to n.

Then I'd fill each group from the right-hand side. That means you're counting up from 1 to the group index to work out the value you're inserting, so you've got an inner loop. Then it's just a matter of working out the array position based on the two loop indices.