I am trying to make it so that my code displays if entered array of numbers can be divided into 2 groups, and if yes then it displays"yes" or if no then"no. The yes or no part works fine, but if the code says yes then I want the code to also display the 2 columns with the numbers, I tried a lot but I just can't figure it out, please help.
function helper(&$arr, $n, $start, $lsum, $rsum) {
if ($start == $n)
return $lsum == $rsum;
if ($arr[$start] % 5 == 0)
$lsum += $arr[$start];
else if ($arr[$start] % 3 == 0)
$rsum += $arr[$start];
else
return helper($arr, $n, $start + 1, $lsum + $arr[$start], $rsum)
|| helper($arr, $n, $start + 1, $lsum, $rsum + $arr[$start]);
return helper($arr, $n, $start + 1, $lsum, $rsum);
}
function splitArray($arr, $n) {
return helper($arr, $n, 0, 0, 0);
}
$arr = array( 7,1, 7,3,4,6);
$n = count($arr);
if (splitArray($arr, $n))
print("Yes");
else
print("No");
Provided you want to split them into two parts Replace your code at the bottom with...
$arr = array( 7,1, 7,3,4,6);
$n = count($arr);
list($group1, $group2) = array_chunk($arr, ceil(count($arr) / 2)); //Split the initial array into 2 if you want 3 just change 2 to 3 and so forth
if (splitArray($arr, $n)){
echo "Yes";
print_r($group1);
print_r($group2); //Each array assigned to new variable
} else {
echo "No";
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.