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>

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0130.00618.30
8.3.50.0110.00416.75
8.3.40.0130.00618.63
8.3.30.0110.00318.71
8.3.20.0090.00020.04
8.3.10.0090.00023.53
8.3.00.0060.00319.56
8.2.180.0120.00616.50
8.2.170.0080.00818.70
8.2.160.0060.01322.96
8.2.150.0080.00025.66
8.2.140.0040.00424.66
8.2.130.0060.00326.16
8.2.120.0110.00020.50
8.2.110.0070.00321.12
8.2.100.0130.00019.64
8.2.90.0000.00817.78
8.2.80.0040.00420.55
8.2.70.0040.00418.16
8.2.60.0040.00417.93
8.2.50.0060.00318.22
8.2.40.0040.00422.33
8.2.30.0000.00719.46
8.2.20.0000.00818.14
8.2.10.0040.00418.07
8.2.00.0000.00718.21
8.1.280.0130.00725.92
8.1.270.0090.00022.06
8.1.260.0080.00026.35
8.1.250.0040.00428.09
8.1.240.0040.00923.98
8.1.230.0040.00722.69
8.1.220.0060.00317.89
8.1.210.0000.00918.77
8.1.200.0080.00017.24
8.1.190.0000.00817.13
8.1.180.0040.00418.10
8.1.170.0060.00318.65
8.1.160.0030.00518.91
8.1.150.0030.00520.35
8.1.140.0000.00717.79
8.1.130.0000.00719.01
8.1.120.0000.00717.38
8.1.110.0000.00917.45
8.1.100.0020.00517.50
8.1.90.0040.00417.34
8.1.80.0030.00617.45
8.1.70.0070.00017.38
8.1.60.0040.00417.59
8.1.50.0040.00417.44
8.1.40.0000.00917.43
8.1.30.0040.00417.66
8.1.20.0060.00317.64
8.1.10.0050.00317.60
8.1.00.0000.00817.45
8.0.300.0040.00420.13
8.0.290.0040.00416.58
8.0.280.0050.00318.38
8.0.270.0030.00317.29
8.0.260.0050.00216.85
8.0.250.0000.00716.96
8.0.240.0050.00316.89
8.0.230.0030.00316.87
8.0.220.0050.00316.79
8.0.210.0000.00816.92
8.0.200.0040.00416.91
8.0.190.0000.00816.95
8.0.180.0030.00616.97
8.0.170.0030.00616.86
8.0.160.0040.00416.88
8.0.150.0060.00616.83
8.0.140.0000.00716.86
8.0.130.0060.00013.38
8.0.120.0040.00416.88
8.0.110.0050.00316.95
8.0.100.0000.00716.79
8.0.90.0050.00216.92
8.0.80.0120.00916.94
8.0.70.0070.00016.77
8.0.60.0040.00416.76
8.0.50.0040.00416.73
8.0.30.0100.00917.02
8.0.20.0110.01017.41
8.0.10.0080.00016.91
8.0.00.0120.00716.65
7.4.330.0000.00515.55
7.4.320.0030.00316.58
7.4.300.0030.00316.63
7.4.290.0000.00716.44
7.4.280.0030.00516.61
7.4.270.0000.00816.57
7.4.260.0030.00316.54
7.4.250.0020.00516.61
7.4.240.0030.00316.59
7.4.230.0070.00016.54
7.4.220.0080.00016.41
7.4.210.0100.01016.64
7.4.200.0080.00016.63
7.4.160.0010.01716.54
7.4.150.0060.01217.40
7.4.140.0130.00917.86
7.4.130.0100.00716.69
7.4.120.0170.00716.49
7.4.110.0130.00416.52
7.4.100.0130.00716.52
7.4.90.0160.00016.76
7.4.80.0140.00419.39
7.4.70.0130.00916.48
7.4.60.0090.01016.29
7.4.50.0070.00416.51
7.4.40.0090.00816.48
7.4.10.0040.01214.86
7.4.00.0100.00815.02
7.3.330.0060.00013.31
7.3.320.0070.00013.20
7.3.310.0070.00016.38
7.3.300.0000.00716.24
7.3.290.0110.00516.35
7.3.280.0060.01016.33
7.3.270.0060.01217.40
7.3.260.0080.01116.38
7.3.240.0120.00716.47
7.3.230.0090.00916.64
7.3.210.0140.00516.58
7.3.200.0080.00816.24
7.3.190.0000.01616.48
7.3.180.0110.00916.45
7.3.170.0130.00616.43
7.3.160.0190.00316.32
7.3.130.0060.01214.91
7.3.120.0060.01214.54
7.3.110.0100.01015.18
7.3.100.0070.00714.90
7.3.90.0040.00714.57
7.3.80.0060.00614.50
7.3.70.0090.00014.86
7.3.60.0000.01514.95
7.3.50.0140.00314.88
7.3.40.0030.00714.98
7.3.30.0040.00414.83
7.3.20.0070.00716.68
7.3.10.0000.01016.46
7.3.00.0060.00616.64
7.2.330.0060.01216.47
7.2.320.0080.00816.70
7.2.310.0060.01216.49
7.2.300.0060.01216.52
7.2.290.0140.00316.61
7.2.260.0100.01015.09
7.2.250.0030.01014.93
7.2.240.0070.01015.11
7.2.230.0060.00614.93
7.2.220.0070.00715.05
7.2.210.0040.01114.83
7.2.200.0030.01315.21
7.2.190.0100.00715.16
7.2.180.0050.00514.88
7.2.170.0090.00315.11
7.2.160.0070.00714.85
7.2.150.0100.00616.89
7.2.140.0040.01116.93
7.2.130.0000.01416.80
7.2.120.0070.01016.61
7.2.110.0060.00716.87
7.2.100.0030.00616.80
7.2.90.0040.00716.80
7.2.80.0090.00317.01
7.2.70.0030.00917.01
7.2.60.0100.00316.91
7.2.50.0030.00616.86
7.2.40.0070.01017.10
7.2.30.0060.00617.09
7.2.20.0080.01218.15
7.2.10.0070.01218.32
7.2.00.0070.01218.52
7.1.330.0090.00615.88
7.1.320.0030.01215.93
7.1.310.0050.00516.13
7.1.300.0150.00315.89
7.1.290.0070.01015.91
7.1.280.0060.00915.92
7.1.270.0040.00815.67
7.1.260.0000.01315.63
7.1.250.0030.01015.88
7.1.240.0070.00715.96
7.1.230.0030.00715.71
7.1.220.0030.01015.94
7.1.210.0070.00715.70
7.1.200.0060.00715.67
7.1.190.0030.01215.54
7.1.180.0030.00615.54
7.1.170.0110.00015.77
7.1.160.0000.00815.86
7.1.150.0000.01615.76
7.1.140.0090.01117.34
7.1.130.0070.00817.12
7.1.120.0080.00917.24
7.1.110.0050.01117.15
7.1.100.0060.00816.91
7.1.90.0070.01017.04
7.1.80.0060.00817.03
7.1.70.0070.00616.69
7.1.60.0140.00825.48
7.1.50.0190.01125.15
7.1.40.0190.01025.09
7.1.30.0170.00925.30
7.1.20.0180.01325.10
7.1.10.0110.00616.26
7.1.00.0030.01216.29
7.0.330.0070.00715.55
7.0.320.0060.00515.45
7.0.310.0000.01515.45
7.0.300.0090.00015.45
7.0.290.0070.00715.40
7.0.280.0070.00715.45
7.0.270.0000.01315.44
7.0.260.0000.00815.44
7.0.250.0070.01015.52
7.0.240.0000.01115.37
7.0.230.0040.00815.59
7.0.220.0030.00615.45
7.0.210.0040.01115.33
7.0.200.0090.00015.42
7.0.190.0060.00315.50
7.0.180.0050.00515.41
7.0.170.0070.00715.31
7.0.160.0070.00715.47
7.0.150.0030.00815.46
7.0.140.0000.00915.46
7.0.130.0110.00415.37
7.0.120.0070.00715.59
7.0.110.0060.00315.61
7.0.100.0030.01315.45
7.0.90.0110.00015.52
7.0.80.0130.00315.45
7.0.70.0060.00615.38
7.0.60.0030.01015.38
7.0.50.0040.00415.64
7.0.40.0050.00313.63
7.0.30.0090.00313.51
7.0.20.0000.01413.61
7.0.10.0110.00313.63
7.0.00.0060.00613.45
5.6.400.0040.00814.48
5.6.390.0000.00914.75
5.6.380.0090.00314.53
5.6.370.0070.00714.43
5.6.360.0030.01014.42
5.6.350.0110.00414.38
5.6.340.0030.01214.29
5.6.330.0060.00914.60
5.6.320.0000.01614.69
5.6.310.0060.00314.53
5.6.300.0030.01214.37
5.6.290.0030.01314.58
5.6.280.0100.00314.53
5.6.270.0040.00714.38
5.6.260.0030.00614.53
5.6.250.0030.00914.40
5.6.240.0080.00314.14
5.6.230.0000.01014.41
5.6.220.0000.01414.75
5.6.210.0080.00814.45
5.6.200.0060.00314.38
5.6.190.0090.00314.28
5.6.180.0040.00814.19
5.6.170.0000.01414.22
5.6.160.0070.00314.47
5.6.150.0040.01214.08
5.6.140.0070.00714.49
5.6.130.0060.00614.48
5.6.120.0030.01314.50
5.6.110.0040.01114.57
5.6.100.0000.01414.34
5.6.90.0040.00914.43
5.6.80.0000.01514.31
5.6.70.0030.00614.58
5.6.60.0000.01014.41
5.6.50.0030.00914.16
5.6.40.0070.00714.69
5.6.30.0030.00614.27
5.6.20.0040.01114.36
5.6.10.0070.00714.18
5.6.00.0030.00614.27

preferences:
64.85 ms | 401 KiB | 5 Q