Created
March 29, 2017 18:13
-
-
Save ferbncode/2dc5b4391c14c7f9e1a064656129d722 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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