Connecting Soldiers | CodeChef Solution

Hello coders, today we are going to solve Connecting Soldiers CodeChef Solution whose Problem Code is NOKIA.

Connecting Soldiers

Task

To protect people from evil, a long and tall wall was constructed a few years ago. But just a wall is not safe, there should also be soldiers on it, always keeping vigil. The wall is very long and connects the left and the right towers. There are exactly N spots (numbered 1 to N) on the wall for soldiers. The Kth spot is K miles far from the left tower and (N+1-K) miles from the right tower.

Given a permutation of spots P of {1, 2, …, N}, soldiers occupy the N spots in that order. The P[i]th spot is occupied before the P[i+1]th spot. When a soldier occupies a spot, he is connected to his nearest soldier already placed to his left. If there is no soldier to his left, he is connected to the left tower. The same is the case with right side. A connection between two spots requires a wire of length equal to the distance between the two.

The realm has already purchased a wire of M miles long from Nokia, possibly the wire will be cut into smaller length wires. As we can observe, the total length of the used wire depends on the permutation of the spots P. Help the realm in minimizing the length of the unused wire. If there is not enough wire, output -1.

Input Format

First line contains an integer T (number of test cases, 1 ≤ T ≤ 10 ). Each of the next T lines contains two integers N M, as explained in the problem statement (1 ≤ N ≤ 30 , 1 ≤ M ≤ 1000).

Output Format

For each test case, output the minimum length of the unused wire, or -1 if the the wire is not sufficient.

Example

Sample Input

4
3 8
3 9
2 4
5 25

Sample Output

0
0
-1
5

Explanation

In the 1st case, for example, the permutation P = {2, 1, 3} will use the exact 8 miles wires in total.

In the 2nd case, for example, the permutation P = {1, 3, 2} will use the exact 9 miles wires in total.

In the 3rd case, the minimum length of wire required is 5, for any of the permutations {1,2} or {2,1}, so length 4 is not sufficient.

In the 4th case, for the permutation {1, 2, 3, 4, 5} we need the maximum length of the wire = 20. So minimum possible unused wire length = 25 – 20 = 5.

Solution – Connecting Soldiers

C++

#include <iostream>
using namespace std;

int minLength(int low, int high){
    if(low >= high || high - low == 1){
        return 0;
    }
    
    int mid = low + (high - low)/2;
    return (high - low) + minLength(low,mid) + minLength(mid,high);
}

int main() {
	// your code goes here
	int t;
	cin>>t;
	
	while(t--){
	    int n,m;
	    cin>>n>>m;
	    
	    int low = 0;
	    int high = n+1;
	    
	    int ans = minLength(low,high);
	    
	    if(ans > m){
	        cout<<-1<<endl;
	    }else if(ans <= m && m <= (n*(n+3)/2)){
            cout << 0 << endl;
        }else{
	        cout<<m - (n*(n+3)/2)<<endl;
	    }
	}
	
	return 0;
}

Python

# recursive prog to find min length of used wire

def minLength (l, r):
    # it is a recursive function
    m = int(l + (r - l)/2)
    if m <= 0 or r - l == 1:
        return 0
    return r - l + minLength(l, m) + minLength(m, r)


def nokia (n, m):
    # calculate min and max length of the used wire
    max_len = ((3 * n) + (n * n)) / 2
    min_len = minLength(0, n + 1)
    if m < min_len:
        return -1
    if m >= max_len:
        return m - max_len
    if m < max_len and m >= min_len:
        return 0
    
t = int(input())
for i in range(t):
    line = input()
    n, m = line.split(' ')
    n = int(n)
    m = int(m)
    print(int(nokia(n, m)))

Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		sc.nextLine();
		while(t-->0)
		{
		    int n=sc.nextInt();
		    int m=sc.nextInt();
		    if(t!=0)
		    sc.nextLine();
		    int max=(n+1)*(n+2);
		    max/=2;
		    max--;
		    //System.out.println(max);
		    if(m>=max)
		    {
		        System.out.println(m-max);
		        continue;
		    }
		    else
		     {
		         
		         int h=func(n);
		         //System.out.print(h+" ");
		         if(m<h)
		         System.out.println("-1");
		         else
		         System.out.println("0");
		     }
		     
		   
		}
	}
	
	static int func(int n)
	{
	    HashSet<Integer> h=new HashSet<Integer>();
	    if(n==0)
	    {
	        
	       return 0;
	    }
	    if(n==1)
	    {
	        
	       
	         return 2;
	    }
	    if(n==2)
	    {
	       
	         return 5;
	    }
	    if(n==3)
	    {
	        return 8;
	    }
	    return n+1+func((n-1)/2)+func(n/2);
	}
}

Disclaimer: The above Problem (Connecting Soldiers) is generated by CodeChef but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *