Athul Posted August 3, 2022 Share Posted August 3, 2022 Watson gives Sherlock an array A of length N. Then he asks him to determine if there exists an element in the array such that the sum of the elements on its left is equal to the sum of the elements on its right. If there are no elements to the left/right, then the sum is considered to be zero.Formally, find an i, such that, A1+A2.....Ai-1 = Ai+1+Ai+2......AN. Quote Link to comment https://forums.phpfreaks.com/topic/315129-can-any-body-help-me-with-this-question-have-to-do-this-in-php/ Share on other sites More sharing options...
Barand Posted August 3, 2022 Share Posted August 3, 2022 What have you tried so far? Quote Link to comment https://forums.phpfreaks.com/topic/315129-can-any-body-help-me-with-this-question-have-to-do-this-in-php/#findComment-1598905 Share on other sites More sharing options...
ginerjm Posted August 3, 2022 Share Posted August 3, 2022 (edited) How about this? $arr = array(1,2,3,4,3,2,1); $sz = count($arr); foreach($arr as $k=>$v) echo "$k:$v, "; echo "<br><br>"; for ($n = 0; $n < $sz; $n++) { $left = $right = 0; for($i = 0; $i < $n; $i++) $left += $arr[$i]; for($i = $n+1; $i < $sz; $i++) $right += $arr[$i]; echo "For middle #$n of {$arr[$n]} the left sum is $left and the sum right is $right"; if ($left == $right) echo "***!"; echo "<br>"; } I believe this will satisfy your task requirements. Hopefully it is not HOMEWORK that you decided to farm out. I took it as a small challenge and threw this together. If it were anything more than that I would not have done it without seeing your attempts, as Barand has asked about. Edited August 3, 2022 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/315129-can-any-body-help-me-with-this-question-have-to-do-this-in-php/#findComment-1598906 Share on other sites More sharing options...
Barand Posted August 3, 2022 Share Posted August 3, 2022 or $arr = [ 21, 12, 3 , 4, 25, 26, 14 ]; for ($i=0, $k = count($arr); $i<$k; $i++) { $left = array_sum(array_slice($arr, 0, $i)); $right = array_sum(array_slice($arr, 1+$i-$k)); if ($left == $right) $arr[$i] = "<sub>$left</sub><span style='font-weight: 600; color: red;'>$arr[$i]</span><sub>$right</sub>"; } echo join(', ', $arr); // show solution Quote Link to comment https://forums.phpfreaks.com/topic/315129-can-any-body-help-me-with-this-question-have-to-do-this-in-php/#findComment-1598909 Share on other sites More sharing options...
Athul Posted August 3, 2022 Author Share Posted August 3, 2022 You guys are awesome. Iam soo noob to programing. I was like,what the hell is this I didnt even know what to do. How do you guys learn php? Quote Link to comment https://forums.phpfreaks.com/topic/315129-can-any-body-help-me-with-this-question-have-to-do-this-in-php/#findComment-1598910 Share on other sites More sharing options...
ginerjm Posted August 3, 2022 Share Posted August 3, 2022 We read. Quote Link to comment https://forums.phpfreaks.com/topic/315129-can-any-body-help-me-with-this-question-have-to-do-this-in-php/#findComment-1598911 Share on other sites More sharing options...
mac_gyver Posted August 3, 2022 Share Posted August 3, 2022 programming languages are like any other language. you have to learn enough of the words and syntax to be able to write and read it, not just repeat things you see. this is where the dictionary/documentation for the language you are trying to use comes in handy. Quote Link to comment https://forums.phpfreaks.com/topic/315129-can-any-body-help-me-with-this-question-have-to-do-this-in-php/#findComment-1598912 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.