Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9156123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:42:55+00:00 2026-06-17T12:42:55+00:00

I have an array: $data = array( 1 => array( time => 1, parent

  • 0

I have an array:

$data = array(
  1 => array(
    "time" => 1,
    "parent" => array(4)
  ),
  2 => array(
    "time" => 3,
    "parent" => array(4, 5)
  ),
  3 => array(
    "time" => 2,
    "parent" => array(6)
  ),
  4 => array(
    "time" => 1,
    "parent" => array(6)
  ),
  5 => array(
    "time" => 1,
    "parent" => array(4)
  ),
  6 => array(
    "time" => 1,
    "parent" => array()
  )
);

Key is the ID of an element, parent is an array of elements, which refers to element id and time is just an integer.

This is an illustrated example of a given array:

Schema

The integer on the bottom-left is the “id” and the integer in the middle is “time”.

My goal here is find the most time-consuming path of this array. In the given example, the path would be 2->5->4->6 (id wise) resulting in 6 “time” overall. It looks pretty easy on paper, however I can’t really seem to code an algorythm to get the elements of the most time-consuming path. I would appreciate any kind of help.

I think the algorythm should be kind of bruteforce-ish and check through all of the options available. Thus with the given array it would go like:

1 -> 4 -> 6 = 3
2 -> 4 -> 6 = 5
2 -> 5 -> 4 -> 6 = 6 
3 -> 6 = 3
4 -> 6 = 2
5 -> 4 -> 6 = 3

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:42:56+00:00Added an answer on June 17, 2026 at 12:42 pm

    Note that this will only work if there are no loops in the array.

    // Note: built this in the SO editor, might have bugs
    $cached = [];
    $arrays = []; // Do this yourself
    
    function get_path($num) {
        global $arrays, $cached;
        if (isset($cached[$num])) return $cached[$num];
    
        $array = $arrays[$num];
        $maxtime = $array['time'];
        $bestpath = array($num);
        foreach ($array['parent'] as $i) {
            $path = get_path($i);
            if ($path['time']+$array['time'] > $maxtime) {
                $maxtime = $path['time'] + $array['time'];
                $bestpath = array_merge(array($num),$path['path']);
            }
        }
    
        $cached[$num] = array('path' => $bestpath, 'time' => $maxtime);
        return $cached[$num];
    }
    
    var_dump(get_path(5));
    

    Not really a bruteforce way, should be close enough to O(n). The basic idea is that you just cache the paths it can take.

    Note: I used a bit of C-style syntax here, but ideally you wouldn’t actually write the code like this

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this array Array ( [data] => Array ( [0] => Array (
I have this array {$man_data} which is structured like 10 > 'Text 8' 14
I have a long array of data ( n entities). Every object in this
I have array of strings, String[] data and it's 10 elements has value P
I have a problem related to an array data which fetched from an external
I have an array of point data (for particles) which constantly changes size. To
I have an array that contains data as follows (string and time data separated
I have data contained in an array which is like so, $file['info']['files'] = array(
I have some array data returned from an ajax call consisting of an image
Let say I have an array like: Array ( [0] => Array ( [Data]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.