<br />
<b>Warning</b>:  Undefined array key "global_protection_id" in <b>/home/wikitechy/public_html/interview-questions/wp-content/plugins/content-protector/inc/class-ps-rest-handler.php</b> on line <b>51</b><br />
{"id":663,"date":"2021-07-14T00:03:02","date_gmt":"2021-07-14T00:03:02","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=663"},"modified":"2021-09-11T10:51:40","modified_gmt":"2021-09-11T10:51:40","slug":"how-to-optimally-divide-an-array-into-two-subarrays-so-that-sum-of-elements-in-both-are-same","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-optimally-divide-an-array-into-two-subarrays-so-that-sum-of-elements-in-both-are-same\/","title":{"rendered":"How to optimally divide an array into two subarrays so that sum of elements in both are same ?"},"content":{"rendered":"<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"how-to-optimally-divide-an-array-into-two-subarrays-so-that-sum-of-elements-in-both-are-same\" class=\"color-pink\" style=\"text-align: justify;\">How to optimally divide an array into two subarrays so that sum of elements in both are same ?<\/h2>\n<p style=\"text-align: justify;\">There are two methods used to\u00a0<a href=\"https:\/\/www.wikitechy.com\/tutorials\/java\/how-to-split-string-into-array-in-java\" target=\"_blank\" rel=\"noopener\">split arrays<\/a>,<\/p>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li>Given an array of integers greater than zero, find if it is possible to split it in two subarrays (without reordering the elements), such that the sum of the two subarrays is the same. For example,<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"Output\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<figure class=\"highlight\">\n<pre><code class=\"hljs\" data-lang=\"\"><span class=\"nt\">Input : Arr[] = { 1 , 2 , 3 , 4 , 5 , 5  }\r\nOutput :  { 1 2 3 4 } \r\n          { 5 , 5 }\r\n<\/span><\/code><\/pre>\n<\/figure>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li>Given an array of integers, find if it\u2019s possible to remove exactly one integer from the array that divides the array into two subarrays with the same sum. For Example,<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"Output\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<figure class=\"highlight\">\n<pre><code class=\"hljs bash\" data-lang=\"\"><span class=\"nt\">                           Input:  arr = [<span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">1<\/span>]\r\n                           Output:  <span class=\"hljs-literal\">true<\/span>\r\n<\/span><\/code><\/pre>\n<\/figure>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li>On removing element 2 at index 1, the array gets divided into two subarrays [6] and [3, 2, 1] having equal sum.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"text-center row\" style=\"text-align: justify;\"><\/div>\n<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"sample-code-in-java\" class=\"color-blue\" style=\"text-align: justify;\">Sample Code in Java<\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/ Java program to split an array <br\/>\/\/ into two equal sum subarrays <br\/>import java.io.*;<br\/><br\/>class Wikitechy {<br\/><br\/>    \/\/ Returns split point. If <br\/>    \/\/ not possible, then return -1. <br\/>    static int findSplitPoint(int arr[], int n) {<br\/><br\/>        int leftSum = 0;<br\/><br\/>        \/\/ traverse array element <br\/>        for (int i = 0; i &lt; n; i++) {<br\/>            \/\/ add current element to left Sum <br\/>            leftSum += arr[i];<br\/><br\/>            \/\/ find sum of rest array <br\/>            \/\/ elements (rightSum) <br\/>            int rightSum = 0;<br\/><br\/>            for (int j = i + 1; j &lt; n; j++)<br\/>                rightSum += arr[j];<br\/><br\/>            \/\/ split point index <br\/>            if (leftSum == rightSum)<br\/>                return i + 1;<br\/>        }<br\/><br\/>        \/\/ if it is not possible to <br\/>        \/\/ split array into two parts <br\/>        return -1;<br\/>    }<br\/><br\/>    \/\/ Prints two parts after finding <br\/>    \/\/ split point using findSplitPoint() <br\/>    static void printTwoParts(int arr[], int n) {<br\/><br\/>        int splitPoint = findSplitPoint(arr, n);<br\/><br\/>        if (splitPoint == -1 || splitPoint == n) {<br\/>            System.out.println(&quot;Not Possible&quot;);<br\/>            return;<br\/>        }<br\/><br\/>        for (int i = 0; i &lt; n; i++) {<br\/>            if (splitPoint == i)<br\/>                System.out.println();<br\/><br\/>            System.out.print(arr[i] + &quot; &quot;);<br\/><br\/>        }<br\/>    }<br\/><br\/>    \/\/ Driver program <br\/><br\/>    public static void main(String[] args) {<br\/><br\/>        int arr[] = {1,1,0,2};<br\/>        int n = arr.length;<br\/>        printTwoParts(arr, n);<br\/><br\/>    }<br\/>}<\/code><\/pre> <\/div>\n<div class=\"TextHeading\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<h2 id=\"output\" class=\"color-blue\">Output<\/h2>\n<\/div>\n<\/div>\n<div class=\"Output\">\n<div class=\"hddn\">\n<figure class=\"highlight\" style=\"text-align: justify;\">\n<pre><code class=\"hljs\" data-lang=\"\"><span class=\"nt\">1 1 \r\n0 2<\/span><\/code><\/pre>\n<\/figure>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Answer : Given an array of integers greater than zero&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"passster_activate_protection":false,"passster_protect_child_pages":"","passster_protection_type":"password","passster_password":"","passster_activate_overwrite_defaults":"","passster_headline":"","passster_instruction":"","passster_placeholder":"","passster_button":"","passster_id":"","passster_activate_misc_settings":"","passster_redirect_url":"","passster_hide":"no","passster_area_shortcode":"","gtb_hide_title":false,"gtb_wrap_title":false,"gtb_class_title":"","gtb_remove_headerfooter":false,"footnotes":""},"categories":[4004],"tags":[4280,195,4289,491,4292,4279,4290,2212,4005,360,203,199,214,198,363,209,205,2936,222,4282,196,212,213,4293,4288,4278,4285,4295,4284,4291,4286,4281,4294,4283,4287,286,207,366,204,217,282,4023,483,15926,206,4296,200,197,280,364,968],"class_list":["post-663","post","type-post","status-publish","format-standard","hentry","category-java","tag-2d-array-java","tag-accenture-interview-questions-and-answers","tag-addition-program-in-java","tag-applied-materials-interview-questions-and-answers","tag-array-declaration","tag-array-declaration-in-java","tag-array-of-objects-in-java","tag-array-programs-in-java","tag-arraylist-java","tag-atos-interview-questions-and-answers","tag-capgemini-interview-questions-and-answers","tag-casting-networks-india-pvt-limited-interview-questions-and-answers","tag-cgi-group-inc-interview-questions-and-answers","tag-chetu-interview-questions-and-answers","tag-ciena-corporation-interview-questions-and-answers","tag-collabera-technologies-interview-questions-and-answers","tag-dell-international-services-india-pvt-ltd-interview-questions-and-answers","tag-fis-global-business-solutions-india-pvt-ltd-interview-questions-and-answers","tag-flipkart-interview-questions-and-answers","tag-for-each-loop-java","tag-ibm-interview-questions-and-answers","tag-indecomm-global-services-interview-questions-and-answers","tag-infosys-technologies-interview-questions-and-answers","tag-java-8-reduce","tag-java-add-to-array","tag-java-array","tag-java-array-class","tag-java-array-example","tag-java-array-length","tag-java-array-methods","tag-java-foreach-array","tag-java-initialize-array","tag-java-print-array","tag-java-sort-array","tag-java-stream-sum","tag-lt-infotech-interview-questions-and-answers","tag-mphasis-interview-questions-and-answers","tag-netapp-interview-questions-and-answers","tag-oracle-corporation-interview-questions-and-answers","tag-peoplestrong-interview-questions-and-answers","tag-persistent-systems-interview-questions-and-answers","tag-rbs-india-de-interview-questions-and-answers","tag-reliance-industries-ltd-interview-questions-and-answers","tag-remaxys-infotech-interview-questions-and-answers","tag-sap-labs-india-pvt-ltd-interview-questions-and-answers","tag-split-array-where-sum-of-left-hand-side-is-equal-to-sum-of-right-hand-side","tag-tech-mahindra-interview-questions-and-answers","tag-unitedhealth-group-interview-questions-and-answers","tag-virtusa-consulting-services-pvt-ltd-interview-questions-and-answers","tag-wells-fargo-interview-questions-and-answers","tag-wipro-infotech-interview-questions-and-answers"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to optimally divide an array into two subarrays - Java<\/title>\n<meta name=\"description\" content=\"How to optimally divide an array into two subarrays so that sum of elements in both are same ? - Given an array of integers greater than zero\" \/>\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.wikitechy.com\/interview-questions\/java\/how-to-optimally-divide-an-array-into-two-subarrays-so-that-sum-of-elements-in-both-are-same\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to optimally divide an array into two subarrays - Java\" \/>\n<meta property=\"og:description\" content=\"How to optimally divide an array into two subarrays so that sum of elements in both are same ? - Given an array of integers greater than zero\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-optimally-divide-an-array-into-two-subarrays-so-that-sum-of-elements-in-both-are-same\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-14T00:03:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-11T10:51:40+00:00\" \/>\n<meta name=\"author\" content=\"Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Editor\" \/>\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.wikitechy.com\/interview-questions\/java\/how-to-optimally-divide-an-array-into-two-subarrays-so-that-sum-of-elements-in-both-are-same\/\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-optimally-divide-an-array-into-two-subarrays-so-that-sum-of-elements-in-both-are-same\/\",\"name\":\"How to optimally divide an array into two subarrays - Java\",\"isPartOf\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\"},\"datePublished\":\"2021-07-14T00:03:02+00:00\",\"dateModified\":\"2021-09-11T10:51:40+00:00\",\"author\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\"},\"description\":\"How to optimally divide an array into two subarrays so that sum of elements in both are same ? - Given an array of integers greater than zero\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-optimally-divide-an-array-into-two-subarrays-so-that-sum-of-elements-in-both-are-same\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/\",\"name\":\"Wikitechy\",\"description\":\"Interview Questions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\",\"name\":\"Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"caption\":\"Editor\"},\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to optimally divide an array into two subarrays - Java","description":"How to optimally divide an array into two subarrays so that sum of elements in both are same ? - Given an array of integers greater than zero","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.wikitechy.com\/interview-questions\/java\/how-to-optimally-divide-an-array-into-two-subarrays-so-that-sum-of-elements-in-both-are-same\/","og_locale":"en_US","og_type":"article","og_title":"How to optimally divide an array into two subarrays - Java","og_description":"How to optimally divide an array into two subarrays so that sum of elements in both are same ? - Given an array of integers greater than zero","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-optimally-divide-an-array-into-two-subarrays-so-that-sum-of-elements-in-both-are-same\/","og_site_name":"Wikitechy","article_published_time":"2021-07-14T00:03:02+00:00","article_modified_time":"2021-09-11T10:51:40+00:00","author":"Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Editor","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-optimally-divide-an-array-into-two-subarrays-so-that-sum-of-elements-in-both-are-same\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-optimally-divide-an-array-into-two-subarrays-so-that-sum-of-elements-in-both-are-same\/","name":"How to optimally divide an array into two subarrays - Java","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"datePublished":"2021-07-14T00:03:02+00:00","dateModified":"2021-09-11T10:51:40+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757"},"description":"How to optimally divide an array into two subarrays so that sum of elements in both are same ? - Given an array of integers greater than zero","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-optimally-divide-an-array-into-two-subarrays-so-that-sum-of-elements-in-both-are-same\/"]}]},{"@type":"WebSite","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website","url":"https:\/\/www.wikitechy.com\/interview-questions\/","name":"Wikitechy","description":"Interview Questions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757","name":"Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","caption":"Editor"},"url":"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/663","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/comments?post=663"}],"version-history":[{"count":3,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/663\/revisions"}],"predecessor-version":[{"id":3398,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/663\/revisions\/3398"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}