Question Name:The Enlightened Ones

#include <bits/stdc++.h>
using namespace std;
int ok(int mid, int* pos, int K, int N)
{
	int i;
	int pr_ra=pos[0]+2*mid;
	for (i=1;i<N;i++)
	{
		
		if(pr_ra>=pos[i])
			continue;
		else
		{
			pr_ra=pos[i]+2*mid;
			K--;
		}
		if(!K)
			return 0;
	}
	return 1;
}
int main()
{
	int N, K;
	cin>>N>>K;
	int* pos=new int[N];
	int i;
	for(i=0;i<N;i++)
		cin>>pos[i];
	sort(pos,pos+N);
	int hi=10000000;
	int lo=0;
	int mid;
	while(lo<=hi)
	{
		mid=(lo+hi)/2;
		if(ok(mid,pos,K, N))
		{
			if(!ok(mid-1,pos,K, N))
			{
				break;
			}
			else
				hi=mid-1;
		}
		else
		{
			lo=mid+1;
		}
	}
	cout<<mid<<"\n";
	return 0;
}
  • Problem Description
    There are N temples in a straight line and K monks who want to spread their enlightening power to the entire road of temples. All the monks have an enlightenment value, which denotes the range of enlightenment which they can spread in both the directions. 

    Since, they do not want to waste their efficiency on trivial things of the world, they want to keep their range minimum.

    Also, when we say that the N temples are in a straight line, we mean that that all the temples lie on something like an X-axis in a graph.

    Find the minimum enlightenment value such that all the temples can receive it.

    Input Format:

    The first line contains two integers, N and K – denoting the number of temples and number of monks. 
    The next line contains N integers denoting the position of the temples in the straight line.

    Output format:
    Print the answer in a new line.

    Constraints:
    1 <= N <= 10^5
    1 <= K < N 
    1 <= Positioni <= 10^7

    Update: 
    The enlightenment value is an integer.
  • Test Case 1
    Input (stdin)3 2
    1 5 20
    Expected Output2
  • Test Case 2
    Input (stdin)4 2
    1 2 6 10
    Expected Output2

Leave a Reply

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

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.