# Exercise with a piece of code and x

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;
if x + x = 20
and x + x + x = 15
and x - x = 9
and x + x + x = 6
and x - x + x = r
and x + x + x - x = 10
and x - x = 5


then what is the answer of x * x ?

• I don't think it is possible to solve this as the code dosen't have a single x – Spikatrix Feb 5 '15 at 12:01
• The sixth line currently reads "and x - x + x = r". I guess that the "r" here is a typo? – Gamow Feb 5 '15 at 13:18
• 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, and a dead-end line about x. A pattern could be guessed, but it's just a guess. – TheRubberDuck Feb 5 '15 at 14:13
• Seeing people use = in if statements makes me cringe as a programmer. – Joe Z. Feb 5 '15 at 18:34
• Is this an interview question? – shoover Feb 5 '15 at 19:09

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

Why?

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

Of course, we can cross-validate:

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

• Great answer! Though may I ask how obvious the x[i][j] formula was? Because getting the x[i] relation makes sense, and I was about to find x 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. – JPMC Feb 5 '15 at 16:30
• I can't speak for dmg, but the pattern is pretty well-known. It may help to write it out: x=1, x=2, x=3, x=4 x=5, x=6, x=6, x=7, and so on. It's just the pattern for mapping a 2d array to a 1d array. – Brian_Drozd Feb 5 '15 at 23:03
• @Brian_Drozd However,do notice the overlap of the rows of the matrix - x = x. – dmg Feb 6 '15 at 7:09

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

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

• I guess that the language is Java. Yes. It does initialize array variables(ints) to 0,IIRC. – Spikatrix Feb 5 '15 at 12:24
• 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. – EFrog Feb 5 '15 at 12:38
• No, it's not Java. – Set Big O Feb 5 '15 at 14:07