3v4l.org

run code in 300+ PHP versions simultaneously
<?php function countTriplets($k,$array) { $result = 0; $compare = $array; foreach($compare as $K => $V){ $triplet = array(); $sum = $V; array_push($triplet,$V); foreach($array as $key => $val){ if(intval($key) > intval($K)){ if(($val < ($k - $sum)) && (count($triplet) == 1)){ array_push($triplet,$val); $sum += $val; } if((count($triplet) == 2)&&($val == ($k - $sum))){ array_push($triplet,$val); $sum += $val; } if(($k == $sum) && (count($triplet) == 3)) { echo json_encode($triplet); echo "<br>"; $result++; break; } } } } return $result; } echo "<br>--------16---------<br>"; echo countTriplets(16, [12, 3, 4, 1, 6, 9]); echo "<br>--------24---------<br>"; echo countTriplets(24,[12, 3, 4, 1, 6, 9]); echo "<br>--------32---------<br>"; echo countTriplets(32, [12, 3, 4, 1, 6, 9]); ?> <div class="panel-body"> <h1>Count of all triplets with constant sum</h1> <p>Given an array of numbers, write an algorithm to find all the triplets of the numbers whose sum is a constant <strong>K</strong></p> <p>Implement the placeholder function (in the code editor) which takes an input integer array and returns the count of triplets</p> <h2>Below are a few examples:</h2> <p><strong>Example 2:</strong> For the array <strong>[12, 3, 4, 1, 6, 9]</strong> and K = <strong>16</strong>, there are three triplet [ 12, 3, 1 ], [3, 4, 9] and [1, 6, 9] that sums to 16. And, it returns the count as <strong>2</strong></p> <p><strong>Example 2:</strong> For the array <strong>[12, 3, 4, 1, 6, 9]</strong> and K = <strong>24</strong>, there is only one triplet [ 12, 3, 9 ] that sums to 24. And, it returns the count as <strong>1</strong></p> <h2>Note</h2> <ul> <li>Implement the placeholder function</li> <li>First input is an integer constant</li> <li>Second input is an integer array</li> <li>If you choose C or C++, you have the third integer input which is the length of input array</li> <li>And the function returns an integer (count)</li> <li>Total number of elements in the input array can range from 5 to 10000</li> <li>Value of input array elements can range from 0 to 50000</li> <li>Output count is <span class="caps">NOT</span> a distinct triplets count. There can be duplicate triplets.</li> </ul> </div>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LJj4d
function name:  (null)
number of ops:  20
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   ECHO                                                     '%3Cbr%3E--------16---------%3Cbr%3E'
   34     1        INIT_FCALL                                               'counttriplets'
          2        SEND_VAL                                                 16
          3        SEND_VAL                                                 <array>
          4        DO_FCALL                                      0  $0      
          5        ECHO                                                     $0
   35     6        ECHO                                                     '%3Cbr%3E--------24---------%3Cbr%3E'
   36     7        INIT_FCALL                                               'counttriplets'
          8        SEND_VAL                                                 24
          9        SEND_VAL                                                 <array>
         10        DO_FCALL                                      0  $1      
         11        ECHO                                                     $1
   37    12        ECHO                                                     '%3Cbr%3E--------32---------%3Cbr%3E'
   38    13        INIT_FCALL                                               'counttriplets'
         14        SEND_VAL                                                 32
         15        SEND_VAL                                                 <array>
         16        DO_FCALL                                      0  $2      
         17        ECHO                                                     $2
   41    18        ECHO                                                     '%0A%3Cdiv+class%3D%22panel-body%22%3E%0A++++++++++++++++++++++%3Ch1%3ECount+of+all+triplets+with+constant+sum%3C%2Fh1%3E%0A%3Cp%3EGiven+an+array+of+numbers%2C+write+an+algorithm+to+find+all+the+triplets+of+the+numbers+whose+sum+is+a+constant+%3Cstrong%3EK%3C%2Fstrong%3E%3C%2Fp%3E%0A%3Cp%3EImplement+the+placeholder+function+%28in+the+code+editor%29+which+takes+an+input+integer+array+and+returns+the+count+of+triplets%3C%2Fp%3E%0A%3Ch2%3EBelow+are+a+few+examples%3A%3C%2Fh2%3E%0A%3Cp%3E%3Cstrong%3EExample+2%3A%3C%2Fstrong%3E+For+the+array+%3Cstrong%3E%5B12%2C+3%2C+4%2C+1%2C+6%2C+9%5D%3C%2Fstrong%3E+and+K+%3D+%3Cstrong%3E16%3C%2Fstrong%3E%2C+there+are+three+triplet+%5B+12%2C+3%2C+1+%5D%2C++%5B3%2C+4%2C+9%5D+and+%5B1%2C+6%2C+9%5D+that+sums+to+16.+And%2C+it+returns+the+count+as+%3Cstrong%3E2%3C%2Fstrong%3E%3C%2Fp%3E%0A%3Cp%3E%3Cstrong%3EExample+2%3A%3C%2Fstrong%3E+For+the+array+%3Cstrong%3E%5B12%2C+3%2C+4%2C+1%2C+6%2C+9%5D%3C%2Fstrong%3E+and+K+%3D+%3Cstrong%3E24%3C%2Fstrong%3E%2C+there+is+only+one+triplet+%5B+12%2C+3%2C+9+%5D+that+sums+to+24.+And%2C+it+returns+the+count+as+%3Cstrong%3E1%3C%2Fstrong%3E%3C%2Fp%3E%0A%3Ch2%3ENote%3C%2Fh2%3E%0A%3Cul%3E%0A%09%3Cli%3EImplement+the+placeholder+function%3C%2Fli%3E%0A%09%3Cli%3EFirst+input+is+an+integer+constant%3C%2Fli%3E%0A%09%3Cli%3ESecond+input+is+an+integer+array%3C%2Fli%3E%0A%09%3Cli%3EIf+you+choose+C+or+C%2B%2B%2C+you+have+the+third+integer+input++which+is+the+length+of+input+array%3C%2Fli%3E%0A%09%3Cli%3EAnd+the+function+returns+an+integer+%28count%29%3C%2Fli%3E%0A%09%3Cli%3ETotal+number+of+elements+in+the+input+array+can+range+from+5+to+10000%3C%2Fli%3E%0A%09%3Cli%3EValue+of+input+array+elements+can+range+from+0+to+50000%3C%2Fli%3E%0A%09%3Cli%3EOutput+count+is+%3Cspan+class%3D%22caps%22%3ENOT%3C%2Fspan%3E+a+distinct+triplets+count.+There+can+be+duplicate+triplets.%3C%2Fli%3E%0A%3C%2Ful%3E%0A++++++++++++++++++++%3C%2Fdiv%3E'
   60    19      > RETURN                                                   1

