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; continue; } if((count($triplet) == 2)&&($val == ($k - $sum))){ array_push($triplet,$val); $sum += $val; continue; } } } if(($k == $sum) && (count($triplet) == 3)) { echo json_encode($triplet); echo "<br>"; $result++; } } 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/JM3ss
function name:  (null)
number of ops:  20
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   ECHO                                                     '%3Cbr%3E--------16---------%3Cbr%3E'
   35     1        INIT_FCALL                                               'counttriplets'
          2        SEND_VAL                                                 16
          3        SEND_VAL                                                 <array>
          4        DO_FCALL                                      0  $0      
          5        ECHO                                                     $0
   36     6        ECHO                                                     '%3Cbr%3E--------24---------%3Cbr%3E'
   37     7        INIT_FCALL                                               'counttriplets'
          8        SEND_VAL                                                 24
          9        SEND_VAL                                                 <array>
         10        DO_FCALL                                      0  $1      
         11        ECHO                                                     $1
   38    12        ECHO                                                     '%3Cbr%3E--------32---------%3Cbr%3E'
   39    13        INIT_FCALL                                               'counttriplets'
         14        SEND_VAL                                                 32
         15        SEND_VAL                                                 <array>
         16        DO_FCALL                                      0  $2      
         17        ECHO                                                     $2
   42    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'
   61    19      > RETURN                                                   1

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

End of function counttriplets

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
181.69 ms | 1407 KiB | 20 Q