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 1111735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:35:43+00:00 2026-05-17T02:35:43+00:00

I have a solution to the below problem in PHP. But it is taking

  • 0

I have a solution to the below problem in PHP.
But it is taking too much time to execute for 10 digit numbers. I want to know where am I going wrong ?
I am new to dynamic programming .Can someone have a look at this ?

Problem

In Byteland they have a very strange monetary system.

Each Bytelandian gold coin has an integer number written on it. A coin n
can be exchanged in a bank into three coins: n/2, n/3 and n/4.
But these numbers are all rounded down (the banks have to make a profit).

You can also sell Bytelandian coins for American dollars. The exchange
rate is 1:1. But you can not buy Bytelandian coins.

You have one gold coin. What is the maximum amount of American dollars
you can get for it?

========================================================

<?php 
$maxA=array();

function exchange($money)
{
        if($money == 0)
        {
               return $money;
        }
        if(isset($maxA[$money]))
        {
                $temp = $maxA[$money]; // gets the maximum dollars for N
        }
        else
        {
           $temp = 0;
        }
        if($temp == 0)
        {
            $m = $money/2;
            $m = floor($m);
            $o = $money/3;
            $o = floor($o);
            $n = $money/4;
            $n = floor($n);
            $total = $m+$n+$o;

       if(isset($maxA[$m]))
        {
             $m = $maxA[$m];

        }
        else
        {
           $m = exchange($m);
        }
        if(isset($maxA[$n]))
        {
         $n = $maxA[$n];
        }
        else
        {
           $n = exchange($n);
        }
        if(isset($maxA[$o]))
        {
           $o = $maxA[$o];
        }
        else
        {
           $o = exchange($o);
        }

       $temp = max($total,$m+$n+$o,$money);
       $maxA[$money]=$temp;  //store the value
        }

return $temp; 
}

$A=array();
while(1)
{
      $handle = fopen ("php://stdin","r");
      $line = fgets($handle);
      if(feof($handle))
      {
          break;
      }
      array_push($A,trim($line));
}

$count =count($A);
for($i=0;$i<$count;$i++)
{
      $val = exchange($A[$i]);
      print "$val \n";
 }
?>
  • 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-05-17T02:35:43+00:00Added an answer on May 17, 2026 at 2:35 am

    Here a reformatted version of the code for the ones (like I) who could understand the above. It doesn’t improve anything.

    function exchange($money) {
        static $maxA = array(0 => 0);
    
        if (isset($maxA[$money])) {
            return $money;
        }
    
        $m = floor($money/2);
        $o = floor($money/3);
        $n = floor($money/4);
        $total = $m+$n+$o;
    
        if (isset($maxA[$m])) {
            $m = $maxA[$m];
        } else {
            $m = exchange($m);
        }
    
        if (isset($maxA[$n])) {
            $n = $maxA[$n];
        } else {
            $n = exchange($n);
        }
        if (isset($maxA[$o])) {
            $o = $maxA[$o];
        } else {
            $o = exchange($o);
        }
    
        return $maxA[$money] = max($total, $m + $n + $o, $money);
    }
    

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

Sidebar

Related Questions

I have one solution to the subject problem, but it’s a hack and I’m
I have a challenging problem I've been battling for some time now. The Problem:
Help please, After numerous hours searching for the solution to my problem i have
I have two problems in my current .htaccess. The script below works but has
I have a PHP script (pasted below) which pulls in meta data from a
I have Solution with web site MyProject. I need publish this web site. In
Currently I have solution A that contains a domain layer base and solution B
I have a solution file with has more than 30 projects in it. Different
I have a solution to check NULL values extracted from object, However i feel
I have a Solution called Framework which is a C# assembly for my business

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.