🔥Beats 100%🔥✅Simple Logic (C++/Java/Python) Solution With Detailed Explanation✅
\n# Explanation:\n\nThe goal of the function minOperations is to find the minimum number of operations required to make all elements of an array equal. The arra
\n# Explanation:\n\nThe goal of the function `minOperations` is to find the minimum number of operations required to make all elements of an array equal. The array `arr` is defined such that `arr[i] = 2*i + 1` for `i` in the range `[0, n-1]`.\n\nLet\'s break down the code:\n\n1. **Initialization and Target Calculation:**\n    ```cpp\n    int target = -1;\n    if (n & 1)\n        target = ((n/2) * 2) + 1;\n    else\n        target = n;\n    ```\n    - The target value is calculated based on whether `n` is odd or even.\n    - If `n` is odd, the target is `((n/2) * 2) + 1`. This calculates the middle element in the array.\n    - If `n` is even, the target is `n`. This is essentially choosing an element that would balance the array.\n\n2. **Summing Differences:**\n    ```cpp\n    int res = 0;\n    for (int i = 0; i < (n + 1) / 2; i++) {\n        int cal = 2 * i + 1;\n        res += target - cal;\n    }\n    ```\n    - The loop runs from `0` to `(n + 1) / 2`, which is effectively half of the array.\n    - For each element `2*i + 1`, it calculates the difference between the target and the current element and adds it to `res`.\n\n3. **Returning the Result:**\n    ```cpp\n    return res;\n    ```\n    - The result `res` is returned, which represents the minimum number of operations required.\n\n### Example:\n\nLet\'s go through an example where `n = 5`.\n\n1. **Array Elements:**\n   - The array `arr` would be `[1, 3, 5, 7, 9]` because `arr[i] = 2*i + 1`.\n\n2. **Target Calculation:**\n   - Since `n` is odd, `target = ((5/2) * 2) + 1 = 5`.\n\n3. **Summing Differences:**\n   - Iteration 1: `i = 0`, `cal = 2*0 + 1 = 1`, `res += 5 - 1 = 4`\n   - Iteration 2: `i = 1`, `cal = 2*1 + 1 = 3`, `res += 5 - 3 = 2`\n   - The loop runs up to `i = 2` because `(5 + 1) / 2 = 3`.\n\n4. **Total Result:**\n   - `res = 4 + 2 = 6`.\n\nThus, the minimum number of operations required to make all elements of the array `[1, 3, 5, 7, 9]` equal to `5` is `6`.\n\n\nThis function efficiently calculates the minimum number of operations needed to make all elements of the array equal by targeting either the middle element or a calculated balanced element, and summing up the necessary adjustments for half of the array.\n\n# Complexity\n- Time complexity: `O(n)`\n\n- Space complexity: `O(1)`\n\n# Code\n``` C++ []\nclass Solution {\npublic:\n    int minOperations(int n) {\n        int target = -1;\n        if (n & 1)\n            target = ((n/2) * 2) + 1;\n        else\n            target = n;\n\n        int res = 0;\n        for (int i=0; i<(n+1)/2; i++)\n        {\n            int cal = 2*i + 1;\n            res += target - cal;\n        }\n\n        return res;\n    }\n};\n```\n```Java []\npublic class Solution {\n    public int minOperations(int n) {\n        int target = -1;\n        if ((n & 1) == 1)\n            target = ((n / 2) * 2) + 1;\n        else\n            target = n;\n\n        int res = 0;\n        for (int i = 0; i < (n + 1) / 2; i++) {\n            int cal = 2 * i + 1;\n            res += target - cal;\n        }\n\n        return res;\n    }\n}\n\n```\n```Python []\nclass Solution:\n    def minOperations(self, n: int) -> int:\n        target = -1\n        if n & 1:\n            target = ((n // 2) * 2) + 1\n        else:\n            target = n\n\n        res = 0\n        for i in range((n + 1) // 2):\n            cal = 2 * i + 1\n            res += target - cal\n\n        return res\n\n```\n\n### Please upvote if it helped !!!
['Math', 'C++', 'Java', 'Python3']