**Lily's Homework**

### Problem Statement :

Whenever George asks Lily to hang out, she's busy doing homework. George wants to help her finish it faster, but he's in over his head! Can you help George understand Lily's homework so she can hang out with him? Consider an array of distinct integers, . George can swap any two elements of the array any number of times. An array is beautiful if the sum of among is minimal. Given the array , determine and return the minimum number of swaps that should be performed in order to make the array beautiful. Function Description Complete the lilysHomework function in the editor below. lilysHomework has the following parameter(s): int arr[n]: an integer array Returns int: the minimum number of swaps required Input Format The first line contains a single integer, n, the number of elements in arr. The second line contains n space-separated integers, arr[i].

### Solution :

` ````
Solution in C :
In C++ :
#include <bits/stdc++.h>
const int N = int(1e5) + 5;
int n, a[N], p[N];
bool used[N];
bool cmp(int i, int j) {
return a[i] < a[j];
}
int solve() {
memset(used, 0, sizeof(used));
int cur = 0;
for (int i = 0; i < n; ++i) {
int x = i;
if (used[x])
continue;
while (!used[x]) {
used[x] = true;
x = p[x];
}
cur++;
}
return n - cur;
}
int main() {
assert(scanf("%d", &n) == 1);
for (int i = 0; i < n; ++i) {
assert(scanf("%d", &a[i]) == 1);
assert(1 <= a[i] && a[i] <= int(2e9));
p[i] = i;
}
std::sort(p, p + n, cmp);
for (int i = 0; i + 1 < n; ++i)
assert(a[p[i]] != a[p[i + 1]]);
int res = solve();
std::reverse(p, p + n);
res = std::min(res, solve());
printf("%d\n", res);
return 0;
}
In Python3 :
def solution(a):
m = {}
for i in range(len(a)):
m[a[i]] = i
sorted_a = sorted(a)
ret = 0
for i in range(len(a)):
if a[i] != sorted_a[i]:
ret +=1
ind_to_swap = m[ sorted_a[i] ]
m[ a[i] ] = m[ sorted_a[i]]
a[i],a[ind_to_swap] = sorted_a[i],a[i]
return ret
raw_input()
a = [int(i) for i in raw_input().split(' ')]
asc=solution(list(a))
desc=solution(list(reversed(a)))
print min(asc,desc)
```

## View More Similar Problems

## Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →## Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →## Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →## Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →## Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →## Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →