We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
int swapCount = 0, n = arr.Length;
for (int i = 0; i < n - 1; i++)
{
if (arr[i] != i + 1)
{
for (int j = i + 1; j < n ; j++)
{
if (arr[j] == i + 1)
{
swapCount++;
arr[j] = arr[i];
break;
}
}
}
}
return swapCount;
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Swaps 2
You are viewing a single comment's thread. Return to all comments →
I am having timeout problems. any suggestions?
int swapCount = 0, n = arr.Length; for (int i = 0; i < n - 1; i++) { if (arr[i] != i + 1) { for (int j = i + 1; j < n ; j++) { if (arr[j] == i + 1) { swapCount++; arr[j] = arr[i]; break; } }
} } return swapCount;