Filling stones

4.3

6 votes
Algorithms, Basics of Greedy Algorithms, Greedy Algorithms
Problem

You are given an integer N. You have total 2N stones numbered from 1 to 2N. You have initially two empty arrays of size N each. You have to fill both the arrays utilizing all the 2N stones and using each stone in only one array.

Let us define the beauty of an array as the difference between the sum of elements at odd positions and the sum of elements at even positions.

The beauty of an array is |S1S2| where:

  • S1=Ai1 where Ai1 are the elements positioned at an odd position in an array
  • S2=Ai2 where Ai2 are the elements positioned at an even position in an array 

Your task is to arrange the stones in both arrays such that the product of the beauty of both arrays is as minimum as possible.

Input format 

A single integer representing N (0<N<109)

Output format 

Print that minimum possible product of the beauty of those two arrays.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Since N = 5, we have stones 1,2,3,4,5,6,7,8,9,10.

We can arrange the stones in this way

Array 1: [1,2,3,7,5] 
Here, the beauty = |(1+3+5)-(2+7)| = |9-9| = 0
So beauty = 0
 
Array 2 : [6,4,8,9,10] : 
Here, the beauty = |(6+8+10)-(4+9)| = |24-13| = 11
So beauty = 11

So that the Product is minimum possible. 
Product = 0*11 = 0

Editor Image

?