Maximum rods to put horizontally such that no two rods overlap on X coordinate
Last Updated :
22 Oct, 2021
Given two arrays X[] and H[] of size N, where X[i] denotes the X-coordinate of the ith vertical rod and H[i] denotes the height of that rod, and also given that an ith rod can also be put horizontally, by either placing the rod on the segment [X[i]-H[i], X[i]] or [X[i], X[i]+H[i]] on the X coordinate, the task is to find the maximum number of rods that can be put horizontally such that no two rods overlap on the X coordinate.
Examples:
Input: N = 3, X[] = {1, 2, 3}, H[] = {2, 5, 5}
Output: 2
Explanation:
One possible way to place the rods is:
- Put the rod at X[0]( = 1) horizontally on the left of X[0] i.e on the segment [-1, 1].
- Let the rod placed at position X[1]( = 2) be vertical.
- Put the rod at X[2]( = 3) horizontally on the right of X[2] i.e on the segment [3, 8].
Therefore, 2 rods can be put horizontally. And it is also the maximum possible count.
Input: N = 3, X[] = {1, 2, 5}, H[] = {1, 2, 5}
Output: 3
Approach: The problem can be solved by using the Greedy algorithm. Follow the steps below to solve the problem:
- Initialize two variables, say, ans as 0 to store the answer and prev as INT_MIN to store the last occupied point on X coordinate.
- Iterate over the range [0, N] using the variable i and perform the following steps:
- If the current rod can be put on the left, i.e if X[i]-H[i] is greater than prev, then increment the value of ans by 1 and update prev to X[i].
- Else, if the current rod can be put on the right, i.e if X[i]+H[i] is less than X[i+1], then increment the value of ans by 1 and update prev to X[i]+H[i].
- Else, update prev to X[i].
- Finally, after completing the above steps, print the answer obtained in ans.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMaximumPoints( int N, int X[], int H[])
{
int ans = 0;
int prev = INT_MIN;
for ( int i = 0; i < N; ++i) {
if (prev < (X[i] - H[i])) {
++ans;
prev = X[i];
}
else if (i == N - 1
|| (X[i] + H[i]) < X[i + 1]) {
++ans;
prev = X[i] + H[i];
}
else {
prev = X[i];
}
}
return ans;
}
int main()
{
int X[] = { 1, 2, 3 };
int H[] = { 2, 5, 5 };
int N = sizeof (X) / sizeof (X[0]);
cout << findMaximumPoints(N, X, H);
}
|
Java
class GFG {
public static int findMaximumPoints( int N, int X[], int H[]) {
int ans = 0 ;
int prev = Integer.MIN_VALUE;
for ( int i = 0 ; i < N; ++i) {
if (prev < (X[i] - H[i])) {
++ans;
prev = X[i];
}
else if (i == N - 1 || (X[i] + H[i]) < X[i + 1 ]) {
++ans;
prev = X[i] + H[i];
}
else {
prev = X[i];
}
}
return ans;
}
public static void main(String args[]) {
int X[] = { 1 , 2 , 3 };
int H[] = { 2 , 5 , 5 };
int N = X.length;
System.out.println(findMaximumPoints(N, X, H));
}
}
|
Python3
import sys
def findMaximumPoints(N, X, H):
ans = 0
prev = - sys.maxsize - 1
for i in range (N):
if (prev < (X[i] - H[i])):
ans + = 1
prev = X[i]
elif (i = = N - 1 or (X[i] + H[i]) < X[i + 1 ]):
ans + = 1
prev = X[i] + H[i]
else :
prev = X[i]
return ans
if __name__ = = '__main__' :
X = [ 1 , 2 , 3 ]
H = [ 2 , 5 , 5 ]
N = len (X)
print (findMaximumPoints(N, X, H))
|
C#
using System;
class GFG{
public static int findMaximumPoints( int N, int [] X, int [] H) {
int ans = 0;
int prev = Int32.MinValue;
for ( int i = 0; i < N; ++i) {
if (prev < (X[i] - H[i])) {
++ans;
prev = X[i];
}
else if (i == N - 1 || (X[i] + H[i]) < X[i + 1]) {
++ans;
prev = X[i] + H[i];
}
else {
prev = X[i];
}
}
return ans;
}
static public void Main()
{
int [] X = { 1, 2, 3 };
int [] H = { 2, 5, 5 };
int N = X.Length;
Console.WriteLine(findMaximumPoints(N, X, H));
}
}
|
Javascript
<script>
function findMaximumPoints(N, X, H)
{
let ans = 0;
let prev = Number.MIN_VALUE;
for (let i = 0; i < N; ++i) {
if (prev < (X[i] - H[i])) {
++ans;
prev = X[i];
}
else if (i == N - 1
|| (X[i] + H[i]) < X[i + 1]) {
++ans;
prev = X[i] + H[i];
}
else
{
prev = X[i];
}
}
ans++;
return ans;
}
let X = [1, 2, 3];
let H = [2, 5, 5];
let N = X.length;
document.write(findMaximumPoints(N, X, H));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...