<?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>
<br>--------16---------<br>[12,3,1]<br>[3,4,9]<br>[1,6,9]<br>3<br>--------24---------<br>[12,3,9]<br>1<br>--------32---------<br>0
<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>
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
<br>--------16---------<br>[12,3,1]<br>[3,4,9]<br>[1,6,9]<br>3<br>--------24---------<br>[12,3,9]<br>1<br>--------32---------<br>0
<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>