Posts

Lily's Homework-Whenever George asks Lily to hang out, she's busy doing homework-Hackerrank Solution

                  Lily's Homework Input Format The first line contains a single integer,  , denoting the number of elements in the array  .  The second line contains   space-separated integers describing the respective distinct values of  . Output Format Print the minimum number of swaps that should be performed in order to make the array  beautiful . Sample Input 4 2 5 3 1 Sample Output 2 Explanation Let's define array   to be the beautiful reordering of array  , as the sum of the absolute values of differences between its adjacent elements is minimal among all permutations and only two swaps (  with   and then   with  ) was performed. Solution in C++   #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std ; int main () { /* Enter your code here. Read input from STDIN. Print output to STDOUT */