# What is the best algorithm for this problem?

Filed under:
|
|
|
|
##### optimization

What is the most efficient algorithm to solve the following problem?

Given 6 arrays, D1,D2,D3,D4,D5 and D6 each containing 6 numbers like:

``````D1 = number              D2 = number      ......       D6 = number
D1 = another number      D2 = another number           ....
.....                       ....                ......       ....
D1 = yet another number  ....                ......       ....
``````

Given a second array ST1, containing 1 number:

``````ST1 = 6
``````

Given a third array ans, containing 6 numbers:

``````ans = 3, ans = 4, ans = 5, ......ans = 8
``````

Using as index for the arrays D1,D2,D3,D4,D5 and D6, the number that goes from 0, to the number stored in ST1 minus one, in this example 6, so from 0 to 6-1, compare each res array against each D array

My algorithm so far is:
I tried to keep everything unlooped as much as possible.

``````EML  := ST1   //number contained in ST1
EML1 := 0        //start index for the arrays D

While EML1 < EML
if D1[ELM1] = ans
goto two
if D2[ELM1] = ans
goto two
if D3[ELM1] = ans
goto two
if D4[ELM1] = ans
goto two
if D5[ELM1] = ans
goto two
if D6[ELM1] = ans
goto two

ELM1 = ELM1 + 1

return 0     //bad row of numbers, if while ends

two:

EML1 := 0      start index for arrays Ds
While EML1 < EML
if D1[ELM1] = ans
goto two
if D2[ELM1] = ans
goto two
if D3[ELM1] = ans
goto two
if D4[ELM1] = ans
goto two
if D5[ELM1] = ans
goto two
if D6[ELM1] = ans
goto two
ELM1 = ELM1 + 1

return 0

three:

EML1 := 0      start index for arrays Ds

While EML1 < EML
if D1[ELM1] = ans
goto two
if D2[ELM1] = ans
goto two
if D3[ELM1] = ans
goto two
if D4[ELM1] = ans
goto two
if D5[ELM1] = ans
goto two
if D6[ELM1] = ans
goto two
ELM1 = ELM1 + 1

return 0

four:

EML1 := 0      start index for arrays Ds

While EML1 < EML
if D1[ELM1] = ans
goto two
if D2[ELM1] = ans
goto two
if D3[ELM1] = ans
goto two
if D4[ELM1] = ans
goto two
if D5[ELM1] = ans
goto two
if D6[ELM1] = ans
goto two
ELM1 = ELM1 + 1

return 0

five:

EML1 := 0      start index for arrays Ds

While EML1 < EML
if D1[ELM1] = ans
goto two
if D2[ELM1] = ans
goto two
if D3[ELM1] = ans
goto two
if D4[ELM1] = ans
goto two
if D5[ELM1] = ans
goto two
if D6[ELM1] = ans
goto two
ELM1 = ELM1 + 1

return 0

six:

EML1 := 0      start index for arrays Ds

While EML1 < EML
if D1[ELM1] = ans
return 1            //good row of numbers
if D2[ELM1] = ans
return 1
if D3[ELM1] = ans
return 1
if D4[ELM1] = ans
return 1
if D5[ELM1] = ans
return 1
if D6[ELM1] = ans
return 1
ELM1 = ELM1 + 1

return 0
``````

As language of choice, it would be pure c

© Stack Overflow or respective owner

### Related posts about programming

• #### Imperative Programming v/s Declarative Programming v/s Functional Programming

as seen on Geeks with Blogs - Search for 'Geeks with Blogs'
Imperative Programming :: Imperative programming is a programming paradigm that describes computation in terms of statements that change a program state. In much the same way as the imperative mood in natural languages expresses commands to take action, imperative programs define sequences of commands… >>> More

• #### Programming in python Vs programming in Java

as seen on Stack Overflow - Search for 'Stack Overflow'
I've been writing Java for the last couple of years , and now I've started to write in python (in addition). The problem is that when I look at my Python code it looks like someone tried to hammer Java code into a python format , and it comes out crappy because - well , python ain't Java. Any… >>> More

• #### Have you ever bought a commercial implementation of a programming language for personal programming

as seen on Stack Overflow - Search for 'Stack Overflow'
Commercial products are often a source of ideas and inspiration for open source projects. There are free and open source implementations of almost every programming language ever devised, and a lot of them are very good. For non-work related personal programming projects, have you ever bought an… >>> More

• #### Is Game Programming very different from normal programming?

as seen on Stack Overflow - Search for 'Stack Overflow'
By normal programming, I'm referring to application programming (which is all I'm familiar with at the moment). My goal is to make games, and I wanted to know if the path was different than other programming paths at the beginner level. Or perhaps it's too early for me to worry about that at this… >>> More

• #### Programming activities for high school kids who have no idea what CS or programming is

as seen on Stack Overflow - Search for 'Stack Overflow'
I work at a small high school that's in a very high poverty area. There are only a handful of seniors that are thinking about applying to be an engineer of some sort in college and only 1 kid that applied for Computer Science (he has a couple acceptances so far!). He's been talking to me a lot as… >>> More