Function counttriplets:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 60
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 60
Branch analysis from position: 6
2 jumps found. (Code = 77) Position 1 = 14, Position 2 = 58
Branch analysis from position: 14
2 jumps found. (Code = 78) Position 1 = 15, Position 2 = 58
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 57
Branch analysis from position: 20
2 jumps found. (Code = 46) Position 1 = 23, Position 2 = 26
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 32
Branch analysis from position: 27
2 jumps found. (Code = 46) Position 1 = 35, Position 2 = 38
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 44
Branch analysis from position: 39
2 jumps found. (Code = 46) Position 1 = 46, Position 2 = 49
Branch analysis from position: 46
2 jumps found. (Code = 43) Position 1 = 50, Position 2 = 57
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 58
Branch analysis from position: 58
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 57
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 49
Branch analysis from position: 44
Branch analysis from position: 38
Branch analysis from position: 32
Branch analysis from position: 26
Branch analysis from position: 57
Branch analysis from position: 58
Branch analysis from position: 58
Branch analysis from position: 60
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 60
filename:       /in/LJj4d
function name:  countTriplets
number of ops:  63
compiled vars:  !0 = $k, !1 = $array, !2 = $result, !3 = $compare, !4 = $V, !5 = $K, !6 = $triplet, !7 = $sum, !8 = $val, !9 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    4     2        ASSIGN                                                   !2, 0
    5     3        ASSIGN                                                   !3, !1
    6     4      > FE_RESET_R                                       $12     !3, ->60
          5    > > FE_FETCH_R                                       ~13     $12, !4, ->60
          6    >   ASSIGN                                                   !5, ~13
    7     7        ASSIGN                                                   !6, <array>
    8     8        ASSIGN                                                   !7, !4
    9     9        INIT_FCALL                                               'array_push'
         10        SEND_REF                                                 !6
         11        SEND_VAR                                                 !4
         12        DO_ICALL                                                 
   10    13      > FE_RESET_R                                       $18     !1, ->58
         14    > > FE_FETCH_R                                       ~19     $18, !8, ->58
         15    >   ASSIGN                                                   !9, ~19
   11    16        CAST                                          4  ~21     !9
         17        CAST                                          4  ~22     !5
         18        IS_SMALLER                                               ~22, ~21
         19      > JMPZ                                                     ~23, ->57
   12    20    >   SUB                                              ~24     !0, !7
         21        IS_SMALLER                                       ~25     !8, ~24
         22      > JMPZ_EX                                          ~25     ~25, ->26
         23    >   COUNT                                            ~26     !6
         24        IS_EQUAL                                         ~27     ~26, 1
         25        BOOL                                             ~25     ~27
         26    > > JMPZ                                                     ~25, ->32
   13    27    >   INIT_FCALL                                               'array_push'
         28        SEND_REF                                                 !6
         29        SEND_VAR                                                 !8
         30        DO_ICALL                                                 
   14    31        ASSIGN_OP                                     1          !7, !8
   16    32    >   COUNT                                            ~30     !6
         33        IS_EQUAL                                         ~31     ~30, 2
         34      > JMPZ_EX                                          ~31     ~31, ->38
         35    >   SUB                                              ~32     !0, !7
         36        IS_EQUAL                                         ~33     !8, ~32
         37        BOOL                                             ~31     ~33
         38    > > JMPZ                                                     ~31, ->44
   17    39    >   INIT_FCALL                                               'array_push'
         40        SEND_REF                                                 !6
         41        SEND_VAR                                                 !8
         42        DO_ICALL                                                 
   18    43        ASSIGN_OP                                     1          !7, !8
   20    44    >   IS_EQUAL                                         ~36     !0, !7
         45      > JMPZ_EX                                          ~36     ~36, ->49
         46    >   COUNT                                            ~37     !6
         47        IS_EQUAL                                         ~38     ~37, 3
         48        BOOL                                             ~36     ~38
         49    > > JMPZ                                                     ~36, ->57
   22    50    >   INIT_FCALL                                               'json_encode'
         51        SEND_VAR                                                 !6
         52        DO_ICALL                                         $39     
         53        ECHO                                                     $39
   23    54        ECHO                                                     '%3Cbr%3E'
   24    55        PRE_INC                                                  !2
   25    56      > JMP                                                      ->58
   10    57    > > JMP                                                      ->14
         58    >   FE_FREE                                                  $18
    6    59      > JMP                                                      ->5
         60    >   FE_FREE                                                  $12
   31    61      > RETURN                                                   !2
   32    62*     > RETURN                                                   null

End of function counttriplets

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
161.42 ms | 1411 KiB | 20 Q