Skip to content
Related Articles

Related Articles

Find middle point segment from given segment lengths

View Discussion
Improve Article
Save Article
  • 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[0] represents a segment from 0 arr[0], value of arr[1] represents segment from arr[0] to arr[1], 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 <iostream>
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




<?php
// PHP ementation of the approach
 
// Function that returns the segment
// for the middle point
function 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 = 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
$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




<script>
// Javascript implementation of the approach
 
    // Function that returns the segment for the
    // middle point
    function findSegment( n, m ,segment_length) {
 
        // the middle point
        let meet_point = (1.0 * n) / 2.0;
        let sum = 0;
 
        // stores the segment index
        let segment_number = 0, i;
 
        for ( 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 ( 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    
    let n = 13;
    let m = 3;
    let segment_length =[ 3, 2, 8 ];
 
    let ans = findSegment(n, m, segment_length);
    document.write(ans);
 
// This code is contributed by Rajput-Ji
</script>

Output

3

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!