Find middle point segment from given segment lengths

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: 3
The three segments are (0, 3), (3, 5), (5, 13)
middle point is 6.5 which is in the 3rd segement.

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

3


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.