{"id":99,"date":"2019-06-01T12:05:08","date_gmt":"2019-06-01T12:05:08","guid":{"rendered":"https:\/\/www.369usa.com\/blog\/?p=99"},"modified":"2019-06-01T12:06:34","modified_gmt":"2019-06-01T12:06:34","slug":"tech-interview-exam-question-the-pivot-index-of-this-array","status":"publish","type":"post","link":"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-the-pivot-index-of-this-array\/","title":{"rendered":"Tech Interview Exam Question: the &#8220;pivot&#8221; index of this array"},"content":{"rendered":"<p>Given an array of integers nums, write a method that returns the &#8220;pivot&#8221; index of this array.<\/p>\n<p>We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.<\/p>\n<p>If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.<\/p>\n<p>Example 1:<\/p>\n<p>Input:<br \/>\nnums = [1, 7, 3, 6, 5, 6]<br \/>\nOutput: 3<br \/>\nExplanation:<br \/>\nThe sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.<br \/>\nAlso, 3 is the first index where this occurs.<\/p>\n<p>Example 2:<\/p>\n<p>Input:<br \/>\nnums = [1, 2, 3]<br \/>\nOutput: -1<br \/>\nExplanation:<br \/>\nThere is no index that satisfies the conditions in the problem statement.<\/p>\n<p>Note:<\/p>\n<p>The length of nums will be in the range [0, 10000].<br \/>\nEach element nums[i] will be an integer in the range [-1000, 1000].<\/p>\n<pre>\r\n\r\n\/**\r\n * @param {number[]} nums\r\n * @return {number}\r\n *\/\r\nvar pivotIndex = function(nums) {\r\n    \r\n    var leftSum =  0;\r\n    var rightSum =  (function(){\r\n        var _sum = 0;\r\n        for(var i=0; i&lt;nums.length; i++){\r\n            _sum += nums[i];\r\n        }\r\n        return _sum;\r\n    })();\r\n    \r\n    for(var i=0; i&lt;nums.length; i++){\r\n        \r\n        leftSum += (i==0)?0:nums[i-1];\r\n        rightSum -= nums[i];\r\n        \r\n\/\/console.log(i+':'+leftSum+'--'+rightSum)\r\n        \r\n        if(leftSum === rightSum) return i;\r\n        \r\n    }\r\n    return -1;\r\n};\r\n\r\n<\/pre>\n<p>Runtime: 64 ms<br \/>\nMemory Usage: 37.4 MB<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers nums, write a method that returns the &#8220;pivot&#8221; index of this array. We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index. If no such index &hellip; <a href=\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-the-pivot-index-of-this-array\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Tech Interview Exam Question: the &#8220;pivot&#8221; index of this array&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-99","post","type-post","status-publish","format-standard","hentry","category-6"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Tech Interview Exam Question: the &quot;pivot&quot; index of this array - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-the-pivot-index-of-this-array\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Tech Interview Exam Question: the &quot;pivot&quot; index of this array - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\" \/>\n<meta property=\"og:description\" content=\"Given an array of integers nums, write a method that returns the &#8220;pivot&#8221; index of this array. We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index. If no such index &hellip; Continue reading &quot;Tech Interview Exam Question: the &#8220;pivot&#8221; index of this array&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-the-pivot-index-of-this-array\/\" \/>\n<meta property=\"og:site_name\" content=\"\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\" \/>\n<meta property=\"article:published_time\" content=\"2019-06-01T12:05:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-06-01T12:06:34+00:00\" \/>\n<meta name=\"author\" content=\"darkersss\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"darkersss\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-the-pivot-index-of-this-array\/\",\"url\":\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-the-pivot-index-of-this-array\/\",\"name\":\"Tech Interview Exam Question: the \\\"pivot\\\" index of this array - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\",\"isPartOf\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/#website\"},\"datePublished\":\"2019-06-01T12:05:08+00:00\",\"dateModified\":\"2019-06-01T12:06:34+00:00\",\"author\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-the-pivot-index-of-this-array\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-the-pivot-index-of-this-array\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-the-pivot-index-of-this-array\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.369usa.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tech Interview Exam Question: the &#8220;pivot&#8221; index of this array\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.369usa.com\/blog\/#website\",\"url\":\"https:\/\/www.369usa.com\/blog\/\",\"name\":\"369\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.369usa.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6\",\"name\":\"darkersss\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g\",\"caption\":\"darkersss\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Tech Interview Exam Question: the \"pivot\" index of this array - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-the-pivot-index-of-this-array\/","og_locale":"en_US","og_type":"article","og_title":"Tech Interview Exam Question: the \"pivot\" index of this array - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","og_description":"Given an array of integers nums, write a method that returns the &#8220;pivot&#8221; index of this array. We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index. If no such index &hellip; Continue reading \"Tech Interview Exam Question: the &#8220;pivot&#8221; index of this array\"","og_url":"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-the-pivot-index-of-this-array\/","og_site_name":"\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","article_published_time":"2019-06-01T12:05:08+00:00","article_modified_time":"2019-06-01T12:06:34+00:00","author":"darkersss","twitter_card":"summary_large_image","twitter_misc":{"Written by":"darkersss","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-the-pivot-index-of-this-array\/","url":"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-the-pivot-index-of-this-array\/","name":"Tech Interview Exam Question: the \"pivot\" index of this array - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","isPartOf":{"@id":"https:\/\/www.369usa.com\/blog\/#website"},"datePublished":"2019-06-01T12:05:08+00:00","dateModified":"2019-06-01T12:06:34+00:00","author":{"@id":"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6"},"breadcrumb":{"@id":"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-the-pivot-index-of-this-array\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-the-pivot-index-of-this-array\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-the-pivot-index-of-this-array\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.369usa.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Tech Interview Exam Question: the &#8220;pivot&#8221; index of this array"}]},{"@type":"WebSite","@id":"https:\/\/www.369usa.com\/blog\/#website","url":"https:\/\/www.369usa.com\/blog\/","name":"369\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.369usa.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6","name":"darkersss","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g","caption":"darkersss"}}]}},"_links":{"self":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/99"}],"collection":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/comments?post=99"}],"version-history":[{"count":2,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":101,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/99\/revisions\/101"}],"wp:attachment":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/media?parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/categories?post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/tags?post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}