{"id":11,"date":"2019-02-05T14:43:23","date_gmt":"2019-02-05T14:43:23","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/coding\/?p=11"},"modified":"2019-02-05T17:19:51","modified_gmt":"2019-02-05T17:19:51","slug":"question-2-check-if-subarray-with-zero-sum-exists","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/coding\/question-2-check-if-subarray-with-zero-sum-exists\/","title":{"rendered":"Question 2: Check if Subarray with Zero Sum Exists"},"content":{"rendered":"<p><strong>Question 2<\/strong><\/p>\n<p>You are given an integer array. You need to check if the array contains sub-array with zero sum.<\/p>\n<p>For example:<\/p>\n<p><strong>Input<\/strong>: A = {4, -1, -3, 1}<\/p>\n<p><strong>Output<\/strong>: Subarray with zero sum exists<\/p>\n<p>{4, -1, -3}<\/p>\n<p>&nbsp;<\/p>\n<h4><strong>Solution 1: Inefficient Solution &#8211; Quadratic Time &#8211; O(n<sup>2<\/sup>)<\/strong><\/h4>\n<p>In this solution we take running sum of all each element with every other element, and each time we check if the sum is equal to zero. If yes then we return true.<\/p>\n<p>The Java code is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/*<\/span>\r\n<span style=\"color: #888888;\"> * Written by: Kindson The Genius<\/span>\r\n<span style=\"color: #888888;\"> *\/<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">ZeroSumSubarray1<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">boolean<\/span> <span style=\"color: #0066bb; font-weight: bold;\">zeroSumSubarray<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> A<span style=\"color: #333333;\">[])<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> sum<span style=\"color: #333333;\">;<\/span>\r\n\t\t\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">for<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> i <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span> i <span style=\"color: #333333;\">&lt;<\/span> A<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">length<\/span><span style=\"color: #333333;\">;<\/span> i<span style=\"color: #333333;\">++)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\tsum <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t\t\t\r\n\t\t\t<span style=\"color: #008800; font-weight: bold;\">for<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> j <span style=\"color: #333333;\">=<\/span> i<span style=\"color: #333333;\">;<\/span> j <span style=\"color: #333333;\">&lt;<\/span> A<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">length<\/span><span style=\"color: #333333;\">;<\/span> j<span style=\"color: #333333;\">++)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\t\tsum <span style=\"color: #333333;\">=<\/span> sum <span style=\"color: #333333;\">+<\/span> A<span style=\"color: #333333;\">[<\/span>j<span style=\"color: #333333;\">];<\/span>\r\n\t\t\t\t<span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>sum <span style=\"color: #333333;\">==<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\t\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Subarray exists\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\t\t\t\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #008800; font-weight: bold;\">true<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t\t\t\t<span style=\"color: #333333;\">}<\/span>\t\t\t\t\r\n\t\t\t<span style=\"color: #333333;\">}<\/span><span style=\"color: #888888;\">\/\/end of for loop\t\t\t<\/span>\r\n\t\t<span style=\"color: #333333;\">}<\/span><span style=\"color: #888888;\">\/\/end of for loop<\/span>\r\n\t\t\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Subarray does not exist\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #008800; font-weight: bold;\">false<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span><span style=\"color: #888888;\">\/\/end of zeroSumSubarray<\/span>\r\n\r\n\t\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> ar<span style=\"color: #333333;\">[]<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">{<\/span><span style=\"color: #0000dd; font-weight: bold;\">4<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">};<\/span>\r\n\t\tzeroSumSubarray<span style=\"color: #333333;\">(<\/span>ar<span style=\"color: #333333;\">);<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><\/h4>\n<h4><strong>Solution 2: Using a Hash Table &#8211; Linear Time O(n)<\/strong><\/h4>\n<p>In this solution, start from the first element and take running sum. Then for each element, we store the sum so far in a hash table. Before we store an element, we check the hash table\u00a0 to see if the sum already exists (ie if it has been seen before). If yes, then we know hat there is\u00a0 a sub-array with zero sum that ends at the current index.<\/p>\n<p>The Java program is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/*<\/span>\r\n<span style=\"color: #888888;\"> * Written by Kindson the Genius<\/span>\r\n<span style=\"color: #888888;\"> * Check is Subarry with zero sum exists<\/span>\r\n<span style=\"color: #888888;\"> *\/<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.util.HashSet<\/span><span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.util.Set<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">ZeroSumSubarray2<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> Boolean <span style=\"color: #0066bb; font-weight: bold;\">ZeroSumSubarray<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> A<span style=\"color: #333333;\">[])<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\tSet<span style=\"color: #333333;\">&lt;<\/span>Integer<span style=\"color: #333333;\">&gt;<\/span> set <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> HashSet<span style=\"color: #333333;\">&lt;<\/span>Integer<span style=\"color: #333333;\">&gt;();<\/span>\r\n\t\t\r\n\t\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> sum <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t\t\r\n\t\tset<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">add<\/span><span style=\"color: #333333;\">(<\/span>sum<span style=\"color: #333333;\">);<\/span>\r\n\t\t\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">for<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> i <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span> i<span style=\"color: #333333;\">&lt;<\/span>A<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">length<\/span><span style=\"color: #333333;\">;<\/span> i<span style=\"color: #333333;\">++)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\tsum <span style=\"color: #333333;\">=<\/span> sum <span style=\"color: #333333;\">+<\/span> A<span style=\"color: #333333;\">[<\/span>i<span style=\"color: #333333;\">];<\/span>\r\n\t\t\t\r\n\t\t\t<span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>set<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">contains<\/span><span style=\"color: #333333;\">(<\/span>sum<span style=\"color: #333333;\">))<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\t\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #008800; font-weight: bold;\">true<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t\t\t<span style=\"color: #333333;\">}<\/span>\r\n\t\t\t\r\n\t\t\tset<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">add<\/span><span style=\"color: #333333;\">(<\/span>sum<span style=\"color: #333333;\">);<\/span>\r\n\t\t\t\r\n\t\t<span style=\"color: #333333;\">}<\/span><span style=\"color: #888888;\">\/\/end of for loop<\/span>\r\n\t\t\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #008800; font-weight: bold;\">false<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span><span style=\"color: #888888;\">\/\/end of function<\/span>\r\n\t\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n\t\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> ar<span style=\"color: #333333;\">[]<\/span>  <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">{<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">4<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">4<\/span><span style=\"color: #333333;\">};<\/span>\r\n\t\t\r\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span>ZeroSumSubarray<span style=\"color: #333333;\">(<\/span>ar<span style=\"color: #333333;\">));<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Question 2 You are given an integer array. You need to check if the array contains sub-array with zero sum. For example: Input: A = &hellip; <\/p>\n","protected":false},"author":395,"featured_media":12,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,2,3],"tags":[],"class_list":["post-11","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-array","category-coding-challenge","category-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Question 2: Check if Subarray with Zero Sum Exists - Coding Challenge<\/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.kindsonthegenius.com\/coding\/question-2-check-if-subarray-with-zero-sum-exists\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Question 2: Check if Subarray with Zero Sum Exists - Coding Challenge\" \/>\n<meta property=\"og:description\" content=\"Question 2 You are given an integer array. You need to check if the array contains sub-array with zero sum. For example: Input: A = &hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.kindsonthegenius.com\/coding\/question-2-check-if-subarray-with-zero-sum-exists\/\" \/>\n<meta property=\"og:site_name\" content=\"Coding Challenge\" \/>\n<meta property=\"article:published_time\" content=\"2019-02-05T14:43:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-02-05T17:19:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.kindsonthegenius.com\/coding\/wp-content\/uploads\/sites\/4\/2019\/02\/Subarray-with-zero-sum.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"927\" \/>\n\t<meta property=\"og:image:height\" content=\"495\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-2-check-if-subarray-with-zero-sum-exists\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-2-check-if-subarray-with-zero-sum-exists\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"headline\":\"Question 2: Check if Subarray with Zero Sum Exists\",\"datePublished\":\"2019-02-05T14:43:23+00:00\",\"dateModified\":\"2019-02-05T17:19:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-2-check-if-subarray-with-zero-sum-exists\\\/\"},\"wordCount\":176,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-2-check-if-subarray-with-zero-sum-exists\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/wp-content\\\/uploads\\\/sites\\\/4\\\/2019\\\/02\\\/Subarray-with-zero-sum.jpg\",\"articleSection\":[\"Array\",\"Coding Challenge\",\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-2-check-if-subarray-with-zero-sum-exists\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-2-check-if-subarray-with-zero-sum-exists\\\/\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-2-check-if-subarray-with-zero-sum-exists\\\/\",\"name\":\"Question 2: Check if Subarray with Zero Sum Exists - Coding Challenge\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-2-check-if-subarray-with-zero-sum-exists\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-2-check-if-subarray-with-zero-sum-exists\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/wp-content\\\/uploads\\\/sites\\\/4\\\/2019\\\/02\\\/Subarray-with-zero-sum.jpg\",\"datePublished\":\"2019-02-05T14:43:23+00:00\",\"dateModified\":\"2019-02-05T17:19:51+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-2-check-if-subarray-with-zero-sum-exists\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-2-check-if-subarray-with-zero-sum-exists\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-2-check-if-subarray-with-zero-sum-exists\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/wp-content\\\/uploads\\\/sites\\\/4\\\/2019\\\/02\\\/Subarray-with-zero-sum.jpg\",\"contentUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/wp-content\\\/uploads\\\/sites\\\/4\\\/2019\\\/02\\\/Subarray-with-zero-sum.jpg\",\"width\":927,\"height\":495,\"caption\":\"Check for subarray with zero sum\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/question-2-check-if-subarray-with-zero-sum-exists\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Question 2: Check if Subarray with Zero Sum Exists\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/#website\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/\",\"name\":\"Coding Challenge\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/coding\\\/author\\\/kindsonthegenius-2\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Question 2: Check if Subarray with Zero Sum Exists - Coding Challenge","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.kindsonthegenius.com\/coding\/question-2-check-if-subarray-with-zero-sum-exists\/","og_locale":"en_US","og_type":"article","og_title":"Question 2: Check if Subarray with Zero Sum Exists - Coding Challenge","og_description":"Question 2 You are given an integer array. You need to check if the array contains sub-array with zero sum. For example: Input: A = &hellip;","og_url":"https:\/\/www.kindsonthegenius.com\/coding\/question-2-check-if-subarray-with-zero-sum-exists\/","og_site_name":"Coding Challenge","article_published_time":"2019-02-05T14:43:23+00:00","article_modified_time":"2019-02-05T17:19:51+00:00","og_image":[{"width":927,"height":495,"url":"https:\/\/www.kindsonthegenius.com\/coding\/wp-content\/uploads\/sites\/4\/2019\/02\/Subarray-with-zero-sum.jpg","type":"image\/jpeg"}],"author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.kindsonthegenius.com\/coding\/question-2-check-if-subarray-with-zero-sum-exists\/#article","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/coding\/question-2-check-if-subarray-with-zero-sum-exists\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/www.kindsonthegenius.com\/coding\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"headline":"Question 2: Check if Subarray with Zero Sum Exists","datePublished":"2019-02-05T14:43:23+00:00","dateModified":"2019-02-05T17:19:51+00:00","mainEntityOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/coding\/question-2-check-if-subarray-with-zero-sum-exists\/"},"wordCount":176,"commentCount":0,"image":{"@id":"https:\/\/www.kindsonthegenius.com\/coding\/question-2-check-if-subarray-with-zero-sum-exists\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/coding\/wp-content\/uploads\/sites\/4\/2019\/02\/Subarray-with-zero-sum.jpg","articleSection":["Array","Coding Challenge","Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.kindsonthegenius.com\/coding\/question-2-check-if-subarray-with-zero-sum-exists\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.kindsonthegenius.com\/coding\/question-2-check-if-subarray-with-zero-sum-exists\/","url":"https:\/\/www.kindsonthegenius.com\/coding\/question-2-check-if-subarray-with-zero-sum-exists\/","name":"Question 2: Check if Subarray with Zero Sum Exists - Coding Challenge","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/coding\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/coding\/question-2-check-if-subarray-with-zero-sum-exists\/#primaryimage"},"image":{"@id":"https:\/\/www.kindsonthegenius.com\/coding\/question-2-check-if-subarray-with-zero-sum-exists\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/coding\/wp-content\/uploads\/sites\/4\/2019\/02\/Subarray-with-zero-sum.jpg","datePublished":"2019-02-05T14:43:23+00:00","dateModified":"2019-02-05T17:19:51+00:00","author":{"@id":"https:\/\/www.kindsonthegenius.com\/coding\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"breadcrumb":{"@id":"https:\/\/www.kindsonthegenius.com\/coding\/question-2-check-if-subarray-with-zero-sum-exists\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.kindsonthegenius.com\/coding\/question-2-check-if-subarray-with-zero-sum-exists\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.kindsonthegenius.com\/coding\/question-2-check-if-subarray-with-zero-sum-exists\/#primaryimage","url":"https:\/\/www.kindsonthegenius.com\/coding\/wp-content\/uploads\/sites\/4\/2019\/02\/Subarray-with-zero-sum.jpg","contentUrl":"https:\/\/www.kindsonthegenius.com\/coding\/wp-content\/uploads\/sites\/4\/2019\/02\/Subarray-with-zero-sum.jpg","width":927,"height":495,"caption":"Check for subarray with zero sum"},{"@type":"BreadcrumbList","@id":"https:\/\/www.kindsonthegenius.com\/coding\/question-2-check-if-subarray-with-zero-sum-exists\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.kindsonthegenius.com\/coding\/"},{"@type":"ListItem","position":2,"name":"Question 2: Check if Subarray with Zero Sum Exists"}]},{"@type":"WebSite","@id":"https:\/\/www.kindsonthegenius.com\/coding\/#website","url":"https:\/\/www.kindsonthegenius.com\/coding\/","name":"Coding Challenge","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.kindsonthegenius.com\/coding\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.kindsonthegenius.com\/coding\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/www.kindsonthegenius.com\/coding\/author\/kindsonthegenius-2\/"}]}},"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/coding\/wp-json\/wp\/v2\/posts\/11","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/coding\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/coding\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/coding\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/coding\/wp-json\/wp\/v2\/comments?post=11"}],"version-history":[{"count":1,"href":"https:\/\/www.kindsonthegenius.com\/coding\/wp-json\/wp\/v2\/posts\/11\/revisions"}],"predecessor-version":[{"id":13,"href":"https:\/\/www.kindsonthegenius.com\/coding\/wp-json\/wp\/v2\/posts\/11\/revisions\/13"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/coding\/wp-json\/wp\/v2\/media\/12"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/coding\/wp-json\/wp\/v2\/media?parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/coding\/wp-json\/wp\/v2\/categories?post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/coding\/wp-json\/wp\/v2\/tags?post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}