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 */
int n;
cin >> n;
int arr[n];
for(int i=0;i<n;i++){
cin >> arr[i];
}
int count=0;
int pos_min,pos_max;
for(int j=0;j<=(n/2);j++){
int min=arr[j];
int max=arr[j];
for(int i=j+1; i<n-j; i++){
if(arr[i]<min){
pos_min=i;
}
if(arr[i]>max){
pos_max=i;
}
}
if(arr[j]!=min){
swap(arr[j],arr[pos_min]);
count++;
}
if(arr[j]!=max){
swap(arr[n-1-j],arr[pos_max]);
count++;
}
}
for(int i=0;i<n;i++){
cout << arr[i];
}
return 0;
}
Thanks me later..
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 .
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 */ int n; cin >> n; int arr[n]; for(int i=0;i<n;i++){ cin >> arr[i]; } int count=0; int pos_min,pos_max; for(int j=0;j<=(n/2);j++){ int min=arr[j]; int max=arr[j]; for(int i=j+1; i<n-j; i++){ if(arr[i]<min){ pos_min=i; } if(arr[i]>max){ pos_max=i; } } if(arr[j]!=min){ swap(arr[j],arr[pos_min]); count++; } if(arr[j]!=max){ swap(arr[n-1-j],arr[pos_max]); count++; } } for(int i=0;i<n;i++){ cout << arr[i]; } return 0; }
Thanks me later..
Dude Something is wrong in your code .
ReplyDeleteIts always gives 0 as output.
We can solve this question by using 2 maps and 4 arrays. 2 arrays to store original array and 1 array for storing ascendingly sorted array and 1 array for storing descendingly sorted array.
ReplyDeleteOne thing we need to notice is that for an array to be beautiful it should be sorted (either in ascending or descending order).
To determine the minimum number of swaps which are required to convert the given array into sorted one we can use maps.
Our answer would be minimum of the count of swaps taken to convert the array into ascending or descending order.
For more detailed explanation I would suggest you to watch this video by alGOds:
Link : https://youtu.be/W8oGaAEOeRU