How To Use For Loops To Create Different Size Towers Java
Minimum number of towers required such that every house is in the range of at to the lowest degree one tower
Given a map of the city and the network range, the task is to determine the minimum number of the tower so that every firm is within range of at to the lowest degree one tower. Each tower must be installed on pinnacle of an existing firm.
Examples:
Input: range : 1 house : i ii three four five Output: 2 Input: range : two house : seven 2 4 six 5 nine 12 11 Output: 3
All cities can exist covered by inserting 2 towers i.e. at firm 2 and iv.
All cities tin can exist covered by inserting 3 towers i.e. at firm 4, 9, and 12.
Algorithm:-
- First, sort all the elements.
- Count only in one case and then traverse till its eye business firm.
- Afterward this again traverse till tower range.
- Again echo 1, 2, iii steps till all the houses are covered.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using
namespace
std;
int
number_of_tower(
int
firm[],
int
range,
int
n)
{
sort(firm, house + due north);
int
numOfTower = 0;
int
i = 0;
while
(i < north) {
numOfTower++;
int
loc = house[i] + range;
while
(i < n && house[i] <= loc)
i++;
--i;
loc = house[i] + range;
while
(i < n && firm[i] <= loc)
i++;
}
return
numOfTower;
}
int
primary()
{
int
house[] = { vii, two, 4, six, 5, 9, 12, 11 };
int
range = 2;
int
n =
sizeof
(house) /
sizeof
(firm[0]);
cout << number_of_tower(business firm, range, n);
}
Coffee
import
java.util.Arrays;
public
class
Improve {
static
int
number_of_tower(
int
business firm[],
int
range,
int
north)
{
Arrays.sort(firm);
int
numOfTower =
0
;
int
i =
0
;
while
(i < n) {
numOfTower++;
int
loc = house[i] + range;
while
(i < n && house[i] <= loc)
i++;
--i;
loc = house[i] + range;
while
(i < north && firm[i] <= loc)
i++;
}
render
numOfTower;
}
public
static
void
main(String args[])
{
int
business firm[] = {
7
,
2
,
4
,
six
,
5
,
ix
,
12
,
eleven
};
int
range =
2
;
int
due north = house.length;
Organization.out.println(number_of_tower(house, range, north));
}
}
Python 3
def
number_of_tower(house, r, n):
business firm.sort()
numOfTower
=
0
i
=
0
while
(i < northward) :
numOfTower
+
=
1
loc
=
house[i]
+
r
while
(i < northward
and
firm[i] <
=
loc):
i
+
=
ane
i
-
=
1
loc
=
house[i]
+
r
while
(i < n
and
firm[i] <
=
loc):
i
+
=
ane
render
numOfTower
if
__name__
=
=
"__main__"
:
house
=
[
seven
,
2
,
4
,
6
,
5
,
9
,
12
,
11
]
r
=
2
due north
=
len
(business firm)
impress
(number_of_tower(house, r, northward))
C#
using
Organisation;
public
class
Better {
static
int
number_of_tower(
int
[]house,
int
range,
int
north)
{
Assortment.Sort(firm);
int
numOfTower = 0;
int
i = 0;
while
(i < n) {
numOfTower++;
int
loc = business firm[i] + range;
while
(i < n && firm[i] <= loc)
i++;
--i;
loc = house[i] + range;
while
(i < due north && firm[i] <= loc)
i++;
}
return
numOfTower;
}
public
static
void
Main()
{
int
[]house = { 7, 2, 4, 6, five, 9, 12, eleven };
int
range = two;
int
n = house.Length;
Console.WriteLine(number_of_tower(house, range, northward));
}
}
PHP
<?php
function
number_of_tower(
$house
,
$range
,
$north
)
{
sort(
$house
);
$numOfTower
= 0;
$i
= 0;
while
(
$i
<
$n
) {
$numOfTower
++;
$loc
=
$house
[
$i
] +
$range
;
while
(
$i
<
$n
&&
$house
[
$i
] <=
$loc
)
$i
++;
--
$i
;
$loc
=
$house
[
$i
] +
$range
;
while
(
$i
<
$n
&&
$house
[
$i
] <=
$loc
)
$i
++;
}
return
$numOfTower
;
}
$house
=
assortment
( 7, 2, 4, half dozen, 5, 9, 12, 11 );
$range
= 2;
$n
= sizeof(
$house
) / sizeof(
$house
[0]);
echo
number_of_tower(
$house
,
$range
,
$n
);
?>
Javascript
<script>
role
number_of_tower(business firm,range,due north)
{
house.sort(
part
(a,b){
render
a-b;});
permit numOfTower = 0;
let i = 0;
while
(i < north) {
numOfTower++;
permit loc = house[i] + range;
while
(i < n && business firm[i] <= loc)
i++;
--i;
loc = house[i] + range;
while
(i < n && house[i] <= loc)
i++;
}
return
numOfTower;
}
let house=[7, 2, 4, 6, 5, 9, 12, eleven];
let range = 2;
let n = house.length;
document.write(number_of_tower(house, range, n));
</script>
Time Complexity: O(nlogn)
Space Complication: O(1)
How To Use For Loops To Create Different Size Towers Java,
Source: https://www.geeksforgeeks.org/minimum-number-of-towers-required-such-that-every-house-is-in-the-range-of-at-least-one-tower/
Posted by: lewisgoicame.blogspot.com
0 Response to "How To Use For Loops To Create Different Size Towers Java"
Post a Comment