{"version":"1.0","provider_name":"Mark's Blog","provider_url":"https:\/\/dangnh.cf\/vi","author_name":"admin","author_url":"https:\/\/dangnh.cf\/vi\/author\/admin\/","title":"L\u00e0m to\u00e1n gi\u1ea3i tr\u00ed m\u1ed9t t\u00ed nh\u1ec9? - Mark's Blog","type":"rich","width":600,"height":338,"html":"<blockquote class=\"wp-embedded-content\" data-secret=\"M3qHj6XcXD\"><a href=\"https:\/\/dangnh.cf\/vi\/2017\/05\/19\/lam-toan-giai-tri-mot-ti-nhi\/\">L\u00e0m to\u00e1n gi\u1ea3i tr\u00ed m\u1ed9t t\u00ed nh\u1ec9?<\/a><\/blockquote><iframe sandbox=\"allow-scripts\" security=\"restricted\" src=\"https:\/\/dangnh.cf\/vi\/2017\/05\/19\/lam-toan-giai-tri-mot-ti-nhi\/embed\/#?secret=M3qHj6XcXD\" width=\"600\" height=\"338\" title=\"&#8220;L\u00e0m to\u00e1n gi\u1ea3i tr\u00ed m\u1ed9t t\u00ed nh\u1ec9?&#8221; &#8212; Mark&#039;s Blog\" data-secret=\"M3qHj6XcXD\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" class=\"wp-embedded-content\"><\/iframe><script type=\"text\/javascript\">\n\/* <![CDATA[ *\/\n\/*! This file is auto-generated *\/\n!function(d,l){\"use strict\";l.querySelector&&d.addEventListener&&\"undefined\"!=typeof URL&&(d.wp=d.wp||{},d.wp.receiveEmbedMessage||(d.wp.receiveEmbedMessage=function(e){var t=e.data;if((t||t.secret||t.message||t.value)&&!\/[^a-zA-Z0-9]\/.test(t.secret)){for(var s,r,n,a=l.querySelectorAll('iframe[data-secret=\"'+t.secret+'\"]'),o=l.querySelectorAll('blockquote[data-secret=\"'+t.secret+'\"]'),c=new RegExp(\"^https?:$\",\"i\"),i=0;i<o.length;i++)o[i].style.display=\"none\";for(i=0;i<a.length;i++)s=a[i],e.source===s.contentWindow&&(s.removeAttribute(\"style\"),\"height\"===t.message?(1e3<(r=parseInt(t.value,10))?r=1e3:~~r<200&&(r=200),s.height=r):\"link\"===t.message&&(r=new URL(s.getAttribute(\"src\")),n=new URL(t.value),c.test(n.protocol))&&n.host===r.host&&l.activeElement===s&&(d.top.location.href=t.value))}},d.addEventListener(\"message\",d.wp.receiveEmbedMessage,!1),l.addEventListener(\"DOMContentLoaded\",function(){for(var e,t,s=l.querySelectorAll(\"iframe.wp-embedded-content\"),r=0;r<s.length;r++)(t=(e=s[r]).getAttribute(\"data-secret\"))||(t=Math.random().toString(36).substring(2,12),e.src+=\"#?secret=\"+t,e.setAttribute(\"data-secret\",t)),e.contentWindow.postMessage({message:\"ready\",secret:t},\"*\")},!1)))}(window,document);\n\/\/# sourceURL=https:\/\/dangnh.cf\/wp-includes\/js\/wp-embed.min.js\n\/* ]]> *\/\n<\/script>","thumbnail_url":"https:\/\/dangnh.cf\/wp-content\/uploads\/2017\/05\/learning_math-e1459355151809.jpg","thumbnail_width":800,"thumbnail_height":513,"description":"Ok, ch\u00e0o t\u1ea5t c\u1ea3 anh em Ch\u1ea3 l\u00e0 cu\u1ed1i tu\u1ea7n, \u1edf nh\u00e0 r\u1ea3nh rang, tr\u1eddi th\u00ec se se l\u1ea1nh, m\u0169i h\u01a1i t\u1eafc, ch\u1ec9 mu\u1ed1n co ro qu\u1ea5n ch\u0103n \u00f4m laptop cho \u1ea5m. Lang thang thi c\u00e1i cu\u1ed9c thi l\u1eadp tr\u00ecnh c\u1ee7a b\u1ecdn Top Career g\u00ec \u0111\u00f3 :v v\u00f4 t\u00ecnh v\u1ea5p ph\u1ea3i 1 b\u00e0i test demo, m\u00e0 m\u00ecnh th\u1ea5y kh\u00e1 hay, n\u00ean mu\u1ed1n chia s\u1ebb l\u00ean \u0111\u00e2y cho c\u1ea3 nh\u00e0 \u0110\u1ec1 b\u00e0i: A zero-indexed array A consisting of N integers is given. An equilibrium index of this array is any integer P such that 0 \u2264 P &lt; N and the sum of elements of lower indices is equal to the sum of elements of higher indices, i.e. A[0] + A[1] + ... + A[P\u22121] = A[P+1] + ... + A[N\u22122] + A[N\u22121]. Sum of zero elements is assumed to be equal to 0. This can happen if P = 0 or if P = N\u22121. For example, consider the following array A consisting of N = 8 elements: A[0] = -1 A[1] = 3 A[2] = -4 A[3] = 5 A[4] = 1 A[5] = -6 A[6] = 2 A[7] = 1 P = 1 is an equilibrium index of this array, because: A[0] = \u22121 = A[2] + A[3] + A[4] + A[5] + A[6] + A[7] P = 3 is an equilibrium index of this array, because: A[0] + A[1] + A[2] = \u22122 = A[4] + A[5] + A[6] + A[7] P = 7 is also an equilibrium index, because: A[0] + A[1] + A[2] + A[3] + A[4] + A[5] + A[6] = 0 and there are no elements with indices greater than 7. P = 8 is not an equilibrium index, because it does not fulfill the condition 0 \u2264 P &lt; N. Write a function: function solution($A); that, given a zero-indexed array A consisting of N integers, returns any of its equilibrium indices. The function should return \u22121 if no equilibrium index exists. For example, given array A shown above, the function may return 1, 3 or 7, as explained above. Assume that: N is an integer within the range [0..100,000]; each element of array A is an integer within the range [\u22122,147,483,648..2,147,483,647]. Complexity: expected worst-case time complexity is O(N); expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). Elements of input arrays can be modified. D\u1ecbch \u0111\u1ea1i kh\u00e1i l\u00e0: Ta c\u00f3 1 indexed array A g\u1ed3m N ph\u1ea7n t\u1eed l\u00e0 c\u00e1c s\u1ed1 t\u1ef1 nhi\u00ean b\u1ea5t k\u1ef3. T\u00ecm ch\u1ec9 s\u1ed1 c\u00e2n b\u1eb1ng P sao cho P l\u00e0 - Key c\u1ee7a array A - Th\u1ecfa m\u00e3n \u0111i\u1ec1u ki\u1ec7n: A[0] + A[1] + ... + A[P\u22121] = A[P+1] + ... + A[N\u22122] + A[N\u22121]. - P = 0 ho\u1eb7c P= N-1 \u0111\u1ec1u c\u00f3 th\u1ec3 l\u00e0 gi\u00e1 tr\u1ecb \u0111\u00fang - C\u00f3 th\u1ec3 c\u00f3 nhi\u1ec1u ho\u1eb7c 1 gi\u00e1 tr\u1ecb P - N\u1ebfu kh\u00f4ng c\u00f3 gi\u00e1 tr\u1ecb n\u00e0o, function c\u1ea7n return -1 C\u00e1c \u0111i\u1ec1u ki\u1ec7n v\u1ec1 complexity, assumption m\u1eddi xem ph\u1ea7n ti\u1ebfng Anh nh\u00e9, l\u01b0\u1eddi d\u1ecbch :D R\u1ed3i, \u0111\u1ec1 b\u00e0i l\u00e0 nh\u01b0 v\u1eady, y\u00eau c\u1ea7u vi\u1ebft 1 function d\u01b0\u1edbi ng\u00f4n ng\u1eef n\u00e0o c\u0169ng \u0111\u01b0\u1ee3c, th\u1eddi gian l\u00e0m b\u00e0i l\u00e0\u00a030 ph\u00fat. GI\u1ea2I (\u0110\u00e2y l\u00e0 c\u00e1ch gi\u1ea3i c\u1ee7a m\u00ecnh, ai c\u00f3 c\u00e1ch n\u00e0o hay h\u01a1n th\u00ec comment nh\u00e9 ) H\u01b0\u1edbng gi\u1ea3i c\u1ee7a m\u00ecnh l\u00e0 s\u1ebd chia m\u1ea3ng 1 chi\u1ec1u n\u00e0y th\u00e0nh 2 ph\u1ea7n, T\u1ed5ng\u00a0Tr\u00e1i\u00a0v\u00e0 T\u1ed5ng\u00a0Ph\u1ea3i. Sau \u0111\u00f3 ch\u1ea1y d\u1ea7n t\u1eeb tr\u00e1i qua ph\u1ea3i c\u1ee7a m\u1ea3ng v\u00e0 so s\u00e1nh 2 t\u1ed5ng n\u00e0y v\u1edbi nhau. EZPZ ph\u1ea3i kh\u00f4ng? :)) M\u00ecnh code PHP nh\u00e9, \u0111\u1ea7u ti\u00ean g\u00e1n gi\u00e1 tr\u1ecb default ph\u00e1t\u00a0(th\u00f3i quen th\u00f4i, PHP kh\u00f4ng c\u1ea7n initialize bi\u1ebfn) R\u1ed3i, b\u00e2y gi\u1edd l\u1eb7p qua c\u00e1i array A xem sao nh\u1ec9? B\u1eaft \u0111\u1ea7u so s\u00e1nh, v\u00e0 ph\u1ea3i tr\u1eeb d\u1ea7n\u00a0$sumRight\u00a0 t\u0103ng d\u1ea7n\u00a0$sumLeft\u00a0v\u1edbi gi\u00e1 tr\u1ecb c\u1ee7a m\u1ed7i element Tr\u00f4ng c\u00f3 v\u1ebb \u1ed5n \u1ed5n r\u1ed3i \u0111\u1ea5y nh\u1ec9 :)) Nh\u01b0ng th\u1ef1c ra l\u00e0 \u0111\u1ebfch \u1ed5n, m\u00ecnh c\u00f2n ch\u01b0a check null c\u01a1 m\u00e0 =)) c\u00e1i c\u01a1 b\u1ea3n nh\u1ea5t c\u00f2n su\u1ed1t ng\u00e0y qu\u00ean \u0111\u00e2y Ok done! N\u00e0o, b\u00e2y gi\u1edd test th\u00f4i! Chu\u1ea9n \u0111\u00e9t :)) Test th\u1eed v\u1edbi d\u1eef li\u1ec7u\u00a0kh\u00e1c xem sao Done! Hy v\u1ecdng anh em gi\u1ea3i to\u00e1n vui v\u1ebb :)) c\u00f3 solution n\u00e0o ngon h\u01a1n nh\u1edb comment -_- \u0111\u1eebng c\u00f3 \u00e9m h\u00e0ng"}