**Goodland Electricity**

### Problem Statement :

Goodland is a country with a number of evenly spaced cities along a line. The distance between adjacent cities is unit. There is an energy infrastructure project planning meeting, and the government needs to know the fewest number of power plants needed to provide electricity to the entire list of cities. Determine that number. If it cannot be done, return -1. You are given a list of city data. Cities that may contain a power plant have been labeled . Others not suitable for building a plant are labeled . Given a distribution range of , find the lowest number of plants that must be built such that all cities are served. The distribution range limits supply to cities where distance is less than k. Function Description Complete the pylons function in the editor below. pylons has the following parameter(s): int k: the distribution range int arr[n]: each city's suitability as a building site Returns int: the minimum number of plants required or -1 Input Format The first line contains two space-separated integers and , the number of cities in Goodland and the plants' range constant. The second line contains space-separated binary integers where each integer indicates suitability for building a plant. Output Format Print a single integer denoting the minimum number of plants that must be built so that all of Goodland's cities have electricity. If this is not possible for the given value of k , print -1.

### Solution :

` ````
Solution in C :
In C :
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,k,s,i,j,a,f,z=0;
scanf("%d %d",&n,&k);
int arr[n];
int brr[n];
for(i=0;i<n;i++){scanf("%d",&arr[i]);brr[i]=0;}
s=0;a=0;f=0;
while(a<n)
{
// printf("%d\n",a);
if(a+k-1>n-1&&f==0){a=n-k;f=1;}
for(i=a+k-1;i>=0;i--){
z++;
if(z>100000){printf("-1\n");return 0;}
if(arr[i]==2){printf("-1\n");return 0;}
if(arr[i]==1){
for(j=0;j<k;j++){
if(i+j<n)brr[i+j]=1;
if(i-j>=0)brr[i-j]=1;
}
arr[i]=2;a=i+k;break;}
}
}
for(i=0;i<n;i++){if(brr[i]!=1){printf("-1\n");return 0;}}
s=0;
for(i=0;i<n;i++){if(arr[i]==2){s++;}}
printf("%d\n",s);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
```

` ````
Solution in C++ :
In C++ :
#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }
int main() {
int n; int k;
while(~scanf("%d%d", &n, &k)) {
vector<int> a(n);
for(int i = 0; i < n; ++ i)
scanf("%d", &a[i]);
vi v;
rep(i, n) if(a[i] != 0)
v.push_back(i);
int j = 0;
int ans = 0;
for(int i = 0; i < n; ) {
for(; j + 1 < (int)v.size() && v[j + 1] <= i + k - 1; ++ j);
if(j == v.size() || i + k - 1 < v[j]) {
ans = INF;
break;
}
++ ans;
i = v[j] + k;
++ j;
}
printf("%d\n", ans == INF ? -1 : ans);
}
return 0;
}
```

` ````
Solution in Java :
In Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int[] towers = new int[n];
for (int i = 0; i < n; i++) {
towers[i] = sc.nextInt();
}
int i = 0;
int maxTower = k-1;
int ans = 0;
while (true) {
int nextTower = -1;
while (i <= maxTower && i < n) {
if (towers[i] == 1) {
nextTower = i;
}
i++;
}
if (nextTower == -1) {
System.out.println(-1);
return;
}
ans++;
i = nextTower + 1;
maxTower = nextTower+2*k-1;
if (nextTower+k>=n)
break;
}
System.out.println(ans);
}
}
```

` ````
Solution in Python :
In Python3 :
n, k = map(int, input().split())
tower = list(map(int, input().split())) # 0 - no tower; 1 - tower
def voisins(i):
return range(min(n-1, i+k-1), max(-1, i-k), -1)
count = 0
past_fourni = 0
while past_fourni < n:
for i in voisins(past_fourni):
if tower[i]:
past_fourni = i + k
count += 1
break
else:
count = -1
break
print(count)
```

## View More Similar Problems

## Tree Coordinates

We consider metric space to be a pair, , where is a set and such that the following conditions hold: where is the distance between points and . Let's define the product of two metric spaces, , to be such that: , where , . So, it follows logically that is also a metric space. We then define squared metric space, , to be the product of a metric space multiplied with itself: . For

View Solution →## Array Pairs

Consider an array of n integers, A = [ a1, a2, . . . . an] . Find and print the total number of (i , j) pairs such that ai * aj <= max(ai, ai+1, . . . aj) where i < j. Input Format The first line contains an integer, n , denoting the number of elements in the array. The second line consists of n space-separated integers describing the respective values of a1, a2 , . . . an .

View Solution →## Self Balancing Tree

An AVL tree (Georgy Adelson-Velsky and Landis' tree, named after the inventors) is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. We define balance factor for each node as : balanceFactor = height(left subtree) - height(righ

View Solution →## Array and simple queries

Given two numbers N and M. N indicates the number of elements in the array A[](1-indexed) and M indicates number of queries. You need to perform two types of queries on the array A[] . You are given queries. Queries can be of two types, type 1 and type 2. Type 1 queries are represented as 1 i j : Modify the given array by removing elements from i to j and adding them to the front. Ty

View Solution →## Median Updates

The median M of numbers is defined as the middle number after sorting them in order if M is odd. Or it is the average of the middle two numbers if M is even. You start with an empty number list. Then, you can add numbers to the list, or remove existing numbers from it. After each add or remove operation, output the median. Input: The first line is an integer, N , that indicates the number o

View Solution →## Maximum Element

You have an empty sequence, and you will be given N queries. Each query is one of these three types: 1 x -Push the element x into the stack. 2 -Delete the element present at the top of the stack. 3 -Print the maximum element in the stack. Input Format The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each

View Solution →