Filling Stones

You are given an integer N. You have a total of 2N stones numbered from 1 to 2N. You have initially 2 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's 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.

That is the beauty of an Array is : 
|S1-S2|. 
Where S1 = ∑Ai1, 
where Ai1 are the elements positioned at the odd position in an array. 

And, 
S2 = ∑Ai2, 
where Ai2 are the elements positioned at an even position in an array. 


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

Example:

Input:  n=5
Output: 0

Approach

Java


public class FillingStones {
    public static void main(String[] args) {
        int n = 5;
        if (n == 1)
            System.out.println(2);
        else if (n == 2)
            System.out.println(1);
        else
            System.out.println(0);
    }

}

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n = 5;

    if (n == 1)
        cout << 2 << "\n";
    else if (n == 2)
        cout << 1 << "\n";
    else
        cout << 0 << "\n";

    return 0;
}


No comments:

Post a Comment