Jump to content

PHP code please help


jefstudent

Recommended Posts

HI Everyone, 

 

I am trying to solve following problem but not getting correct results. Hope you can help.

I have gone past the first problem as in to search how many numbers in the given array are bigger than its neighbour numbers given in the array. 

 

The second part where i need to calculate the flags script is not giving correct results. following is the task plus my tried code.

 

A non-empty zero-indexed array A consisting of N integers is given. A peak is an array element which is larger than its neighbours. More precisely, it is an index P such that 0 < P < N − 1 and A[P − 1] < A[P] > A[P + 1].

For example, the following array A:

 

A[0] = 1
A[1] = 5
A[2] = 3
A[3] = 4
A[4] = 3
A[5] = 4
A[6] = 1
A[7] = 2
A[8] = 3
A[9] = 4
A[10] = 6
A[11] = 2

 

has exactly four peaks: elements 1, 3, 5 and 10.

You are going on a trip to a range of mountains whose relative heights are represented by array A. You have to choose how many flags you should take with you. The goal is to set the maximum number of flags on the peaks, according to certain rules.

Flags can only be set on peaks. What's more, if you take K flags, then the distance between any two flags should be greater than or equal to K. The distance between indices P and Q is the absolute value |P − Q|.

For example, given the mountain range represented by array A, above, with N = 12, if you take:

You can therefore set a maximum of three flags in this case.

  • two flags, you can set them on peaks 1 and 5;
  • three flags, you can set them on peaks 1, 5 and 10;
  • four flags, you can set only three flags, on peaks 1, 5 and 10.

Write a function:

that, given a non-empty zero-indexed array A of N integers, returns the maximum number of flags that can be set on the peaks of the array.

For example, given N = 12 and the following array A:

 

A[0] = 1
A[1] = 5
A[2] = 3
A[3] = 4
A[4] = 3
A[5] = 4
A[6] = 1
A[7] = 2
A[8] = 3
A[9] = 4
A[10] = 6
A[11] = 2

 

the function should return 3, as explained above.

Assume that:

  • N is an integer within the range [1..100,000];
  • each element of array A is an integer within the range [0..1,000,000,000].

 

$p=0;
 
 $peak = array(); 
 $counter = 0;
 
 $arr_tot = count($A);
 
 $mid= $arr_tot / 2; 
 
    // 1st half of array 
  for ($i=1; $i<$mid-1;$i++)
  {
    if($A[$i] > $A[$i-1] && $A[$i] > $A[$i+1])
 {
        $peak[$p] = $i; 
 
        $p++;
 
}
 
  
  }
  
    // second half of array
  for ($m=$mid-1; $m<$arr_tot-1 ; $m++)
       {
   if($A[$m] > $A[$m-1] && $A[$m] > $A[$m+1])
{
 $peak[$p] = $m; 
  
           $p++;
}
  }
 
 
  if(!(is_array($peak)) || $p == 1 )
    {
$counter = 1;
     return $counter;
     }else if ( $p == 2)
             { 
             $counter = 2;
 
return $counter;
              }
     
 
   $sFlag=$peak[0]; 
   $eFlag=isset($peak[$p]);   
   
  //  469  4 & 6
  //  158  1 , 5 , 8
  // 1 3 5 10
  // 2 7 
  $check = 'false';
   for($h=0;$h<$p;$h++)
       {
   
if($h>=1){
       if( abs($peak[$h] - $peak[$h - 1] ) >= $p )
    {
$counter++;
 
}else if ($check == 'false' && $h!=3 ||  $check == 'false' && ($h - 1) == 0)
              $counter++;
 
else
$check='false';
}
 
}
 
  
    
 
echo "<br/>" . $counter;    
 
 

 

 

Link to comment
Share on other sites

See following are the inputs array for $A . If its successful for 3 but fails for the last one.

 

//$A = array(9, 9, 4, 3, 5, 4, 5, 2, 8, 9, 3, 1);  //got 3 expt 2
  //       0 ,1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11   // Should be getting 4 , 6 , 9 for the first 2 search loops and then $counter should be = 2
 
//$A = array (1,5,3,4,3,4,1,2,3,4,6,2);     Should be getting 1 3 5 10 for the first 2 search loops and then $counter should be = 3
          //0,1,2,3,4,5,6,7,8,9,10,11            // 1 3 5 10   should be 3 
 
//$A = array(4, 5, 8, 5, 1, 4, 6, 8, 7, 2, 2, 5);   //expected 2
         //  0 ,1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11    // 2 7 
 
//$A = array(5, 9, 6, 2, 2, 7, 3, 2, 7, 4, 7, 9);   // 1 5 8 
     //      0 ,1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11
 
//$A = array(7, 10, 4, 5, 7, 4, 6, 1, 4, 3, 3, 7);
Link to comment
Share on other sites

function getPeaks($array) {
if (($count = count($array)) < 3 ) {
// not enough neighbours
return false;
}
$peaks = array();
// $i starts at the second element, $max points to the last element that still has a neighbour
// array(
// 0 => ..
// 1 => .. <-- $i
// 2 => .. <-- $max has neighbours 1 and 3
// 3 => ..
// )
for ($i = 1, $max = $count - 2; $i <= $max; $i++) {
if ($array[$i - 1] < $array[$i] && $array[$i + 1] < $array[$i]) {
$peaks[] = $i; // both neighbours of $i are smaller
}
}
return $peaks;
}

function getFlags($array) {
// number of peaks is the required distance between flags
$distance = $count = count($array);
if ($count < 2) {
return false;
}
// flag positions, add first array element
$positions = array($array[0]);
// $i is current element position
// $j is last flag position to compare against
for ($i = 1, $j = 0, $max = $count; $i < $max; $i++) {
if (abs($array[$j] - $array[$i]) >= $distance) {
$positions[] = $array[$i];
$j = $i; // move position to new flag position
}
}
return $positions;
}

header('Content-Type: text/plain');
$peaks = getPeaks(array(1,5,3,4,3,4,1,2,3,4,6,2));

echo 'Peaks:', PHP_EOL;
var_dump($peaks);

echo 'Flag positions:', PHP_EOL;
var_dump(getFlags($peaks));
Edited by ignace
added comments
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.