Created
May 25, 2026 12:37
-
-
Save SuryaPratapK/e2b88aed9091714d1fb8692f4e2390d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| bool checkIfIncreasing(vector<int>& nums,int& min_idx,int& n){ | |
| int i; | |
| for(i=min_idx;i<min_idx+n;++i){ | |
| if(nums[(i+1)%n]-1 != nums[i%n]) | |
| break; | |
| } | |
| if((i+1)%n == min_idx) | |
| return true; | |
| return false; | |
| } | |
| public: | |
| int minOperations(vector<int>& nums) { | |
| int n = nums.size(); | |
| //Step-1: Find min_idx | |
| int min_idx; | |
| int i; | |
| for(i=0;i<n;++i) | |
| if(nums[i]==0){ | |
| min_idx = i; | |
| break; | |
| } | |
| //Step-2: Find if increasing or decreasing ? | |
| bool is_increasing = checkIfIncreasing(nums,min_idx,n); | |
| bool is_decreasing = false; | |
| if(!is_increasing){ | |
| reverse(nums.begin(),nums.end()); | |
| min_idx = n-min_idx-1; | |
| is_decreasing = checkIfIncreasing(nums,min_idx,n); | |
| } | |
| if(!is_increasing and !is_decreasing) | |
| return -1; | |
| //Step-3: Calculate min_operations | |
| int min_ops = n; | |
| if(is_increasing){ | |
| min_ops = min(min_idx, 2+n-min_idx); | |
| }else{ | |
| min_ops = min((1+min_idx), 1+(n-min_idx)); | |
| } | |
| return min_ops; | |
| } | |
| }; | |
| /* | |
| //JAVA | |
| class Solution { | |
| private boolean checkIfIncreasing(int[] nums, int min_idx, int n) { | |
| int i; | |
| for (i = min_idx; i < min_idx + n; ++i) { | |
| if (nums[(i + 1) % n] - 1 != nums[i % n]) { | |
| break; | |
| } | |
| } | |
| if ((i + 1) % n == min_idx) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| public int minOperations(int[] nums) { | |
| int n = nums.length; | |
| // Step-1: Find min_idx | |
| int min_idx = 0; | |
| for (int i = 0; i < n; ++i) { | |
| if (nums[i] == 0) { | |
| min_idx = i; | |
| break; | |
| } | |
| } | |
| // Step-2: Find if increasing or decreasing? | |
| boolean is_increasing = checkIfIncreasing(nums, min_idx, n); | |
| boolean is_decreasing = false; | |
| if (!is_increasing) { | |
| // Reverse the array | |
| for (int i = 0; i < n / 2; i++) { | |
| int temp = nums[i]; | |
| nums[i] = nums[n - 1 - i]; | |
| nums[n - 1 - i] = temp; | |
| } | |
| min_idx = n - min_idx - 1; | |
| is_decreasing = checkIfIncreasing(nums, min_idx, n); | |
| } | |
| if (!is_increasing && !is_decreasing) { | |
| return -1; | |
| } | |
| // Step-3: Calculate min_operations | |
| int min_ops = n; | |
| if (is_increasing) { | |
| min_ops = Math.min(min_idx, 2 + n - min_idx); | |
| } else { | |
| min_ops = Math.min((1 + min_idx), 1 + (n - min_idx)); | |
| } | |
| return min_ops; | |
| } | |
| } | |
| #Python | |
| from typing import List | |
| class Solution: | |
| def _checkIfIncreasing(self, nums: List[int], min_idx: int, n: int) -> bool: | |
| i = min_idx | |
| while i < min_idx + n: | |
| if nums[(i + 1) % n] - 1 != nums[i % n]: | |
| break | |
| i += 1 | |
| if (i + 1) % n == min_idx: | |
| return True | |
| return False | |
| def minOperations(self, nums: List[int]) -> int: | |
| n = len(nums) | |
| # Step-1: Find min_idx | |
| min_idx = -1 | |
| for i in range(n): | |
| if nums[i] == 0: | |
| min_idx = i | |
| break | |
| # Step-2: Find if increasing or decreasing? | |
| is_increasing = self._checkIfIncreasing(nums, min_idx, n) | |
| is_decreasing = False | |
| if not is_increasing: | |
| # Reverse the list in-place | |
| nums.reverse() | |
| min_idx = n - min_idx - 1 | |
| is_decreasing = self._checkIfIncreasing(nums, min_idx, n) | |
| if not is_increasing and not is_decreasing: | |
| return -1 | |
| # Step-3: Calculate min_operations | |
| min_ops = n | |
| if is_increasing: | |
| min_ops = min(min_idx, 2 + n - min_idx) | |
| else: | |
| min_ops = min(1 + min_idx, 1 + (n - min_idx)) | |
| return min_ops | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment