0% found this document useful (0 votes)
4 views6 pages

Array-7 Assemble Minions

The document presents a problem where Gru wants to assemble his minions at a single position, but they are scattered along the x-axis. Given the maximum range of a loudspeaker, the task is to determine if all minions can gather together based on their initial positions. The input format includes multiple test cases with the number of minions and their positions, and the output is 'YES' or 'NO' depending on whether assembly is possible.

Uploaded by

jj491bendreacc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Array-7 Assemble Minions

The document presents a problem where Gru wants to assemble his minions at a single position, but they are scattered along the x-axis. Given the maximum range of a loudspeaker, the task is to determine if all minions can gather together based on their initial positions. The input format includes multiple test cases with the number of minions and their positions, and the output is 'YES' or 'NO' depending on whether assembly is possible.

Uploaded by

jj491bendreacc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Array

Assemble the Minions


Practice Problems
Assignment Questions

Assignment Questions
Question Name: Assemble the minions

Problem Statement

Gru, the criminal mastermind wants to rob the Deshute Bank. To do that he hired N minions. The minions
randomly arranged themselves in front of the main gate of the bank but Gru wants all of them to be
assembled together. More precisely consider that the minions are arranged in the x-axis with each minion
initially standing at position Pi. Gru wants each of the minions to come at the same position.

But Gru is running out of time as the local police will arrive in few minutes. So he does not have the time
to go to each minion and say them to assemble together. So he decided to give a loudspeaker to one of
the minions who will make the announcement. But the loudspeaker has a maximum range of K so after
the announcement only those minions who are at a distance less than K from this minion will come to him.

Given the initial positions of the minions, you have to tell whether it is possible to assemble all the minions
or not.

Input Forma
First line contains T denoting the number of testcases
First line of every test case contains two space separated integers N and K denoting the number of
minions and maximum range of the loudspeaker respectively
Next line contains N space separated integers denoting the initial positions of the minions.

Output Forma
Print “YES” if it is possible to assemble all the minions else print “NO”

Constraint
1<=T<=
2<=N<=10
0<=K<=10
1<=Initial position of minions<=109

Sample Input 1

5 2

1 2 3 4 5

5 1

1 2 3 4 5

Sample Output 1

YES

NO

Assignment Questions

Explanation of Sample 1

In test case 1, give the loudspeaker to the 3rd minion.

Distance of minions 1 from 3rd minion = 3-1 = 2 <= 2

Distance of minion 2 from 3rd minion = 3-2 = 1 <= 2

Distance of minion 4 from 3rd minion = 4-3 = 1 <= 2

Distance of minion 5 from 3rd minion = 5-3 = 2 <= 2

In test case 2 it is not possible to assemble the minions.


Array
Assemble the Minions
Practice Problems
Assignment Solutions

Assignment Solutions

process.stdin.resume();

process.stdin.setEncoding('utf8');

let inputString = "";

let currentLine = 0;

process.stdin.on("data", (inputStdin) => {

inputString += inputStdin;

});

process.stdin.on("end", (_) => {

inputString = inputString

.trim()

.split("\n")

.map((string) => {

return string.trim();

});

main();

});

function readLine() {

return inputString[currentLine++];

function input() {

let n, k;

let input_arr2 = readLine()

.split(" ")

.map((x) => parseInt(x));

n = input_arr2[0];

k = input_arr2[1];

return solve(n,k)

function solve(n,k) {

let a = new Array(n + 1).fill(0);

let input_arr3 = readLine()

.split(" ")

.map((x) => parseInt(x));

for (let i = 1; i <= n; i++) {

a[i] = input_arr3[i - 1];

Assignment Solutions

a.sort((x, y) => x - y);

for (let i = 1; i <= n; i++) {

let te1 = a[i] + k;

let te2 = a[i] - k;

var is = 1;

if (!(a[1] >= te2)) is = 0;

if (!(a[n] <= te1)) is = 0;

if (is) return 1;

return 0;

/////////////

function main() {

let t = parseInt(readLine());

while (t--) {

if (input()) {

console.log("YES");

} else {

console.log("NO");

Github Link - https://github.com/TANAYTAPANSHU/DS-Assignment-Solutions/blob/main/

Assembletheminions.js

You might also like