# What is the best algorithm for this problem?

Posted
by mark
on Stack Overflow
See other posts from Stack Overflow
or by mark

Published on 2010-05-05T18:26:23Z
Indexed on
2010/05/05
19:08 UTC

Read the original article
Hit count: 266

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[0] = number D2[0] = number ...... D6[0] = number
D1[1] = another number D2[1] = another number ....
..... .... ...... ....
D1[5] = yet another number .... ...... ....
```

Given a second array ST1, containing 1 number:

```
ST1[0] = 6
```

Given a third array ans, containing 6 numbers:

```
ans[0] = 3, ans[1] = 4, ans[2] = 5, ......ans[5] = 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[0] 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[0] //number contained in ST1[0]
EML1 := 0 //start index for the arrays D
While EML1 < EML
if D1[ELM1] = ans[0]
goto two
if D2[ELM1] = ans[0]
goto two
if D3[ELM1] = ans[0]
goto two
if D4[ELM1] = ans[0]
goto two
if D5[ELM1] = ans[0]
goto two
if D6[ELM1] = ans[0]
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[1]
goto two
if D2[ELM1] = ans[1]
goto two
if D3[ELM1] = ans[1]
goto two
if D4[ELM1] = ans[1]
goto two
if D5[ELM1] = ans[1]
goto two
if D6[ELM1] = ans[1]
goto two
ELM1 = ELM1 + 1
return 0
three:
EML1 := 0 start index for arrays Ds
While EML1 < EML
if D1[ELM1] = ans[2]
goto two
if D2[ELM1] = ans[2]
goto two
if D3[ELM1] = ans[2]
goto two
if D4[ELM1] = ans[2]
goto two
if D5[ELM1] = ans[2]
goto two
if D6[ELM1] = ans[2]
goto two
ELM1 = ELM1 + 1
return 0
four:
EML1 := 0 start index for arrays Ds
While EML1 < EML
if D1[ELM1] = ans[3]
goto two
if D2[ELM1] = ans[3]
goto two
if D3[ELM1] = ans[3]
goto two
if D4[ELM1] = ans[3]
goto two
if D5[ELM1] = ans[3]
goto two
if D6[ELM1] = ans[3]
goto two
ELM1 = ELM1 + 1
return 0
five:
EML1 := 0 start index for arrays Ds
While EML1 < EML
if D1[ELM1] = ans[4]
goto two
if D2[ELM1] = ans[4]
goto two
if D3[ELM1] = ans[4]
goto two
if D4[ELM1] = ans[4]
goto two
if D5[ELM1] = ans[4]
goto two
if D6[ELM1] = ans[4]
goto two
ELM1 = ELM1 + 1
return 0
six:
EML1 := 0 start index for arrays Ds
While EML1 < EML
if D1[ELM1] = ans[0]
return 1 //good row of numbers
if D2[ELM1] = ans[0]
return 1
if D3[ELM1] = ans[0]
return 1
if D4[ELM1] = ans[0]
return 1
if D5[ELM1] = ans[0]
return 1
if D6[ELM1] = ans[0]
return 1
ELM1 = ELM1 + 1
return 0
```

As language of choice, it would be pure c

© Stack Overflow or respective owner