jefstudent Posted October 12, 2013 Share Posted October 12, 2013 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] = 1A[1] = 5A[2] = 3A[3] = 4A[4] = 3A[5] = 4A[6] = 1A[7] = 2A[8] = 3A[9] = 4A[10] = 6A[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] = 1A[1] = 5A[2] = 3A[3] = 4A[4] = 3A[5] = 4A[6] = 1A[7] = 2A[8] = 3A[9] = 4A[10] = 6A[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; Quote Link to comment https://forums.phpfreaks.com/topic/282916-php-code-please-help/ Share on other sites More sharing options...
AbraCadaver Posted October 12, 2013 Share Posted October 12, 2013 Clearly stated problem (assignment), kind of like homework? Quote Link to comment https://forums.phpfreaks.com/topic/282916-php-code-please-help/#findComment-1453674 Share on other sites More sharing options...
jefstudent Posted October 12, 2013 Author Share Posted October 12, 2013 Well its one of the problem to improve programming skills. I tried over 3 days and still there are bugs in my code. I want to know where I am going wrong. Hope you can help me with that. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/282916-php-code-please-help/#findComment-1453675 Share on other sites More sharing options...
jefstudent Posted October 12, 2013 Author Share Posted October 12, 2013 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); Quote Link to comment https://forums.phpfreaks.com/topic/282916-php-code-please-help/#findComment-1453677 Share on other sites More sharing options...
GratianHarris Posted October 13, 2013 Share Posted October 13, 2013 It will save the value stored in your custom field on to $custom_field_value, then you can display, like this: echo $custom_field_value; Quote Link to comment https://forums.phpfreaks.com/topic/282916-php-code-please-help/#findComment-1453715 Share on other sites More sharing options...
ignace Posted October 13, 2013 Share Posted October 13, 2013 (edited) 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 October 13, 2013 by ignace added comments Quote Link to comment https://forums.phpfreaks.com/topic/282916-php-code-please-help/#findComment-1453717 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.