
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find middle point segment from given segment lengths in C++
In this problem, we are given an array arr[] of size m representing the lengths of line segments.
The line segments are from 0 to arr[0] , arr[0] to arr[1] and so on. Our task is to find the segment which is at the middle of all segments.
Let’s take an example to understand the problem,
Input
arr[] = {5, 7, 13}
Output
3
Explanation
Segments are : (0, 5) , (5, 12), (12, 25)
Solution Approach
To solve the problem, we will find the middle point of the line by (arrSum/2). If this middle point is a starting or ending point of a line segment then print -1. Else, print the segment number.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; int findSegmentWithMidPoint(int n, int m, int segment_length[]) { double centerPoint = (1.0 * n) / 2.0; int sum = 0; int segment = 0; for (int i = 0; i < m; i++) { sum += segment_length[i]; if ((double)sum == centerPoint) { segment = -1; break; } if (sum > centerPoint) { segment = i + 1; break; } } return segment; } int main() { int m = 3; int segment_length[] = { 5, 7, 13 }; int arrSum = segment_length[0]; for(int i = 0; i < m; i++) arrSum += segment_length[i]; int ans = findSegmentWithMidPoint(arrSum, m, segment_length); cout<<"The segment number where middle point lies is "<<ans; return 0; }
Output
The segment number where middle point lies is 3
- Related Articles
- In Question 4, point \( \mathrm{C} \) is called a mid-point of line segment \( \mathrm{AB} \). Prove that every line segment has one and only one mid-point.
- Show that of all line segments drawn from a given point not on it, the perpendicular line segment is the shortest.
- Construct a line segment Ab of length 8cm. From this, cut a line segment AC of length 3.6 cm. Measure the remaining line segment
- Find the distance of the point $(1, 2)$ from the mid-point of the line segment joining the points $(6, 8)$ and $(2, 4)$.
- Point $P( x,\ 4)$ lies on the line segment joining the points $A( -5,\ 8)$ and $B( 4,\ -10)$. Find the ratio in which point P divides the line segment AB. Also find the value of x.
- Find the mid point of the line segment joining the points $( -5,\ 7)$ and $( -1,\ 3)$.
- Find the mid point of the line segment joining the points $( 0,\ 0)$ and $( -2,\ -4)$.
- Find the mid point of the line segment joining the points $( 0,\ 0)$ and $( 2,\ 2)$.
- Segment Trees in Data Structure
- Draw any line segment, say $\overline{AB}$. Take any point C lying in between. Measure the lengths of AB, BC, and AC. Is $AB=AC+CB$?
- 8086 program to add the content of one segment to another segment
- Seven Segment LED Displays
- What Is Line Segment
- What is a Segment?
- Program to find area of a Circular Segment in C++

Advertisements