# Find middle point segment from given segment lengths

• Difficulty Level : Easy
• Last Updated : 10 Aug, 2022

Given an array arr[] of size M. The array represents segment lengths of different sizes. These segments divide a line beginning with 0. The value of arr represents a segment from 0 arr, value of arr represents segment from arr to arr, and so on.
The task is to find the segment which contains the middle point, If the middle segment does not exist, print ‘-1’.
Examples:

Input: arr = {3, 2, 8}
Output:
The three segments are (0, 3), (3, 5), (5, 13)
middle point is 6.5 which is in the 3rd segment.
Input: arr = {3, 2, 5}
Output: -1
Middle point is 5 which is between segments 2 and 3.

Approach:

The middle point will always be N / 2. Now, check in which segment does this point exist and print the segment number. If it is the starting or ending for any segment then print ‘-1’.

Below is the implementation of the above approach:

## C++

 `// C/C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function that returns the segment for the``// middle point``int` `findSegment(``int` `n, ``int` `m, ``int` `segment_length[])``{` `    ``// the middle point``    ``double` `meet_point = (1.0 * n) / 2.0;``    ``int` `sum = 0;` `    ``// stores the segment index``    ``int` `segment_number = 0;` `    ``for` `(``int` `i = 0; i < m; i++) {` `        ``// increment sum by``        ``// length of the segment``        ``sum += segment_length[i];` `        ``// if the middle is``        ``// in between two segments``        ``if` `((``double``)sum == meet_point) {``            ``segment_number = -1;``            ``break``;``        ``}` `        ``// if sum is greater``        ``// than middle point``        ``if` `(sum > meet_point) {``            ``segment_number = i + 1;``            ``break``;``        ``}``    ``}` `    ``return` `segment_number;``}` `// Driver code``int` `main()``{``    ``int` `n = 13;``    ``int` `m = 3;``    ``int` `segment_length[] = { 3, 2, 8 };` `    ``int` `ans = findSegment(n, m, segment_length);``    ``cout << (ans);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``class` `GFG {` `    ``// Function that returns the segment for the``    ``// middle point``    ``static` `int` `findSegment(``int` `n, ``int` `m,``                           ``int``[] segment_length)``    ``{` `        ``// the middle point``        ``double` `meet_point = (``1.0` `* n) / ``2.0``;``        ``int` `sum = ``0``;` `        ``// stores the segment index``        ``int` `segment_number = ``0``;` `        ``for` `(``int` `i = ``0``; i < m; i++) {` `            ``// increment sum by``            ``// length of the segment``            ``sum += segment_length[i];` `            ``// if the middle is``            ``// in between two segments``            ``if` `((``double``)sum == meet_point) {``                ``segment_number = -``1``;``                ``break``;``            ``}` `            ``// if sum is greater``            ``// than middle point``            ``if` `(sum > meet_point) {``                ``segment_number = i + ``1``;``                ``break``;``            ``}``        ``}` `        ``return` `segment_number;``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `n = ``13``;``        ``int` `m = ``3``;``        ``int``[] segment_length = ``new` `int``[] { ``3``, ``2``, ``8` `};` `        ``int` `ans = findSegment(n, m, segment_length);``        ``System.out.println(ans);``    ``}``}`

## Python3

 `# Python 3 implementation of the approach` `# Function that returns the segment for the``# middle point`  `def` `findSegment(n, m, segment_length):``    ``# the middle point``    ``meet_point ``=` `(``1.0` `*` `n) ``/` `2.0``    ``sum` `=` `0` `    ``# stores the segment index``    ``segment_number ``=` `0` `    ``for` `i ``in` `range``(``0``, m, ``1``):``        ``# increment sum by``        ``# length of the segment``        ``sum` `+``=` `segment_length[i]` `        ``# if the middle is``        ``# in between two segments``        ``if` `(``sum` `=``=` `meet_point):``            ``segment_number ``=` `-``1``            ``break` `        ``# if sum is greater``        ``# than middle point``        ``if` `(``sum` `> meet_point):``            ``segment_number ``=` `i ``+` `1``            ``break` `    ``return` `segment_number`  `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ``n ``=` `13``    ``m ``=` `3``    ``segment_length ``=` `[``3``, ``2``, ``8``]` `    ``ans ``=` `findSegment(n, m, segment_length)``    ``print``(ans)``# This code is contributed by``# Surendra_Gangwar`

## C#

 `// C# implementation of the approach``using` `System;``class` `GFG {` `    ``// Function that returns the``    ``// segment for the middle point``    ``static` `int` `findSegment(``int` `n, ``int` `m,``                           ``int``[] segment_length)``    ``{` `        ``// the middle point``        ``double` `meet_point = (1.0 * n) / 2.0;``        ``int` `sum = 0;` `        ``// stores the segment index``        ``int` `segment_number = 0;` `        ``for` `(``int` `i = 0; i < m; i++) {` `            ``// increment sum by``            ``// length of the segment``            ``sum += segment_length[i];` `            ``// if the middle is``            ``// in between two segments``            ``if` `((``double``)sum == meet_point) {``                ``segment_number = -1;``                ``break``;``            ``}` `            ``// if sum is greater``            ``// than middle point``            ``if` `(sum > meet_point) {``                ``segment_number = i + 1;``                ``break``;``            ``}``        ``}` `        ``return` `segment_number;``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main()``    ``{``        ``int` `n = 13;``        ``int` `m = 3;``        ``int``[] segment_length = ``new` `int``[] { 3, 2, 8 };` `        ``int` `ans = findSegment(n, m, segment_length);``        ``Console.WriteLine(ans);``    ``}``}` `// This code is contributed``// by shs`

## PHP

 ` ``\$meet_point``)``        ``{``            ``\$segment_number` `= ``\$i` `+ 1;``            ``break``;``        ``}``    ``}` `    ``return` `\$segment_number``;``}` `// Driver code``\$n` `= 13;``\$m` `= 3;``\$segment_length` `= ``array``( 3, 2, 8 );` `\$ans` `= findSegment(``\$n``, ``\$m``,``                   ``\$segment_length``);``echo` `(``\$ans``);``    ` `// This code is contributed by ajit``?>`

## Javascript

 ``

Output

`3`

Time Complexity: O(m), for traversal
Auxiliary Space: O(1), as no extra space is required

My Personal Notes arrow_drop_up