0ms | Easy | Brute Force
Intuition\nQuestion isn\'t asking about the number of ways to get them consecutive, so it isn\'t about DP. It requires straight forward observation and calculat
# Intuition\nQuestion isn\'t asking about the number of ways to get them consecutive, so it isn\'t about DP. It requires straight forward observation and calculation to get solution. \n\n# Approach\n- a, b and c are given but a < b < c is not always satisfied. Hence, small is the smallest ,big is biggest and med is between big and small. \n- xor of same value is always 0 e.g. 2^2 = 3^3 = 0. small and big are from a, b and c ,Suppose a is smallest and b is biggest then \n\n>  for  med = small ^ a ^ b ^ big ^ c\n> small(a) ^ a = 0\nbig(b) ^ b = 0\nhence med  = c;\n\n- Left is the number of positions in between med and small and right is number of positions in big and med.\n- if the left and right both are 0 means all all the three numbers are already consecutive then we dont need to do any operation hence minimum and maximum are both 0.\n- if two of the three numbers are consecutive then the minimum will be 1\n    eg: 1,2,8\n    in this case only 8 can be moved at position 3 in one operation, hence mini = 1;\n    similarly for \n- if atleast any two of the three numbers have exactly one position in between them, then the minimum will again be 1\n    eg: 1,3,9\n    in this case only 9 can be moved at position 2 in one operation, hence mini = 1;\n    similarly for [1,3,5]\n- In any other case, i.e. if number of positions in small,med and med,big 2 or more, the minimum would be 2\n    eg: 1,4,9\n    in one operation, move 9 to 3 and in second operation 1 to 2 => [2,3,4]\n    OR\n    in one operation, move 1 to 5 and in second operation 9 to 6 => [4,5,6].\n- maximum will always be the number of positions in small,med and med,big as we can take that many operations as maximum steps to get them all consecutive\n    eg 2,4,7\n    move 2 to 3, 7 to 6, 6 to 5 hence 3 operations at max.\n\n# Complexity\n- Time complexity:\n$$O(1)$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    vector<int> numMovesStones(int a, int b, int c) {\n        int small = min({a,b,c});\n        int big = max({a,b,c});\n        int med = small ^ a ^ b ^ big ^ c;\n       \n        int left = med-small-1;\n        int right = big-med-1;\n        int mini;\n        if(left == right && right == 0)\n            mini = 0;\n        else if(left == 0 || right == 0)\n            mini = 1;\n        else if(min(left,right) == 1)\n            mini = 1;\n        else mini = 2;\n        return {mini,left+right};\n    }\n};\n```\n```Python []\nclass Solution:\n    def numMovesStones(self, a: int, b: int, c: int):\n        small = min(a, b, c)\n        big = max(a, b, c)\n        med = small ^ a ^ b ^ big ^ c\n        \n        left = med - small - 1\n        right = big - med - 1\n        if left == right == 0:\n            mini = 0\n        elif left == 0 or right == 0:\n            mini = 1\n        elif min(left, right) == 1:\n            mini = 1\n        else:\n            mini = 2\n        \n        return [mini, left + right]\n```\n```Java []\nimport java.util.Arrays;\n\nclass Solution {\n    public int[] numMovesStones(int a, int b, int c) {\n        int small = Math.min(Math.min(a, b), c);\n        int big = Math.max(Math.max(a, b), c);\n        int med = small ^ a ^ b ^ big ^ c;\n        \n        int left = med - small - 1;\n        int right = big - med - 1;\n        int mini;\n        if (left == right && right == 0) {\n            mini = 0;\n        } else if (left == 0 || right == 0) {\n            mini = 1;\n        } else if (Math.min(left, right) == 1) {\n            mini = 1;\n        } else {\n            mini = 2;\n        }\n        \n        return new int[] {mini, left + right};\n    }\n}\n```\n
['Math', 'Brainteaser', 'C++', 'Java', 'Python3']