# Fair Cut

### Problem Statement :

```Li and Lu have n integers, a1,a2, ...,an, that they want to divide fairly between the two of them. They decide that if Li gets integers with indices I = {i1, i2, ...,  ik} (which implies that Lu gets integers with indices J = {1, 2, ...,n}\I), then the measure of unfairness of this division is:
f(I) =  Σ(i∈I) Σ(j∈J)|ai-aj|
Find the minimum measure of unfairness that can be obtained with some division of the set of integers where Li gets exactly k integers.

Note A\B means Set complement

Input Format

The first line contains two space-separated integers denoting the respective values of  n(the number of integers Li and Lu have) and k (the number of integers Li wants).
The second line contains n space-separated integers describing the respective values of a1, a2, ..., an.

Constraints

1 <= k < n < =3000
1 <= ai <= 10^9
For 15% of the test cases, n <=20.
For 45% of the test cases, n <=40.
Output Format

Print a single integer denoting the minimum measure of unfairness of some division where Li gets k integers.```

### Solution :

```                            ```Solution in C :

In C++ :

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long ll;

const ll Inf = 4000000000000000000;
const int Maxn = 3005;

int n, k;
int a[Maxn];
ll dp[Maxn][Maxn];

int main() {
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
sort(a, a + n);
fill((ll*)dp, (ll*)dp + Maxn * Maxn, Inf);
dp[0][0] = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j <= i && j <= k; j++) if (dp[i][j] < Inf) {
int my = j, his = i - j;
if (my < k) {
ll add = ll(a[i]) * (his - (n - k - his));
dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + add);
}
if (his < n - k) {
ll add = ll(a[i]) * (my - (k - my));
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + add);
}
}
printf("%lld\n", dp[n][k]);
return 0;
}

In Java :

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
private static long[] DIFF;
private static long[] DATA;
private static int COUNT;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
COUNT = sc.nextInt();
DATA = new long[n];
for(int i = 0 ; i < n ; i++) {
DATA[i] = sc.nextLong();
}

COUNT = Math.min(DATA.length - COUNT, COUNT);
DIFF = new long[DATA.length];
System.out.println(calc2());
}

private static long calc2() {
Arrays.sort(DATA);

long[] sum = new long[DATA.length + 1];
for (int i = 1; i <= DATA.length; i++) {
sum[i] = sum[i - 1] + DATA[i - 1];
}

for (int i = 0; i < DATA.length; i++) {
for (long aT : DATA) {
DIFF[i] += Math.abs(DATA[i] - aT);
}
}

return Math.min(clc(0), clc(1));
}

private static long clc(int i) {
long chSum = 0;
long summ0 = 0;

for (int j = 0; j < DATA.length; j++) {
if (!valid(j, i)) {
continue;
}
chSum += DATA[j];

for (int m = 0; m < DATA.length; m++) {
if (!valid(m, i)) {
summ0 += Math.abs(DATA[j] - DATA[m]);
}
}
}

long min = summ0;
int first = i;
int next = 2 * COUNT + i;
while (next < DATA.length) {

chSum -= DATA[first];

summ0 -= DIFF[first];
summ0 += 2 * (chSum - DATA[first] * (COUNT - 1));

summ0 += DIFF[next];
summ0 -=  2 * (DATA[next] * (COUNT - 1) - chSum);

chSum += DATA[next];

min = Math.min(summ0, min);
next += 2;
first += 2;
}

return min;
}

private static boolean valid(int j, int i) {
return j % 2 == i && j < COUNT * 2;
}
}

In C :

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int n;
int k;
int a[3001];
long long listcost[3001];
long long allcost[3001];

int main()
{
int i;
int j;
int besti;
long long bestscore;
long long allscore;
long long tmp;

scanf("%d %d", &n, &k);
if (k > n - k) {
k = n - k;
}
for (i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
for (i = 1; i < n; ++i) {
tmp = a[i];
j = i;
while (j) {
if (tmp > a[j-1]) {
break;
}
a[j] = a[j-1];
j -= 1;
}
a[j] = tmp;
}
for (i = 0; i < n; ++i) {
listcost[i] = 0;
allcost[i] = 0;
}
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
if (a[i] > a[j]) {
allcost[i] += a[i] - a[j];
} else {
allcost[i] += a[j] - a[i];
}
}
}
allscore = 0;
bestscore = 0;
for (i = 0; i < k; ++i) {
allscore = bestscore;
besti = 0;
bestscore = 1000LL*1000LL*1000LL*1000LL*1000LL;
for (j = 0; j < n; ++j) {
tmp = allcost[j];
tmp -= listcost[j];
tmp -= listcost[j];
tmp += allscore;
if (tmp < bestscore) {
bestscore = tmp;
besti     = j;
}
if (tmp == bestscore) {
if (listcost[j] > listcost[besti]) {
besti = j;
}
}
}
tmp = allcost[besti];
allcost[besti] = allcost[n - 1];
allcost[n - 1] = tmp;
tmp = listcost[besti];
listcost[besti] = listcost[n - 1];
listcost[n - 1] = tmp;
tmp = a[besti];
a[besti] = a[n - 1];
a[n - 1] = (int) tmp;
n -= 1;
j = besti;
while (j < n - 1) {
if (a[j] < a[j+1]) {
break;
}
tmp = allcost[j];
allcost[j] = allcost[j+1];
allcost[j+1] = tmp;
tmp = listcost[j];
listcost[j] = listcost[j+1];
listcost[j+1] = tmp;
tmp = a[j];
a[j] = a[j+1];
a[j+1] = (int) tmp;
j += 1;
}
// update listcost
for (j = 0; j < n; ++j) {
if (a[j] > a[n]) {
listcost[j] += a[j] - a[n];
} else {
listcost[j] += a[n] - a[j];
}
}
}
printf("%lld\n", bestscore);

return 0;
}

In python3 :

import sys

def solve(a, k):
def sumup(I, J):
sm = 0
for u in I:
for v in J:
sm += abs(u - v)
return sm
k = min(k, len(a) - k)
a.sort()
print('k=%d a=%s' % (k, a), file = sys.stderr)
index = (len(a) - 2 * k) // 2
I = a[index:index + 2 * k:2]
J = a[:index] + a[index + 1:index + 2 * k + 1:2] + a[index + 2 * k:]
print('I=%s J=%s' % (I, J), file = sys.stderr)
return sumup(I, J)

_, k = map(int, input().split())
a = list(map(int, input().split()))
print(solve(a, k))```
```

## Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer

## Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

## 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

## 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.

## 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

## 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