question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
⌀
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
first-completely-painted-row-or-column
JAVA Solution Using (1).Array [15ms] || (2).HashMap [25ms]
java-solution-using-1array-15ms-2hashmap-5rm2
IntuitionThe challenge is to track how many elements of each row and column have been processed efficiently. This can be achieved by mapping the elements of mat
Prem_Chandu_Palivela
NORMAL
2025-01-20T03:06:50.900011+00:00
2025-01-20T03:06:50.900011+00:00
31
false
# Intuition The challenge is to track how many elements of each row and column have been processed efficiently. This can be achieved by mapping the elements of mat to their positions by using HashMap or Array.And then we traverse the array maintaining frequency arrays of rows and columns. And we return the index of arr...
2
0
['Array', 'Hash Table', 'Java']
0
first-completely-painted-row-or-column
Simple by traversal || Easy Intuition || HashMap || TC: O(n* m + k)
simple-by-traversal-easy-intuition-hashm-yhu2
IntuitionIn this simply we will map the indexes so that it can be easily accessible in O(1). Once we get all the index mapped we will take a counter for each co
hamza_18
NORMAL
2025-01-20T02:34:06.194873+00:00
2025-01-20T02:34:06.194873+00:00
16
false
# Intuition In this simply we will map the indexes so that it can be easily accessible in O(1). Once we get all the index mapped we will take a counter for each col and each row (1D array) and for each row and col we will keep track of the number of elements painted in the curr row or col at present. as soon as we get ...
2
0
['Hash Table', 'Matrix', 'C++']
0
first-completely-painted-row-or-column
Hash Map Solution | Clean Code
hash-map-solution-clean-code-by-attentio-85sm
Approacharr and mat both contain all integers in the range [1, m * n]. So we: Use a map posMap to store every integer's position in mat. Use two arrays rowsCnt
Attention2000
NORMAL
2025-01-20T01:12:35.223521+00:00
2025-01-20T01:12:35.223521+00:00
13
false
# Approach <!-- Describe your approach to solving the problem. --> `arr` and `mat` both contain all integers in the range `[1, m * n]`. So we: 1. Use a map `posMap` to store every integer's position in `mat`. 2. Use two arrays `rowsCnt` and `colsCnt` to record how many cells in a row and a column are colored separately...
2
0
['Hash Table', 'C++']
0
first-completely-painted-row-or-column
Python 3 solution - Easy to understand
python-3-solution-by-iloabachie-bc2h
IntuitionFirst, I created 3 dictionaries. Dictionary with row numbers as key and count of elements in the row as values. Dictionary with col numbers as key and
iloabachie
NORMAL
2025-01-20T01:10:14.680403+00:00
2025-01-20T01:22:06.308427+00:00
270
false
# Intuition First, I created 3 dictionaries. 1. Dictionary with row numbers as key and count of elements in the row as values. 2. Dictionary with col numbers as key and count of elements in the column as values. 3. Dictionary with each number in the matrix as key and a list containing its row number and column number a...
2
0
['Python3']
1
first-completely-painted-row-or-column
Best solution in C++ . O(n*m) time and space complexity.
best-solution-in-clinear-time-and-space-f8rfp
IntuitionApproachComplexity Time complexity: Space complexity: Code
Shahriar22
NORMAL
2025-01-20T00:27:53.127821+00:00
2025-01-20T00:41:45.692318+00:00
151
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
2
0
['C++']
0
first-completely-painted-row-or-column
Simple and clear solution. O(n*m)
simple-and-clear-solution-onm-by-xxxxkav-qijo
null
xxxxkav
NORMAL
2025-01-20T00:17:28.977201+00:00
2025-01-20T00:17:50.313705+00:00
239
false
``` class Solution: def firstCompleteIndex(self, arr: List[int], mat: List[List[int]]) -> int: m, n = len(mat), len(mat[0]) cnt_row, cnt_col = [0] * m, [0] * n dict_mat = [0] * (m*n+1) for i, j in product(range(m), range(n)): dict_mat[mat[i][j]] = (i, j) for ind, ...
2
0
['Python3']
0
first-completely-painted-row-or-column
Simple | ✅
simple-by-shadab_ahmad_khan-n2ta
Code\n\nclass Solution {\n public int firstCompleteIndex(int[] arr, int[][] mat) {\n HashMap<Integer,int[]> map=new HashMap<>();\n HashMap<Inte
Shadab_Ahmad_Khan
NORMAL
2023-12-14T09:15:23.725040+00:00
2023-12-14T09:15:23.725064+00:00
38
false
# Code\n```\nclass Solution {\n public int firstCompleteIndex(int[] arr, int[][] mat) {\n HashMap<Integer,int[]> map=new HashMap<>();\n HashMap<Integer,Integer> row=new HashMap<>();\n HashMap<Integer,Integer> col=new HashMap<>();\n for(int i=0; i<mat.length; i++){\n for(int j=0...
2
0
['Array', 'Hash Function', 'Java']
0
first-completely-painted-row-or-column
Unordered Map , C++ ✅✅
unordered-map-c-by-deepak_5910-mxv1
Approach\n Describe your approach to solving the problem. \nThink brute force, but optimize brute force by tracking colored cells using a hash table.\n# Complex
Deepak_5910
NORMAL
2023-06-20T09:09:30.883791+00:00
2023-09-16T12:58:12.716006+00:00
50
false
# Approach\n<!-- Describe your approach to solving the problem. -->\nThink brute force, but optimize brute force by tracking colored cells using a hash table.\n# Complexity\n- Time complexity:O(N*M)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N*M)\n<!-- Add your space complexity here,...
2
0
['Hash Table', 'C++']
0
first-completely-painted-row-or-column
scala easy solution
scala-easy-solution-by-lyk4411-rw4h
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
lyk4411
NORMAL
2023-05-13T12:38:32.304819+00:00
2023-05-13T12:38:32.304877+00:00
31
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
2
0
['Scala']
0
first-completely-painted-row-or-column
✅C++ || Hash Table✅
c-hash-table-by-deepak_2311-k592
Complexity\n- Time complexity:O(n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(n)\n Add your space complexity here, e.g. O(n) \n\n# Code
godmode_2311
NORMAL
2023-04-30T07:51:18.561636+00:00
2023-04-30T07:59:54.807143+00:00
89
false
# Complexity\n- Time complexity:`O(n)`\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:`O(n)`\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n\n int row_Size , col_Size;\n\n bool isFilled(vector<int>&rows,int curr_Row , vector<int>&co...
2
0
['Array', 'Hash Table', 'Matrix', 'C++']
0
first-completely-painted-row-or-column
Map solution for C# (explanation + complexity)
map-solution-for-c-explanation-complexit-23f6
Approach\nThe problem can be divided into two sub-problems:\n1. How to find the value in the matrix in a fast way\n2. How to understand that a row or column is
deleted_user
NORMAL
2023-04-30T06:44:13.114444+00:00
2023-05-07T04:41:26.304082+00:00
41
false
# Approach\nThe problem can be divided into two sub-problems:\n1. How to find the value in the matrix in a fast way\n2. How to understand that a row or column is completely painted\n\nSo,\n1. To solve the first problem, we will create a dictionary with cell values as a key and row/column indices as a value. It will cos...
2
0
['C#']
0
first-completely-painted-row-or-column
C++ || Easy
c-easy-by-shrikanta8-mahs
\nclass Solution {\npublic:\n int firstCompleteIndex(vector<int>& arr, vector<vector<int>>& mat) {\n int r = mat.size(),c= mat[0].size();\n vec
Shrikanta8
NORMAL
2023-04-30T04:05:12.761218+00:00
2023-04-30T04:13:12.397418+00:00
93
false
```\nclass Solution {\npublic:\n int firstCompleteIndex(vector<int>& arr, vector<vector<int>>& mat) {\n int r = mat.size(),c= mat[0].size();\n vector<pair<int,int> > ind(r*c+1);\n \n for(int i=0;i<r;i++){\n for(int j=0;j<c;j++){\n ind[mat[i][j]] = {i,j};\n ...
2
0
['C']
0
first-completely-painted-row-or-column
Using HashMap by Storing the Index values || Beats 90% of Java Code
using-hashmap-by-storing-the-index-value-ycw4
IntuitionApproachComplexity Time complexity:O(m*n) Space complexity: O(m*n) Code
zeeshan312
NORMAL
2025-01-27T20:33:51.131955+00:00
2025-01-27T20:33:51.131955+00:00
3
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity:O(m*n) <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: O(m*n) <!-- Add your space complexity here, e.g. $$O(n)$$ ...
1
0
['Hash Table', 'Java']
0
first-completely-painted-row-or-column
Beat 92.2% C++. O(m*n) easy solution.
beat-922-c-omn-easy-solution-by-vikasnir-9dpj
IntuitionFocus on reducing time complexity by using hash arrays.ApproachStep 1 : Create a hash vector to store the row and col of the values given in the matrix
VikasNiranjan
NORMAL
2025-01-22T12:50:56.727087+00:00
2025-01-22T12:50:56.727087+00:00
6
false
# Intuition Focus on reducing time complexity by using hash arrays. # Approach **Step 1 :** Create a hash vector to store the row and col of the values given in the matrix to get row and col in O(1). **Step 2 :** Create a rowHash and a colHash to count the number of cells painted in that particular row and col. **St...
1
0
['Hash Table', 'C++']
0
first-completely-painted-row-or-column
✅ 🌟 JAVA SOLUTION ||🔥 BEATS 100% PROOF🔥|| 💡 CONCISE CODE ✅ || 🧑‍💻 BEGINNER FRIENDLY
java-solution-beats-100-proof-concise-co-02l8
Complexity Time complexity:O(m * n) Space complexity:O(m * n) Code
Shyam_jee_
NORMAL
2025-01-21T18:55:22.584290+00:00
2025-01-21T18:55:22.584290+00:00
4
false
# Complexity - Time complexity:O(*m* * *n*) <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity:O(*m* * *n*) <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int firstCompleteIndex(int[] arr, int[][] mat) { // Map to store the index of ...
1
0
['Array', 'Hash Table', 'Matrix', 'Java']
0
first-completely-painted-row-or-column
Easy to understand C++ approach
easy-to-understand-c-approach-by-itsme_s-r3mt
IntuitionHere our intuition is that we will have to keep track of the number of elements coloured for each row and each column so that we can figure out when a
itsme_S
NORMAL
2025-01-21T06:04:16.208183+00:00
2025-01-21T06:04:16.208183+00:00
13
false
# Intuition Here our intuition is that we will have to keep track of the number of elements coloured for each row and each column so that we can figure out when a full row or column has been coloured. If we do it in a brute force way, then it will take a lot of time which we do not want. So we need to think of another ...
1
0
['C++']
0
semi-ordered-permutation
[Java/C++/Python] Index of 1 and n
javacpython-index-of-1-and-n-by-lee215-ivn3
Explanation\nIf index of 1 is i,\n1 needs i swaps to be the first.\n\nIf index of n is j,\nn needs n - 1 - j swaps to be the first.\n\nIf i < j,\nres is i + (n
lee215
NORMAL
2023-06-04T04:28:19.435006+00:00
2023-06-04T04:28:19.435040+00:00
1,931
false
# **Explanation**\nIf index of `1` is `i`,\n`1` needs `i` swaps to be the first.\n\nIf index of `n` is `j`,\n`n` needs `n - 1 - j` swaps to be the first.\n\nIf `i < j`,\n`res` is `i + (n - 1 - j)`.\n\nIf `i > j`,\n`res` is `i + (n - 1 - j) - 1`,\nsave one swap when swap `1` and `n`.\n<br>\n\n# **Complexity**\nTime `O(n...
24
0
['C', 'Python', 'Java']
6
semi-ordered-permutation
Python 3 || 2 lines, w/ brief explanation || T/S: 99% / 63%
python-3-2-lines-w-brief-explanation-ts-ysw6p
Here\'s the plan:\n\n- We determine the indices mnandmxof two numbers being migrated to the ends of the list.\n- We count the number of steps required to move e
Spaulding_
NORMAL
2023-06-04T16:54:41.140523+00:00
2024-11-02T06:40:51.060592+00:00
746
false
Here\'s the plan:\n\n- We determine the indices `mn`and`mx`of two numbers being migrated to the ends of the list.\n- We count the number of steps required to move each, which are `mn` and `n-mx-1`. \n- We check whether `mn > mx`, in which case the two migrations share a step and we then correct by decrementing the sum ...
16
0
['Python3']
2
semi-ordered-permutation
Find Positions
find-positions-by-votrubac-m4q2
C++\ncpp\nint semiOrderedPermutation(vector<int>& n) {\n int first = find(begin(n), end(n), 1) - begin(n);\n int last = find(begin(n), end(n), n.size()) -
votrubac
NORMAL
2023-06-04T04:01:47.388959+00:00
2023-06-04T04:01:47.389002+00:00
1,420
false
**C++**\n```cpp\nint semiOrderedPermutation(vector<int>& n) {\n int first = find(begin(n), end(n), 1) - begin(n);\n int last = find(begin(n), end(n), n.size()) - begin(n);\n return first + (n.size() - last - 1) - (first > last);\n}\n```
13
1
['C']
1
semi-ordered-permutation
Long but easy solution :)
long-but-easy-solution-by-movsar-k1gy
\n/**\n * @param {number[]} nums\n * @return {number}\n */\nconst semiOrderedPermutation = function (nums) {\n const n = nums.length\n if (nums[0] === 1 &
movsar
NORMAL
2023-06-04T04:03:38.167103+00:00
2023-06-04T04:03:38.167148+00:00
1,303
false
```\n/**\n * @param {number[]} nums\n * @return {number}\n */\nconst semiOrderedPermutation = function (nums) {\n const n = nums.length\n if (nums[0] === 1 && nums[n - 1] === n) return 0\n \n let cn = 0\n \n let i = nums.indexOf(1)\n while (nums[0] !== 1 && i >= 0) {\n const curr = nums[i - ...
8
0
['JavaScript']
2
semi-ordered-permutation
Explained - iteration || Very simple and easy to understand
explained-iteration-very-simple-and-easy-1u03
Approach\n1. Find the ith index of min val 1 => we need to do i operation to move 1 to first position\n2. Find the ith index of the max val n => we need to do (
kreakEmp
NORMAL
2023-06-04T04:19:03.574853+00:00
2023-06-04T14:22:51.622828+00:00
2,981
false
## Approach\n1. Find the ith index of min val 1 => we need to do i operation to move 1 to first position\n2. Find the ith index of the max val n => we need to do (n - i - 1) operation to take it to last position\n3. Take the sum of above two as the answer. Only corner case is when the ith index of 1 is larger than ith ...
7
0
['C++']
2
semi-ordered-permutation
python3 Solution
python3-solution-by-motaharozzaman1996-ez5s
\n\nclass Solution:\n def semiOrderedPermutation(self, nums: List[int]) -> int:\n n=len(nums)\n i=nums.index(1)\n j=nums.index(n)\n
Motaharozzaman1996
NORMAL
2023-06-04T12:45:44.714687+00:00
2023-06-04T12:45:44.714727+00:00
621
false
\n```\nclass Solution:\n def semiOrderedPermutation(self, nums: List[int]) -> int:\n n=len(nums)\n i=nums.index(1)\n j=nums.index(n)\n ans=i+n-1-j-(i>j)\n return ans\n```
6
0
['Python', 'Python3']
0
semi-ordered-permutation
Java | Easy solution | 5 lines
java-easy-solution-5-lines-by-judgementd-51p4
Complexity\n- Time complexity: O(n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(1)\n Add your space complexity here, e.g. O(n) \n\n# Co
judgementdey
NORMAL
2023-06-04T04:04:48.261498+00:00
2023-06-04T04:09:18.636911+00:00
1,376
false
# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int semiOrderedPermutation(int[] nums) {\n int n = nums.length, a = 0, b = 0;\n \n ...
6
0
['Array', 'Java']
1
semi-ordered-permutation
Find C++
find-c-by-harsh_negi_07-h7l4
Intuition\nFind Position of 1 and n\nResult = ind_one + (n - ind_n + 1) (Calculating Swaps)\nBut if ind_one is on left of ind_n, it means you one swap operation
negiharsh12
NORMAL
2023-06-04T04:02:36.968333+00:00
2023-06-04T04:09:54.247898+00:00
1,156
false
# Intuition\n**Find Position of ```1``` and ```n```**\n**```Result = ind_one + (n - ind_n + 1)```** (Calculating Swaps)\n**But if ind_one is on left of ind_n, it means you one swap operation will be common for both i.e ```swap(1, n)``` so will do ```Result - 1```**\n\n# Code\n```\nclass Solution {\npublic:\n int sem...
5
0
['C++']
1
semi-ordered-permutation
GET INDEX OF 1 & n || C++
get-index-of-1-n-c-by-ganeshkumawat8740-jmyh
get index 1 and n\na = index of 1;\nb = index of n\nif(index of 1 < index of n)\n than return a+n-1-b;\nelse return a+n-1-b-n//there is we substracte previou
ganeshkumawat8740
NORMAL
2023-06-04T06:41:41.697889+00:00
2023-06-04T06:41:41.697935+00:00
1,246
false
get index 1 and n\na = index of 1;\nb = index of n\nif(index of 1 < index of n)\n than return a+n-1-b;\nelse return a+n-1-b-n//there is we substracte previous ans by 1 because here one case is possible that we swap(1,n)\n# Code\n```\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& nums) {\n ...
4
0
['C++']
2
semi-ordered-permutation
C++ || easy solution
c-easy-solution-by-gauravtripathi310-6ir9
Intuition\n Describe your first thoughts on how to solve this problem. \nFind the position of 1 and the n in the nums which will take o(n)\ni have used find but
gauravtripathi310
NORMAL
2023-06-05T14:16:05.425210+00:00
2023-06-05T14:16:05.425245+00:00
53
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFind the position of 1 and the n in the nums which will take o(n)\ni have used find but simple iteration can also be done to find the pos of both the elements \n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Find ...
3
0
['C++']
0
semi-ordered-permutation
[C++] ✅Easy O(n) Solution w/ Intuition + Code [Finding index]
c-easy-on-solution-w-intuition-code-find-m3zt
Intuition\nMy intuition for this problem was to know the number of swaps to change the position of a number. Our task is to move the smallest number to the star
nipunrautela
NORMAL
2023-06-04T05:35:51.190442+00:00
2023-06-04T05:37:17.809836+00:00
95
false
# Intuition\nMy intuition for this problem was to know the number of swaps to change the position of a number. Our task is to move the smallest number to the start and the largest number to the end of the array.\nTo move a number from index `i=5` to `j=0`, we need `i-j` swaps. So to move the minimum number from any ind...
3
0
['C++']
1
semi-ordered-permutation
Simple Java O(n) Solution
simple-java-on-solution-by-shashankbhat-cr1w
Approach\n- Get the index of 1 and N\n- Number of swaps would be sum of difference between index of 1 and 0 and difference between index of N and last index.\n-
shashankbhat
NORMAL
2023-06-04T04:44:47.024424+00:00
2023-06-04T07:15:12.671190+00:00
648
false
# Approach\n- Get the index of 1 and N\n- Number of swaps would be sum of difference between index of 1 and 0 and difference between index of N and last index.\n- If position of N is before 1, then we require 1 less swap\n\n# Code\n```\nclass Solution {\n public int semiOrderedPermutation(int[] nums) {\n int ...
3
0
['Java']
0
semi-ordered-permutation
Python 3 || Easy Solution || Compare Min and Max index
python-3-easy-solution-compare-min-and-m-v7wh
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
SarahDong
NORMAL
2023-06-04T04:17:30.986764+00:00
2023-06-04T04:17:30.986804+00:00
621
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $...
3
0
['Python3']
2
semi-ordered-permutation
Solution in C
solution-in-c-by-akcyyix0sj-unxt
Code
vickyy234
NORMAL
2025-01-05T04:46:01.469512+00:00
2025-01-05T04:46:01.469512+00:00
57
false
# Code ```c [] int semiOrderedPermutation(int* nums, int numsSize) { int index1, index2; for (int i = 0; i < numsSize; i++) { if (nums[i] == 1) index1 = i; if (nums[i] == numsSize) index2 = i; } if (index1 < index2) return index1 + ((numsSize - 1) - index2...
2
0
['C']
0
semi-ordered-permutation
i/p o/p analyze kro ho jayega :D
ip-op-analyze-kro-ho-jayega-d-by-yesyese-04pe
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
yesyesem
NORMAL
2024-08-26T09:06:06.675161+00:00
2024-08-26T09:06:06.675180+00:00
42
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
2
0
['C++']
0
semi-ordered-permutation
Java ⭐️ || Beginner Friendly Solution ✅ || 100% beats ✈️ || Fully Explained 💯
java-beginner-friendly-solution-100-beat-5hom
Approach\n\nTo convert the given permutation nums into a semi-ordered permutation, we need to perform swaps to move the number 1 to the first position and the n
akobirswe
NORMAL
2023-08-06T14:40:27.564305+00:00
2023-08-06T14:40:27.564333+00:00
70
false
# Approach\n\nTo convert the given permutation `nums` into a semi-ordered permutation, we need to perform swaps to move the number `1` to the first position and the number `n` to the last position. We can achieve this by finding the indices of `1` and `n` in the array and then calculating the number of operations requi...
2
0
['Array', 'Simulation', 'Java', 'C#']
0
semi-ordered-permutation
[C++/Java] Determining the Positions of 1 and N
cjava-determining-the-positions-of-1-and-wahp
\nDetermine the locations of 1 and N, labelled as one_index and n_index respectively. The number of swaps needed to reposition 1 at the start is equivalent to o
xiangcan
NORMAL
2023-06-06T18:53:09.231527+00:00
2023-06-06T18:54:01.276138+00:00
87
false
\nDetermine the locations of `1` and `N`, labelled as `one_index` and `n_index` respectively. The number of swaps needed to reposition `1` at the start is equivalent to `one_index`, and similarly, to move `N` to the final position, `N - 1 - n_index` swaps are required. \n\nHowever, should `1` be located to the right of...
2
0
['C', 'Java']
0
semi-ordered-permutation
Easy Solution || C++
easy-solution-c-by-dcoder_53-vf6i
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Dcoder_53
NORMAL
2023-06-05T05:24:02.158455+00:00
2023-06-05T11:02:13.916377+00:00
190
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(n)\n\n- Space complexity:O(1)\n\n# Code\n```\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& nums) {\n ...
2
0
['C++']
0
semi-ordered-permutation
Python Easy Solution
python-easy-solution-by-sumedh0706-8zte
Code\n\nclass Solution:\n def semiOrderedPermutation(self, nums: List[int]) -> int:\n a=nums.index(min(nums))\n b=nums.index(max(nums))\n
Sumedh0706
NORMAL
2023-06-04T07:25:15.323235+00:00
2023-06-04T07:25:15.323277+00:00
82
false
# Code\n```\nclass Solution:\n def semiOrderedPermutation(self, nums: List[int]) -> int:\n a=nums.index(min(nums))\n b=nums.index(max(nums))\n if b<a:\n return a+(len(nums)-1-b)-1\n else:\n return a+(len(nums)-1-b)\n \n```
2
0
['Python3']
0
semi-ordered-permutation
✔️ Python3 Solution | Find Position
python3-solution-find-position-by-satyam-r66t
Intuition\nFind the position of 1 and n in the nums let say l and r resp., no. of swaps required to make l the first-number is l since we need to swap all the e
satyam2001
NORMAL
2023-06-04T04:43:27.110340+00:00
2023-06-04T04:43:27.110377+00:00
390
false
# Intuition\nFind the position of 1 and n in the nums let say l and r resp., no. of swaps required to make l the first-number is `l` since we need to swap all the elements with 1 from index l to 0, Similarly for n be the last-number we required `n - r - 1` swaps.\nBut there\'s a catch if the index of n is less than 1 t...
2
0
['Python', 'Python3']
0
semi-ordered-permutation
O(n) || Very easy || Java
on-very-easy-java-by-shyamchadha34-889d
Approach\n Describe your approach to solving the problem. \n Just find the idex of 1 and the index of maximum number. \n If the index of 1 > index of max
shyamchadha34
NORMAL
2023-06-04T04:13:46.628440+00:00
2023-06-04T04:13:46.628465+00:00
131
false
# Approach\n<!-- Describe your approach to solving the problem. -->\n Just find the idex of 1 and the index of maximum number. \n If the index of 1 > index of max number, it will take one swap less \n if(idx1>idx){\n idx1=idx1-1;\n }\n The answer will be (nums.length-1-idx) + idx1\n\n# Complex...
2
0
['Java']
0
semi-ordered-permutation
Simple || Clean || Java Solution
simple-clean-java-solution-by-himanshubh-ngcs
\njava []\nclass Solution {\n public int semiOrderedPermutation(int[] nums) {\n int opr = 0, j = 0;\n// finding 1\n for(int i=0; i<nums
HimanshuBhoir
NORMAL
2023-06-04T04:03:12.424852+00:00
2023-06-04T04:03:12.424898+00:00
148
false
\n```java []\nclass Solution {\n public int semiOrderedPermutation(int[] nums) {\n int opr = 0, j = 0;\n// finding 1\n for(int i=0; i<nums.length; i++) \n if(nums[i] == 1) j = i;\n// operating 1 to start index\n while(j > 0){\n nums[j] = nums[j-1];\n ...
2
0
['Java']
0
semi-ordered-permutation
cpp 🤩 | easy solution
cpp-easy-solution-by-varuntyagig-b563
Complexity Time complexity: O(n) Space complexity: O(1) Code
varuntyagig
NORMAL
2025-03-24T09:14:39.172930+00:00
2025-03-24T09:14:39.172930+00:00
12
false
# Complexity - Time complexity: $$O(n)$$ - Space complexity: $$O(1)$$ # Code ```cpp [] class Solution { public: int semiOrderedPermutation(vector<int>& nums) { // Step-1 // find 1 int one = -1; for (int i = 0; i < nums.size(); ++i) { if (nums[i] == 1) { ...
1
0
['Array', 'Two Pointers', 'C++']
0
semi-ordered-permutation
Easy C solution
easy-c-solution-by-aswath_1192-681a
IntuitionApproachComplexity Time complexity: Space complexity: Code
Aswath_1192
NORMAL
2025-01-04T14:11:55.693620+00:00
2025-01-04T14:11:55.693620+00:00
22
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
1
0
['C']
0
semi-ordered-permutation
Simple C solution
simple-c-solution-by-pavithrav25-tyac
IntuitionApproachComplexity Time complexity: Space complexity: Code
pavithrav25
NORMAL
2025-01-02T13:21:22.876917+00:00
2025-01-02T13:21:22.876917+00:00
13
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
1
0
['C']
0
semi-ordered-permutation
Easy Solution in C
easy-solution-in-c-by-sathurnithy-ft6o
Code
Sathurnithy
NORMAL
2024-12-31T10:17:26.985681+00:00
2024-12-31T10:17:26.985681+00:00
12
false
# Code ```c [] int semiOrderedPermutation(int* nums, int numsSize) { if(nums[0] == 1 && nums[numsSize - 1] == numsSize) return 0; int posOne = -1; int posSize = -1; for(int i=0; i<numsSize; i++){ if(nums[i] == 1) posOne = i; else if(nums[i] == numsSize) posSize = i; if(posOne ...
1
0
['C']
0
semi-ordered-permutation
Answer
answer-by-reddyshavva198-r2mx
\n# Code\npython3 []\nclass Solution:\n def semiOrderedPermutation(self, nums: List[int]) -> int:\n n=len(nums)\n mn, mx = nums.index(1) , nums
ReddyShavva198
NORMAL
2024-10-02T10:59:23.831956+00:00
2024-10-02T10:59:23.831989+00:00
18
false
\n# Code\n```python3 []\nclass Solution:\n def semiOrderedPermutation(self, nums: List[int]) -> int:\n n=len(nums)\n mn, mx = nums.index(1) , nums.index(n) \n return mn - mx + n - 1 - (mn > mx)\n```
1
0
['Python3']
0
semi-ordered-permutation
C++ could we grind 1 swap?
c-could-we-grind-1-swap-by-the_ghuly-s1cv
\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& nums) {\n int first = -1;\n int last = -1;\n for(int i = 0;i<nums.
the_ghuly
NORMAL
2024-06-21T10:12:04.171340+00:00
2024-06-21T10:12:04.171374+00:00
3
false
```\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& nums) {\n int first = -1;\n int last = -1;\n for(int i = 0;i<nums.size();++i)\n {\n if(nums[i]==1)\n first = i;\n if(nums[i] == nums.size())\n last = i;\n }\...
1
0
['C++']
0
semi-ordered-permutation
Simple java code 1 ms beats 100 %
simple-java-code-1-ms-beats-100-by-arobh-gega
\n# Complexity\n- \n\n# Code\n\nclass Solution {\n public int semiOrderedPermutation(int[] nums) {\n int x=-1,y=-1;\n for(int i=0;i<nums.length
Arobh
NORMAL
2024-01-23T04:45:46.533962+00:00
2024-01-23T04:45:46.534003+00:00
13
false
\n# Complexity\n- \n![image.png](https://assets.leetcode.com/users/images/233e2ae4-ae8d-426a-a792-2ad3e10cc547_1705985140.33696.png)\n# Code\n```\nclass Solution {\n public int semiOrderedPermutation(int[] nums) {\n int x=-1,y=-1;\n for(int i=0;i<nums.length;i++){\n if(nums[i]==1) x=i;\n ...
1
0
['Java']
0
semi-ordered-permutation
2hrs
2hrs-by-zaharkhach-fkni
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
ZaharKhach
NORMAL
2023-10-02T14:56:20.558341+00:00
2023-10-02T14:56:20.558362+00:00
32
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
0
['JavaScript']
0
semi-ordered-permutation
C++ solution
c-solution-by-prparaskar-j4ih
Intuition\n Describe your first thoughts on how to solve this problem. \nFor the given 0-indexed array, we are allowed to perform swap on any two adjacent eleme
prparaskar
NORMAL
2023-08-14T17:52:15.080828+00:00
2023-08-14T17:58:15.256005+00:00
77
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFor the given 0-indexed array, we are allowed to perform swap on any two adjacent elements unit we make array nums a semi-ordered permutation.\nwe can perform swap on element valued 1 till it reaches index 0.\nSimilarly, we perform swap o...
1
0
['C++']
1
semi-ordered-permutation
2 C++ solutions || Beginner-friendly approach || With and without vector.find()
2-c-solutions-beginner-friendly-approach-3ayq
\n# Code\n\n// Soution 1 (With find())\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& A) {\n int n = A.size();\n int i =
prathams29
NORMAL
2023-06-23T08:22:51.770588+00:00
2023-06-23T08:23:32.136769+00:00
36
false
\n# Code\n```\n// Soution 1 (With find())\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& A) {\n int n = A.size();\n int i = find(A.begin(), A.end(), 1) - A.begin(); \n int j = find(A.begin(), A.end(), n) - A.begin();\n return i + n - 1 - j - (i > j);\n }\n}\n\n// ...
1
0
['C++']
0
semi-ordered-permutation
simple code C++
simple-code-c-by-vips80091-zahi
\n# Complexity\n- Time complexity: O(n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(1)\n Add your space complexity here, e.g. O(n) \n\n
vips80091
NORMAL
2023-06-23T07:40:03.591728+00:00
2023-06-23T07:40:03.591752+00:00
15
false
\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& nums) {\n int start,end;\n int n = nums....
1
0
['C++']
0
semi-ordered-permutation
[C++] 2 Pointer Solution, 7ms, 46.3MB
c-2-pointer-solution-7ms-463mb-by-ajna2-pwkw
For this problem we have to just find the indexes of 1 and the value matching length of nums, then check how far they are from being in the first and in the las
Ajna2
NORMAL
2023-06-23T05:23:19.647653+00:00
2023-06-23T05:23:19.647684+00:00
16
false
For this problem we have to just find the indexes of `1` and the value matching length of `nums`, then check how far they are from being in the first and in the last position - with a caveat: if the first position is `>` than the last one, when we start swapping one of the indexes we will put the latter closer to its p...
1
0
['Array', 'Two Pointers', 'C++']
0
semi-ordered-permutation
simple logic!!
simple-logic-by-tanmay_jaiswal-bk3o
\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& nums) {\n int n = nums.size();\n int i = 0, j = 0;\n for (int x =
tanmay_jaiswal_
NORMAL
2023-06-11T17:52:01.932796+00:00
2023-06-11T17:52:01.932838+00:00
175
false
```\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& nums) {\n int n = nums.size();\n int i = 0, j = 0;\n for (int x = 0; x<n; x++) {\n if (nums[x] == 1) i = x;\n if (nums[x] == n) j = x;\n }\n\n if (j < i) return i + n-j-2;\n return...
1
0
['C++']
1
semi-ordered-permutation
Formula Based Approach
formula-based-approach-by-ridamexe-amzr
Code\n\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& nums) {\n int n=nums.size();\n int mx=0, mxidx=0;\n int mn=I
ridamexe
NORMAL
2023-06-11T05:33:15.170807+00:00
2023-06-11T05:33:15.170849+00:00
96
false
# Code\n```\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& nums) {\n int n=nums.size();\n int mx=0, mxidx=0;\n int mn=INT_MAX, mnidx;\n int count=0;\n\n for(int i=0; i<n; i++){\n mx=max(mx, nums[i]);\n if(nums[i]==mx) mxidx=i;\n\n ...
1
0
['C++']
1
semi-ordered-permutation
Easy, Clean, Python3 Solution 🔥
easy-clean-python3-solution-by-aadyatiwa-8loe
Intuition\n Describe your first thoughts on how to solve this problem. \nIt is a very straightforward approach, we can simply, easily simulate it.\n\n# Approach
aadyatiwari
NORMAL
2023-06-09T19:15:15.504169+00:00
2023-06-09T19:15:15.504210+00:00
79
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIt is a very straightforward approach, we can simply, easily simulate it.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nkeep track of the given indices of 1 and the last element. Perform swaps and calculate swaps...
1
1
['Array', 'Hash Table', 'Simulation', 'Python3']
0
semi-ordered-permutation
Super Easy Solution with Simple Logic || Store positions
super-easy-solution-with-simple-logic-st-kyla
Intuition\nSince we have to swap adjacent numbers to fulfil the given condition ,we will store the psoiton of 1 and the last number by iterating through loop. A
varunkusabi8
NORMAL
2023-06-05T12:12:52.944198+00:00
2023-06-05T12:21:37.998236+00:00
27
false
# Intuition\nSince we have to swap adjacent numbers to fulfil the given condition ,we will store the psoiton of 1 and the last number by iterating through loop. After the answer will be total number of swaps,hence we will add the number of swaps with the help of their positions.\nBut if the last number i.e. n is presen...
1
0
['Array', 'C++']
0
semi-ordered-permutation
Java Solution - Beats 100% (Using Recursion)
java-solution-beats-100-using-recursion-r0qym
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
ruch21
NORMAL
2023-06-04T16:01:07.027270+00:00
2023-06-04T16:01:07.027316+00:00
365
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
0
['Java']
1
semi-ordered-permutation
Easiest implementation
easiest-implementation-by-chaturvedipart-utjo
Complexity\n- Time complexity : O(n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity : O(1)\n Add your space complexity here, e.g. O(n) \n\n#
ChaturvediParth
NORMAL
2023-06-04T11:24:10.947815+00:00
2023-06-04T11:24:10.947856+00:00
24
false
# Complexity\n- Time complexity : $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity : $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& nums) {\n int n = nums.size();\n ...
1
0
['C++']
1
semi-ordered-permutation
STEP BY STEP EXPLANATION WITH EXAMPLE
step-by-step-explanation-with-example-by-pswi
Intuition\n Describe your first thoughts on how to solve this problem. \nWe just need to find the index of both the elements min element=1\nand maxelement=nums.
vijay__bhaskar
NORMAL
2023-06-04T09:29:46.977741+00:00
2023-06-04T15:06:03.629189+00:00
81
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**We just need to find the index of both the elements min element=1\nand maxelement=nums.size();**\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n# There are two cases only\n1. If location of max element is right...
1
0
['Array', 'Two Pointers', 'C++', 'Java']
0
semi-ordered-permutation
C++ || Simple Solution
c-simple-solution-by-deepak_2311-xdpf
\n# Complexity\n- Time complexity:O(n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(1)\n Add your space complexity here, e.g. O(n) \n\n#
godmode_2311
NORMAL
2023-06-04T06:02:03.137189+00:00
2023-06-04T06:02:03.137228+00:00
34
false
\n# Complexity\n- Time complexity:`O(n)`\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:`O(1)`\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& nums) {\n \n int n = nums.size();\n ...
1
0
['Array', 'Math', 'C++']
0
semi-ordered-permutation
Positions solution for C# (explanation+complexity)
positions-solution-for-c-explanationcomp-0hze
Approach\nThe idea is to find the positions of the element with a value 1 and the element with a value n.\n The first position is the number of iterations neede
deleted_user
NORMAL
2023-06-04T05:55:26.256096+00:00
2023-06-04T16:20:47.081797+00:00
93
false
# Approach\nThe idea is to find the positions of the element with a value 1 and the element with a value n.\n* The first position is the number of iterations needed to move an element from its current position to the zero position.\n* The (nums.length - second position) is the number of iterations needed to move an ele...
1
0
['C#']
0
semi-ordered-permutation
🤩easy understanding✅with intuition and approach and explanation✅
easy-understandingwith-intuition-and-app-7ydz
Intuition\n Describe your first thoughts on how to solve this problem. \nI got to reach the solution with the help of some examples\nYou may understand with the
peplup_world
NORMAL
2023-06-04T04:52:26.156844+00:00
2023-06-04T04:52:26.156885+00:00
377
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI got to reach the solution with the help of some examples\nYou may understand with the help of:-\nexample 1: arr1=[2,1,4,3]\n- first thing to observe is that the position of 1 and n are such that 1 appears before n\n- we see that we have...
1
0
['Math', 'Brainteaser', 'C++']
1
semi-ordered-permutation
2 LINER
2-liner-by-bariscan97-wz6n
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Bariscan97
NORMAL
2023-06-04T04:39:20.080403+00:00
2023-06-04T04:39:20.080439+00:00
227
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
0
['Array', 'Math', 'Python', 'Python3']
0
semi-ordered-permutation
Easy C++ Solution - Observation based
easy-c-solution-observation-based-by-baq-49js
\n\n# Code\n\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& nums) {\n \n int n = nums.size();\n if(nums[0] == 1 an
baquer
NORMAL
2023-06-04T04:29:42.980678+00:00
2023-06-04T04:29:42.980711+00:00
269
false
\n\n# Code\n```\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& nums) {\n \n int n = nums.size();\n if(nums[0] == 1 and nums[nums.size() - 1] == nums.size()) {\n return 0;\n }\n \n int pos1 = -1;\n int pos2 = -1;\n \n for(...
1
0
['C++']
0
semi-ordered-permutation
Simplest solution in java beats 💯✅
simplest-solution-in-java-beats-by-chint-7yec
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
ChinthaLalitha3
NORMAL
2023-06-04T04:28:30.856269+00:00
2023-06-04T04:28:30.856302+00:00
250
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
0
['Java']
0
semi-ordered-permutation
Easy C++ Solution
easy-c-solution-by-spidy007-dss3
\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& nums) {\n int idx,idx2;\n for(int i = 0;i<nums.size();i++){\n
spidy007
NORMAL
2023-06-04T04:26:00.548557+00:00
2023-06-04T04:26:00.548591+00:00
80
false
```\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& nums) {\n int idx,idx2;\n for(int i = 0;i<nums.size();i++){\n if(nums[i]==1) idx=i;\n if(nums[i]==nums.size()) idx2=i;\n }\n int ans=0;\n for(int i = idx;i>=0;i--){\n if(i==0) ...
1
0
['C']
0
semi-ordered-permutation
explained cpp solution
explained-cpp-solution-by-siya2323-29hg
Certainly! Here\'s an explanation of the code with comments:\n\ncpp\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& nums) {\n int
Siya2323
NORMAL
2023-06-04T04:20:18.894407+00:00
2023-06-04T04:20:18.894436+00:00
26
false
Certainly! Here\'s an explanation of the code with comments:\n\n```cpp\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& nums) {\n int n = nums.size(); // Get the size of the input vector\n int s = 0; // Initialize variable s to store the index of value 1\n int e = 0; // In...
1
0
['C++']
0
semi-ordered-permutation
semi order permutation
semi-order-permutation-by-nikhithnaini-p6bh
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
nikhithnaini
NORMAL
2023-06-04T04:18:59.206324+00:00
2023-06-04T04:18:59.206365+00:00
98
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->o(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$...
1
0
['C++']
0
semi-ordered-permutation
O(N) Approach || C++ ✅
on-approach-c-by-neergx-dg76
\n# Code\n\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& nums) {\n int n = nums.size();\n if(nums[0] == 1 && nums[n-1] =
neergx
NORMAL
2023-06-04T04:11:47.626050+00:00
2023-06-04T04:11:47.626088+00:00
17
false
\n# Code\n```\nclass Solution {\npublic:\n int semiOrderedPermutation(vector<int>& nums) {\n int n = nums.size();\n if(nums[0] == 1 && nums[n-1] == n)\n return 0;\n int lo = 0, hi = n-1;\n for(int i = 0;i < n; i++){\n if(nums[i] == 1){\n lo = i;\n ...
1
0
['C++']
0
semi-ordered-permutation
C++||Solutions🚀||Explained Line by Line ✔✔||Easy to understand😊||Beginner Friendly🚀✔✔
csolutionsexplained-line-by-line-easy-to-dpl2
Intuition\n Describe your first thoughts on how to solve this problem. \nThe code works based on the intuition that for a semi-ordered permutation, we need to b
shubh-30
NORMAL
2023-06-04T04:09:51.221005+00:00
2023-06-04T04:09:51.221051+00:00
166
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe code works based on the intuition that for a semi-ordered permutation, we need to bring the number 1 to the front of the array and the maximum number to the end. The number of elements between these two positions represents the elemen...
1
0
['C++']
1
semi-ordered-permutation
Super Easy C++ Solution
super-easy-c-solution-by-lotus18-u2gx
Code\n\nclass Solution \n{\npublic:\n int semiOrderedPermutation(vector<int>& nums) \n {\n int n=nums.size();\n if(nums[0]==1 && nums[n-1]==
lotus18
NORMAL
2023-06-04T04:06:20.253408+00:00
2023-06-04T04:06:20.253454+00:00
22
false
# Code\n```\nclass Solution \n{\npublic:\n int semiOrderedPermutation(vector<int>& nums) \n {\n int n=nums.size();\n if(nums[0]==1 && nums[n-1]==n) return 0;\n int swaps=0;\n int i=0;\n for(i=0; i<n; i++)\n {\n if(nums[i]==1) break;\n }\n for(int ...
1
0
['C++']
0
semi-ordered-permutation
java simple Solution
java-simple-solution-by-kashi_27-gxeb
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Kashi_27
NORMAL
2023-06-04T04:04:48.380961+00:00
2023-06-04T04:04:48.381012+00:00
19
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
0
['Java']
0
semi-ordered-permutation
Easy java fast || Brute force
easy-java-fast-brute-force-by-deveshpate-hgt2
\nclass Solution {\n public int semiOrderedPermutation(int[] nums) {\n int count=0;\n if(nums[0]==1&& nums[nums.length-1]==nums.length){\n
kanishkpatel1
NORMAL
2023-06-04T04:04:45.887259+00:00
2023-06-04T04:04:45.887299+00:00
47
false
```\nclass Solution {\n public int semiOrderedPermutation(int[] nums) {\n int count=0;\n if(nums[0]==1&& nums[nums.length-1]==nums.length){\n return 0;\n }\n int indst=0;\n int indend=0;\n for(int i=0;i<nums.length;i++){\n if(nums[i]==1){\n inds...
1
0
['Array', 'Java']
1
semi-ordered-permutation
Index of 1 and n || simple solution || Java
index-of-1-and-n-simple-solution-java-by-xcsc
Code\n\nclass Solution {\n public int semiOrderedPermutation(int[] nums) {\n int first = 0, last = 0, n = nums.length;\n for (int i = 0; i < n;
ByteMaster_
NORMAL
2023-06-04T04:04:01.655514+00:00
2023-06-04T04:04:01.655561+00:00
204
false
# Code\n```\nclass Solution {\n public int semiOrderedPermutation(int[] nums) {\n int first = 0, last = 0, n = nums.length;\n for (int i = 0; i < n; i++) {\n if(nums[i]==1){\n first=i;\n }\n if(nums[i]==n){\n last=i;\n }\n }\...
1
0
['Java']
1
semi-ordered-permutation
c++ easy Solution
c-easy-solution-by-madhukeshsingh-f48u
The given code represents a solution in C++ for the problem of finding the number of operations required to transform a given permutation of integers into a sem
MadhukeshSingh
NORMAL
2023-06-04T04:02:44.644390+00:00
2023-06-04T04:51:46.914346+00:00
78
false
The given code represents a solution in C++ for the problem of finding the number of operations required to transform a given permutation of integers into a semi-ordered permutation.\nThe variable n is assigned the size of the nums vector, which represents the length of the permutation.\n\nThe variables s and e are ini...
1
0
['C++']
0
semi-ordered-permutation
Simple JS Code!!!
simple-js-code-by-abhich930-2vim
IntuitionLets find out the index of 1 and n then we can follow the linear equations to find the number of total swaps needed.ApproachlengthOfNums: Stores the le
abhich930
NORMAL
2025-04-09T10:54:18.805274+00:00
2025-04-09T10:54:18.805274+00:00
2
false
# Intuition Lets find out the index of 1 and n then we can follow the linear equations to find the number of total swaps needed. # Approach lengthOfNums: Stores the length of the nums array. x: Will store the index of the number 1 in the nums array. y: Will store the index of the largest number in the array, which i...
0
0
['JavaScript']
0
semi-ordered-permutation
Easy peasy solution
easy-peasy-solution-by-abhinav_gupta0007-1hz9
IntuitionApproachComplexity Time complexity: Space complexity: Code
Abhinav_Gupta0007
NORMAL
2025-04-03T04:55:55.588946+00:00
2025-04-03T04:55:55.588946+00:00
1
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
0
0
['C++']
0
semi-ordered-permutation
Easy solution in java (O(n))
easy-solution-in-java-on-by-kumarakash-rpzp
IntuitionTake care of 1st and last value where it is present if suppose of index of 1 is i and index of n is j. and if i<j it means just we need to move 1 to 0
kumarakash
NORMAL
2025-04-02T08:17:58.621496+00:00
2025-04-02T08:17:58.621496+00:00
2
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> Take care of 1st and last value where it is present if suppose of index of 1 is i and index of n is j. and if i<j it means just we need to move 1 to 0 position and j to nth position. else (just we need to move 1 to 0 position and j to nt...
0
0
['Java']
0
semi-ordered-permutation
Easy Solution Beats 100% (C++)
easy-solution-beats-100-c-by-yl263258-w791
IntuitionFirst, find the indexes of the elements 1 and n, where n is the length of the array. Then, find the distance from index 0 to the index of 1, and the di
liam4021
NORMAL
2025-03-27T14:46:36.822100+00:00
2025-03-27T14:46:36.822100+00:00
3
false
# Intuition First, find the indexes of the elements $1$ and $n$, where $n$ is the length of the array. Then, find the distance from index $0$ to the index of $1$, and the distance between index $n$ and the index of $n$. # Approach One way to solve this is by iterating over the array to find the indexes of $1$ and $n$. ...
0
0
['C++']
0
semi-ordered-permutation
Simple solution in Java. Beats 100 %
simple-solution-in-java-beats-100-by-kha-5yip
Complexity Time complexity: O(n) Space complexity: O(1) Code
Khamdam
NORMAL
2025-03-21T15:08:16.715041+00:00
2025-03-21T15:08:16.715041+00:00
2
false
# Complexity - Time complexity: O(n) - Space complexity: O(1) # Code ```java [] class Solution { public int semiOrderedPermutation(int[] nums) { int n = nums.length; int min = nums[0]; int max = nums[0]; for (int num : nums) { min = Math.min(min, num); max =...
0
0
['Array', 'Java']
0
semi-ordered-permutation
🔥✅✅ Dart Solution 📌📌 || with Explanation 👌👌
dart-solution-with-explanation-by-nosar-9vef
Solution Find the indices of 1 and n: Locate the positions of 1 and n in the array. Calculate the number of swaps: The number of swaps required to move 1 to th
NosaR
NORMAL
2025-03-21T07:50:14.587362+00:00
2025-03-21T07:50:14.587362+00:00
1
false
# Solution 1. **Find the indices of `1` and `n`:** Locate the positions of `1` and `n` in the array. 2. **Calculate the number of swaps:** - The number of swaps required to move `1` to the first position is equal to its current index. - The number of swaps required to move `n` to the last position is equal to ...
0
0
['Dart']
0
semi-ordered-permutation
Python3 Program
python3-program-by-vaarshik_pemmasani-xe2c
Code
VAARSHIK_PEMMASANI
NORMAL
2025-02-22T18:25:43.987819+00:00
2025-02-22T18:25:43.987819+00:00
2
false
# Code ```python3 [] class Solution: def semiOrderedPermutation(self, nums: List[int]) -> int: count = 0 for i in range(len(nums)): if nums[i] == 1: while i > 0: nums[i], nums[i - 1] = nums[i - 1], nums[i] count += 1 ...
0
0
['Python3']
0
semi-ordered-permutation
EASY BY PRINCE GANESHWALA
easy-by-prince-ganeshwala-by-princeganes-5bfq
IntuitionApproachComplexity Time complexity: Space complexity: Code
PrinceGaneshwala
NORMAL
2025-02-19T16:23:41.480151+00:00
2025-02-19T16:23:41.480151+00:00
3
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
0
0
['Java']
0
semi-ordered-permutation
[C++] Simple Solution
c-simple-solution-by-samuel3shin-ng0j
Code
Samuel3Shin
NORMAL
2025-01-27T15:54:12.494146+00:00
2025-01-27T15:54:12.494146+00:00
2
false
# Code ```cpp [] class Solution { public: int semiOrderedPermutation(vector<int>& A) { int ans = 0; int idx = 0; for(int i=0; i<A.size(); i++) { if(A[i] == 1) { idx = i; break; } ans++; } while(A[0] != 1) { ...
0
0
['C++']
0
semi-ordered-permutation
semiOrderedPermutation: easy O(n) solution. BEAT 100%. ✅
semiorderedpermutation-easy-on-solution-8s4am
IntuitionThe problem involves making the given permutation semi-ordered with a minimum number of adjacent swaps. By finding the positions of 1 and n, we can cal
rTIvQYSHLp
NORMAL
2025-01-24T08:20:18.780428+00:00
2025-01-24T08:20:18.780428+00:00
2
false
# Intuition The problem involves making the given permutation semi-ordered with a minimum number of adjacent swaps. By finding the positions of 1 and n, we can calculate the necessary swaps. # Approach 1. Locate the positions of the numbers 1 and n in the permutation. 2. Calculate the number of swaps needed by conside...
0
0
['Array', 'C', 'Simulation']
0
semi-ordered-permutation
Track min/max value ids
track-minmax-value-ids-by-linda2024-enm7
IntuitionApproachComplexity Time complexity: Space complexity: Code
linda2024
NORMAL
2025-01-21T18:17:08.752880+00:00
2025-01-21T18:17:08.752880+00:00
2
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
0
0
['C#']
0
semi-ordered-permutation
Semi ordered permutation in C
semi-ordered-permutation-in-c-by-praveen-ot4a
Code
praveenkesava
NORMAL
2025-01-21T09:42:35.657970+00:00
2025-01-21T09:42:35.657970+00:00
1
false
# Code ```c [] int semiOrderedPermutation(int* nums, int numsSize) { int index1, index2; for (int i = 0; i < numsSize; i++) { if (nums[i] == 1) index1 = i; if (nums[i] == numsSize) index2 = i; } if (index1 < index2) return index1 + ((numsSize - 1) - inde...
0
0
['C']
0
semi-ordered-permutation
Easy (with comments)
easy-with-comments-by-vamsi0874-mkil
IntuitionApproachComplexity Time complexity: Space complexity: Code
vamsi0874
NORMAL
2025-01-21T06:53:37.772896+00:00
2025-01-21T06:53:37.772896+00:00
2
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
0
0
['TypeScript']
0
semi-ordered-permutation
Java beats 100%! very fast solution with explanation
java-beats-100-very-fast-solution-with-e-54cn
IntuitionWe need to move min and max elements to the start and to the end of the array.ApproachWe iterate over the array and search for max element. Then, we mo
had0uken
NORMAL
2025-01-20T14:26:50.201295+00:00
2025-01-20T14:26:50.201295+00:00
5
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> We need to move min and max elements to the start and to the end of the array. # Approach <!-- Describe your approach to solving the problem. --> We iterate over the array and search for max element. Then, we move element right and increme...
0
0
['Java']
0
semi-ordered-permutation
Easy solution
easy-solution-by-akshayya_ru-ldrt
IntuitionApproachComplexity Time complexity: Space complexity: Code
Akshayya_RU
NORMAL
2025-01-16T14:30:20.423886+00:00
2025-01-16T14:30:20.423886+00:00
2
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
0
0
['C++']
0
semi-ordered-permutation
Solution in C
solution-in-c-by-mariya_ashile-ztwj
Code
Mariya_Ashile
NORMAL
2025-01-13T10:15:34.222749+00:00
2025-01-13T10:15:34.222749+00:00
2
false
# Code ```c [] int semiOrderedPermutation(int* nums, int numsSize) { int n1=-1,n2=-1; for(int i=0;i<numsSize;i++) { if(nums[i]==1) n1=i; else if(nums[i]==numsSize) n2=i; if(n1!=-1 && n2!=-1) break; } return ((n1<n2) ? (n1 - n2 + numsSize-1) : (n1 - n2 + numsSize-2)); } ...
0
0
['C']
0
semi-ordered-permutation
Solution in C
solution-in-c-by-vishalkannan_070-ls3l
Approach Iterate through the array to find the index of 1and index of last element. Compare the indices "a" and "b" to determine if they are in the correct orde
VishalKannan_070
NORMAL
2025-01-09T17:25:54.269569+00:00
2025-01-09T17:25:54.269569+00:00
3
false
# Approach - Iterate through the array to find the index of `1`and index of last element. - Compare the indices "a" and "b" to determine if they are in the correct order `(a < b)` - If 1 is before numsSize, the number of swaps is the sum of "a" and the distance from "b" to the end of the array. - If 1 is after numsSize...
0
0
['Array', 'C']
0
semi-ordered-permutation
2717. Semi-Ordered Permutation
2717-semi-ordered-permutation-by-g8xd0qp-acaq
IntuitionApproachComplexity Time complexity: Space complexity: Code
G8xd0QPqTy
NORMAL
2025-01-06T17:39:10.252753+00:00
2025-01-06T17:39:10.252753+00:00
5
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
0
0
['JavaScript']
0
semi-ordered-permutation
Semi Ordered Permutation Solution in C
semi-ordered-permutation-solution-in-c-b-b6om
IntuitionApproachComplexity Time complexity: o(n) Space complexity: O(1) Code
Darsan20
NORMAL
2025-01-05T17:34:55.484518+00:00
2025-01-05T17:34:55.484518+00:00
2
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: o(n) - Space complexity: O(1) # Code ```c [] int semiOrderedPermutation(int* nums, int numsSize) { int one_index = 0, n_index = 0; ...
0
0
['C']
0
semi-ordered-permutation
Semi-ordered-permutation(In Java with Explaination and Example)
semi-ordered-permutationin-java-with-exp-85t0
Explaination Find the positions of 1 and n in the array (x and y). The position of 1 in nums (let's call it x). The position of n in nums (let's call it y). Cal
nishanthinimani525
NORMAL
2025-01-03T09:15:40.389678+00:00
2025-01-03T09:15:40.389678+00:00
11
false
# Explaination - Find the positions of 1 and n in the array (x and y). The **position of 1** in nums (let's call it x). The **position of n** in nums (let's call it y). - Calculate the swaps as: If x < y: Total swaps = x + (n - 1 - y). If x > y: Total swaps = x + (n - 1 - y) - 1. # Examples Input: nums = [2, 1, 4, 3] ...
0
0
['Java']
0
semi-ordered-permutation
Kotlin Straightforward Solution
kotlin-straightforward-solution-by-curen-l4ae
Code
curenosm
NORMAL
2025-01-01T16:48:03.764755+00:00
2025-01-01T16:48:03.764755+00:00
4
false
# Code ```kotlin [] class Solution { fun semiOrderedPermutation(nums: IntArray): Int { val (indexOne, indexN) = arrayOf(nums.indexOf(1), nums.indexOf(nums.size)) val res = indexOne + (nums.size - indexN - 1) return res - if (indexOne >= indexN) 1 else 0 } } ```
0
0
['Kotlin']
0
semi-ordered-permutation
Easy soln.
easy-soln-by-mamthanagaraju1-q6kw
IntuitionApproachComplexity Time complexity: Space complexity: Code
mamthanagaraju1
NORMAL
2025-01-01T12:51:51.431787+00:00
2025-01-01T12:51:51.431787+00:00
3
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
0
0
['C++']
0
semi-ordered-permutation
Easy Solution in Java
easy-solution-in-java-by-sathurnithy-fap8
Code
Sathurnithy
NORMAL
2024-12-31T10:27:23.756980+00:00
2024-12-31T10:27:23.756980+00:00
7
false
# Code ```java [] class Solution { public int semiOrderedPermutation(int[] nums) { if(nums[0] == 1 && nums[nums.length - 1] == nums.length) return 0; int posOne = -1, posN = -1; for(int i=0; i<nums.length; i++){ if(nums[i] == 1) posOne = i; else if(n...
0
0
['Java']
0
semi-ordered-permutation
Submission beat 100% of other submissions' runtime.
submission-beat-100-of-other-submissions-qgyw
IntuitionApproachComplexity Time complexity: Space complexity: Code
Vishva-mitra
NORMAL
2024-12-26T15:25:19.664315+00:00
2024-12-26T15:25:19.664315+00:00
8
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code `...
0
0
['Java']
0
semi-ordered-permutation
One Line
one-line-by-mrn3088-qhww
null
mrn3088
NORMAL
2024-12-10T21:26:53.343795+00:00
2024-12-10T21:26:53.343795+00:00
7
false
\n# Complexity\n- Time complexity: O(N) \nNotice here we used the := operator. \n- Space complexity: O(1)\n\n# Code\n```python3 []\nclass Solution:\n def semiOrderedPermutation(self, nums: List[int]) -> int:\n return max(-1 + 2 * ((i := min((t for t in enumerate(nums)), key=lambda x:x[1])[0]) - (j := max((t f...
0
0
['Python3']
0
semi-ordered-permutation
Go golang clean solution
go-golang-clean-solution-by-leaf_peng-tst0
go\n\nfunc semiOrderedPermutation(nums []int) int {\n var i, j int\n n := len(nums)\n for idx, num := range nums {\n if num == 1 {\n
leaf_peng
NORMAL
2024-11-24T15:04:02.329495+00:00
2024-11-24T15:04:02.329529+00:00
5
false
```go\n\nfunc semiOrderedPermutation(nums []int) int {\n var i, j int\n n := len(nums)\n for idx, num := range nums {\n if num == 1 {\n i = idx\n } else if num == n {\n j = idx\n }\n }\n if i < j {\n return i + (n - j - 1)\n }\n return i + (n - j - ...
0
0
['Go']
0
semi-ordered-permutation
beats 100% .Simple approach
beats-100-simple-approach-by-kumardixit8-3dy2
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
kumardixit88
NORMAL
2024-11-19T02:38:35.737631+00:00
2024-11-19T02:38:35.737657+00:00
3
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
0
['Java']
0