JAVA | 2 solutions | with pictures ✅
Please Upvote :D\n---\n### 1. Naive solution using a loop:\n##### When n is odd:\n\n\n##### When n is even:\n\n\n#### Code:\n\njava []\nclass Solution {\n    pu
# Please Upvote :D\n---\n### 1. Naive solution using a loop:\n##### When `n` is odd:\n![image.png](https://assets.leetcode.com/users/images/23e33018-fb0a-4d00-a796-b6c0b18fb1e8_1674414311.8147304.png)\n\n##### When `n` is even:\n![image.png](https://assets.leetcode.com/users/images/3d021e25-740e-4017-aa2e-f262fbe3287a_1674414531.4001348.png)\n\n#### Code:\n\n``` java []\nclass Solution {\n    public int minOperations(int n) {\n        int ops = 0;\n        for (int i = 0; i < n / 2; i++) {\n            int x = 2 * i + 1;  // given in statement\n            ops += (n - x);\n        } \n\n        return ops;\n    }\n}\n\n// TC: O(n/2) => O(n), SC: O(1)\n```\n---\n### 2. Optimal solution (No loop):\n##### When `n` is odd:\n![image.png](https://assets.leetcode.com/users/images/d13fe28a-c3f3-4d2f-b461-eec7bf86465b_1674415315.432981.png)\n\n##### When `n` is even:\n![image.png](https://assets.leetcode.com/users/images/97972aca-dd14-4638-a4b6-02525a49a4eb_1674415114.2349482.png)\n\n#### Code:\n``` java []\nclass Solution {\n    public int minOperations(int n) {\n        int mid = n / 2;\n        return mid * ((n % 2 == 0)? mid : (mid + 1));\n    }\n}\n\n// TC: O(1), SC: O(1)\n```\n#### OR\n``` java []\nclass Solution {\n    public int minOperations(int n) {\n        int mid = n / 2;\n        return mid * (mid + n % 2);\n    }\n}\n\n// TC: O(1), SC: O(1)\n```\n\n
['Math', 'Java']