Jump to content

jefstudent

New Members
  • Posts

    3
  • Joined

  • Last visited

jefstudent's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. 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);
  2. 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
  3. 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;
×
×
  • 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.