Skip to content

Instantly share code, notes, and snippets.

@ferbncode
Created March 29, 2017 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ferbncode/2dc5b4391c14c7f9e1a064656129d722 to your computer and use it in GitHub Desktop.
Save ferbncode/2dc5b4391c14c7f9e1a064656129d722 to your computer and use it in GitHub Desktop.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int tests;
scanf("%d", &tests);
for(int t=0;t<tests;t++)
{
int n;
scanf("%d", &n);
int arr[n];
// Variables indexmin and indexmax are for the indexes of the maximum and
// and the minimum elements.
int indexmin, indexmax;
int minele = 99999999, maxele = -1;
// Find the min, max elements and their respective indices.
for(int i=0;i<n;i++)
{
scanf("%d", &arr[i]);
if(arr[i] > maxele)
{
indexmax = i;
maxele = arr[i];
}
if(arr[i] <= minele)
{
indexmin = i;
minele = arr[i];
}
}
int count = 0;
int flag = 0;
// If they are at an intersection, there would be one less swap. Consider example 2
// in example test cases.
if(indexmin < indexmax)
flag = 1;
count += (indexmax - 0);
count += ((n-1) - indexmin);
count -= flag;
cout << count << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment