6
$\begingroup$

I was given this exercise to solve. It's beyond me, but important for me to solve this. The following piece of code is all the context I was given.

int[][] x = new int[6][6];
        if x[1][2] + x[1][4] = 20
        and x[0][3] + x[0][4] + x[0][5] = 15
        and x[2][2] - x[0][5] = 9
        and x[0][0] + x[0][1] + x[0][2] = 6
        and x[3][2] - x[3][1] + x[2][4] = r
        and x[0][5] + x[2][2] + x[1][2] - x[3][1] = 10
        and x[3][0] - x[2][1] = 5

then what is the answer of x[2][5] * x[3][0] ?

$\endgroup$
11
  • 1
    $\begingroup$ I don't think it is possible to solve this as the code dosen't have a single x[2][5] $\endgroup$
    – Spikatrix
    Feb 5, 2015 at 12:01
  • $\begingroup$ The sixth line currently reads "and x[3][2] - x[3][1] + x[2][4] = r". I guess that the "r" here is a typo? $\endgroup$
    – Gamow
    Feb 5, 2015 at 13:18
  • 1
    $\begingroup$ This isn't actually a computer science puzzle. The first line is a line of code, but that's really just a way of indicating that we're talking about a 6x6 matrix, and that we're using C-style access notation. The rest of the lines are not code, but simple facts about the matrix. With that in mind, there may be a pattern here, but it's not a logic puzzle. That is, as Cool Guy said above, we have no information about x[2][5], and a dead-end line about x[3][0]. A pattern could be guessed, but it's just a guess. $\endgroup$ Feb 5, 2015 at 14:13
  • 2
    $\begingroup$ Seeing people use = in if statements makes me cringe as a programmer. $\endgroup$
    – user88
    Feb 5, 2015 at 18:34
  • 1
    $\begingroup$ Is this an interview question? $\endgroup$
    – shoover
    Feb 5, 2015 at 19:09

2 Answers 2

11
$\begingroup$

The answer is:

(2*6 + 6) * (3*6 + 1) = 342

Why?

Using $x[0][3] + x[0][4] + x[0][5] = 15$ and $x[0][0] + x[0][1] + x[0][2] = 6$ we get that $sum(x[0][i]) = 21$ and we can deduce that $x[0][i] = i + 1$. We also can find that $x[2][2] = 9 + x[0][5] = 15$. An obvious thing that can be suggested is that $x[i][j] = i*6 + j + 1$. Thus $x[2][5] * x[3][0] = (2*6 + 6) * (3*6 + 1) = 342$.

Of course, we can cross-validate:

Here are all the equations:
$x[1][2] + x[1][4] = 6 + 3 + 6 + 5 = 20$
$x[0][3] + x[0][4] + x[0][5] = 4 + 5 + 6 = 15$
$x[2][2] - x[0][5] = 2*6 + 3 - 6 = 9$
$x[0][0] + x[0][1] + x[0][2] = 1 + 2 + 3 = 6$
$x[3][2] - x[3][1] + x[2][4] = 3*6 + 3 - 3*6 - 2 + 2*6 + 5 = r$ // which kinda makes sense as r is the 18th letter of the alphabet
$x[0][5] + x[2][2] + x[1][2] - x[3][1] = 6 + 2*6 + 3 + 1*6 + 3 - 3*6 - 2 = 10$
$x[3][0] - x[2][1] = 3*6 + 1 - 2*6 + 2 = 5$

$\endgroup$
3
  • $\begingroup$ Great answer! Though may I ask how obvious the x[i][j] formula was? Because getting the x[0][i] relation makes sense, and I was about to find x[2][2] by myself. But I couldn't make the jump to your relation on my own. It works, but it didn't seem obvious to me. Is this a common pattern/trend in these types of problems, or were you yourself just able to see it as an obvious thing? Just curious in case there is any sort of thing I need to keep in mind if I run into these things again. $\endgroup$
    – JPMC
    Feb 5, 2015 at 16:30
  • $\begingroup$ I can't speak for dmg, but the pattern is pretty well-known. It may help to write it out: x[0][0]=1, x[0][1]=2, x[0][2]=3, x[0][3]=4 x[0][4]=5, x[0][5]=6, x[1][0]=6, x[1][1]=7, and so on. It's just the pattern for mapping a 2d array to a 1d array. $\endgroup$ Feb 5, 2015 at 23:03
  • $\begingroup$ @Brian_Drozd However,do notice the overlap of the rows of the matrix - x[0][5] = x[1][0]. $\endgroup$
    – dmg
    Feb 6, 2015 at 7:09
4
$\begingroup$

My guess would be that this code is written in some programming language that initializes new numerical variables with 0.

Since x[2][5] has not been referenced / used / accessed / changed before your question, then its value will be 0 and also the product x[2][5] * x[3][0] will have value 0.

$\endgroup$
3
  • $\begingroup$ I guess that the language is Java. Yes. It does initialize array variables(ints) to 0,IIRC. $\endgroup$
    – Spikatrix
    Feb 5, 2015 at 12:24
  • $\begingroup$ I don't know anything about Java, but when I look at it, it looks to me like an array of some sort. That is, each of the numbers 0 - 5 are variables. $\endgroup$
    – EFrog
    Feb 5, 2015 at 12:38
  • 3
    $\begingroup$ No, it's not Java. $\endgroup$
    – Set Big O
    Feb 5, 2015 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.