O(1) solution - with COMPLETE analysis
Imagine, x number of jumbo burgers and y number of small burgers are made from the given tomatoSlices and cheeseSlices. \n\nSo, the equation would be, \n4x + 2y
Imagine, `x` number of jumbo burgers and `y` number of small burgers are made from the given `tomatoSlices` and `cheeseSlices`. \n\nSo, the equation would be, \n`4x + 2y = tomatoSlices` which means, total number of tomatoSlices required for making `x` jumbo burgers and `y` small burgers should be EXACTLY `tomatoSlices` (if possible)\n\nSecond equation would be,\n`x + y = cheeseSlices` which means, total number of cheeseSlices required for making `x` jumbo burgers and `y` small burgers should be EXACTLY `cheeseSlices` (if possible)\n\nThe way we solve this equation is,\nmultiply `4` on both sides of equation 2.\nSo, `4x + 4y = 4*cheeseSlices` <-- Equation 3\n\nSubtract equation 1 from equaiton 3\n\n```\n    4x + 4y = 4*cheeseSlices (equation 3)\n-   4x + 2y = tomatoSlices (equation 1)\n\t__________________________________________\n         2y = 4*cheeseSlices - tomatoSlices <-- Equation 4\n```\n\nNow, if the solution is impossible, we will not get a positive whole integer value for `y` from equation 4.\nSo, let\'s make a quick check for that. \n`y = (4*cheeseSlices - tomatoSlices) / 2` (Based on equation 4)\n\nwhich means, for `y` to be a positive whole integer `4*cheeseSlices - tomatoSlices` must be positive and a multple of 2, otherwise we return empty list.\n\nafter getting y, use equation 2 to get x\n`x = cheeseSlices - y`\n\nAgain check if x is a positive whole integer. (Because `x` could turn out negative based on value of `y`). So, if `x` is negative, return an empty list.\n\nIf all goes well, return list of `x` and `y` as result.\n\n```\npublic List<Integer> numOfBurgers(int tomatoSlices, int cheeseSlices) {\n\tint jCount = 0, // x\n\t\tsCount = 0; // y\n\n\t// 4x + 2y = tomatoSlices\n\t// x + y = cheeseSlices\n\n\tif ((4*cheeseSlices - tomatoSlices) < 0 || (4*cheeseSlices - tomatoSlices) % 2 != 0) {\n\t\treturn Collections.emptyList();\n\t}\n\n\tsCount = (4*cheeseSlices - tomatoSlices) / 2;\n\tjCount = cheeseSlices - sCount;\n\n\treturn jCount >= 0\n\t\t? Arrays.asList(jCount, sCount)\n\t\t: Collections.emptyList();\n}\n```\n\n
[]