Skip to main content

Command Palette

Search for a command to run...

Semi-Ordered Permutation

Published
2 min read
Semi-Ordered Permutation
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You are given a 0-indexed permutation of n integers nums.

A permutation is called semi-ordered if the first number equals 1 and the last number equals n. You can perform the below operation as many times as you want until you make nums a semi-ordered permutation:

  • Pick two adjacent elements in nums, then swap them.

Return the minimum number of operations to make nums a semi-ordered permutation.

A permutation is a sequence of integers from 1 to n of length n containing each number exactly once.

LeetCode Problem - 2717

class Solution {

    // Method to calculate the minimum number of swaps needed to make a semi-ordered permutation
    public int semiOrderedPermutation(int[] nums) {
        int answer = 0;  // Variable to store the result
        int n = nums.length;  // Get the length of the array

        // Find the index of the smallest number (1) in the array
        int x = idxOfNum(nums, 1);

        // Find the index of the largest number (n) in the array
        int y = idxOfNum(nums, n);

        // If the index of the smallest number is less than the index of the largest number
        if(x < y) {
            // Return the sum of swaps needed to move 1 to the front and n to the end
            return x + (n - y - 1);
        }

        // If the index of the largest number is before the smallest, reduce the count by 1
        return x + (n - y - 1) - 1;
    }

    // Helper method to find the index of a specific number in the array
    public int idxOfNum(int[] arr, int number) {
        // Loop through the array to find the position of the target number
        for(int i = 0; i < arr.length; i++) {
            if(arr[i] == number) return i;  // Return index if found
        }
        return -1;  // Return -1 if the number is not found in the array
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.