1

There is a technique by which the list of (N+1) bit codes can be generated from (N)-bit codes.

  1. Take the list of N bit codes in the given order and call it List-N
  2. Reverse the above list (List-N), and name the new reflected list: Reflected-List-N
  3. Prefix each member of the original list (List-N) with 0 and call this new list 'A'
  4. Prefix each member of the new list (Reflected-List-N) with 1 and call this new list 'B'
  5. The list of codes with N+1 bits is the concatenation of Lists A and B.

A Demonstration of the above steps: Generating the list of 3-bit Mystery Codes from 2-Bit Mystery Codes

  1. 2-bit list of codes: 00, 01, 11, 10
  2. Reverse/Reflect the above list: 10, 11, 01, 00
  3. Prefix Old Entries with 0: 000, 001, 011, 010
  4. Prefix Reflected List with 1: 110, 111, 101, 100
  5. Concatenate the lists obtained in the last two steps: 000, 001, 011, 010, 110, 111, 101, 100

But when I enter 65 as input getting error:

PHP Fatal error: Out of memory (allocated 538968064) in $result[]=$prefix.$value; line code

:: Code ::

<?php
function MysteryCodes($input){
    $initArr=array(0,1);

    $list=array();
    $list[0]=$initArr;
    for ($i=1; $i<$input; $i++)
    {
        $prevIndex=$i-1;
        $reflectedListN = array_reverse($list[$prevIndex]);
        $listA=prefixR($list[$prevIndex], '0');
        $listB=prefixR($reflectedListN, '1');
        $list[$i]=array_merge($listA, $listB);
    }
    return array_slice($list[$input-1], -$input);
}

function prefixR($input, $prefix){
    $result=array();
    foreach($input as $value){
        $result[]=$prefix.$value;
    }
    return $result;
}

$inp = 5;
if($inp>=1 && $inp<=65){
    $result=MysteryCodes($inp);
    $output="";
    foreach($result as $key=>$value)
        $output.="$value\n";

    echo $output;
}

Also is there any other way to overcome by changing code or logic for the same.

Thanks in advance.

1

1 Answer 1

0

For $inp = 65 your resulting array would contain 2^65 distinct binary values. You store each value as a string, which is not the most efficient way of storing binary digits. You could, probably, convert strings to integers for storing in an array and then back to string for calculations (reversing, prefixing) - but you would soon run into integer overflow - for values larger than 2^32 on 32-bit PHP and 2^64 on 64-bit PHP - see docs - values would then be converted to float type and you'll lose precision.

So you could be able to squeeze more values into the memory, but you'll definitely reach the point where regardless of what you do, the array just wouldn't fit into the memory_limit.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your replay. What you have said i understand but how can i resolved the issues? any hint will be appreciated.
Apart from cramming more data into memory by using more efficient data storage structures you could also have the vast majority of the data in a file, and read into memory only small parts. This would, however, impose a huge speed penalty on the calculation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.