Advertisement

Minimum Right Shifts to Sort the Array - LeetCode 2855 Solution

Minimum Right Shifts to Sort the Array - Complete Solution Guide

Minimum Right Shifts to Sort the Array is LeetCode problem 2855, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3.

Problem Statement

You are given a 0-indexed array nums of length n containing distinct positive integers. Return the minimum number of right shifts required to sort nums and -1 if this is not possible. A right shift is defined as shifting the element at index i to index (i + 1) % n , for all indices. Example 1: Input: nums = [3,4,5,1,2] Output: 2 Explanation: After the first right shift, nums = [2,3,4,5,1]. After the second right shift, nums = [1,2,3,4,5]. Now nums is sorted; therefore the answer is 2. Example 2:

Detailed Explanation

The problem asks you to find the minimum number of right shifts needed to sort a given array of distinct positive integers. A right shift moves each element one position to the right, wrapping around from the last position to the first. The goal is to determine the smallest number of such shifts that result in a sorted array. If it's impossible to sort the array using right shifts, the function should return -1.

Solution Approach

The provided Python code uses a brute-force approach. It iterates through a range of possible right shifts (from 0 up to the array length). For each shift count, it creates a copy of the original array and performs the specified number of right shifts. Finally, it checks if the shifted array is sorted. If a sorted array is found, the corresponding shift count is returned. If no sorted array is found after trying all possible shift counts, it returns -1.

Step-by-Step Algorithm

  1. Step 1: Iterate through possible shift counts (from 0 to n-1, where n is the array length).
  2. Step 2: For each shift count, create a copy of the input array.
  3. Step 3: Perform the right shifts: Move the last element to the beginning, and shift all other elements one position to the right.
  4. Step 4: Check if the shifted array is sorted using `all(temp[i] <= temp[i+1] for i in range(n-1))`.
  5. Step 5: If the array is sorted, return the current shift count.
  6. Step 6: If no sorted array is found after trying all shift counts, return -1.

Key Insights

  • Insight 1: The problem involves cyclical shifts. We need to consider the wrap-around effect when shifting elements.
  • Insight 2: Brute-force is a viable approach for smaller input sizes (as indicated by constraints). We can iterate through all possible numbers of shifts and check if the resulting array is sorted.
  • Insight 3: The array contains distinct elements. This simplifies the check for sorted order – we only need to verify that each element is less than or equal to its successor.

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(n)

Topics

This problem involves: Array.

Companies

Asked at: Accenture.