{"id":338,"date":"2023-01-13T14:38:06","date_gmt":"2023-01-13T14:38:06","guid":{"rendered":"https:\/\/premsvmm.com\/?p=338"},"modified":"2023-01-13T14:38:06","modified_gmt":"2023-01-13T14:38:06","slug":"find-value-of-x-from-array-pair","status":"publish","type":"post","link":"https:\/\/premsvmm.com\/index.php\/2023\/01\/13\/find-value-of-x-from-array-pair\/","title":{"rendered":"Find value of x from array pair"},"content":{"rendered":"\n<pre class=\"wp-block-preformatted\">Find a pair that match to the given value x from the input array.<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Input : [1,5,10,3,4] X=15<\/p>\n\n\n\n<p>Output: [5,10]<\/p>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;eclipse&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Java&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;java&quot;}\">package wordpress;\n\nimport java.util.*;\n\npublic class FindX {\n\n    public static HashSet&lt;List&lt;Integer&gt;&gt; findXFromGivenArray(Integer[] input, int x) {\n        HashSet&lt;List&lt;Integer&gt;&gt; result = new HashSet&lt;&gt;();\n        HashSet&lt;Integer&gt; hashSet = new HashSet&lt;&gt;();\n        for (Integer value : input) {\n            hashSet.add(value);\n        }\n        for (Integer value : input) {\n            List&lt;Integer&gt; pair = new LinkedList&lt;&gt;();\n            if (hashSet.contains(x - value)) {\n                pair.add(value);\n                pair.add(x - value);\n                result.add(pair);\n                hashSet.remove(value);\n            }\n        }\n        return result;\n    }\n\n    public static void main(String[] args) {\n        System.out.println(findXFromGivenArray(new Integer[]{1, 5, 10, 3, 4}, 15)); \/\/ [5,10]\n        System.out.println(findXFromGivenArray(new Integer[]{1, 2, 3, 4}, 4)); \/\/ [1,3] [2,2]\n        System.out.println(findXFromGivenArray(new Integer[]{1,2}, 10)); \/\/ []\n        System.out.println(findXFromGivenArray(new Integer[]{-1,-3,-8,-5}, -4)); \/\/ [-1,-3]\n    }\n}\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Find a pair that match to the given value x from the input array. Input : [1,5,10,3,4] X=15 Output: [5,10] Code:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[10],"tags":[11],"_links":{"self":[{"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/posts\/338"}],"collection":[{"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/comments?post=338"}],"version-history":[{"count":1,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/posts\/338\/revisions"}],"predecessor-version":[{"id":339,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/posts\/338\/revisions\/339"}],"wp:attachment":[{"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/media?parent=338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/categories?post=338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/premsvmm.com\/index.php\/wp-json\/wp\/v2\/tags?post=338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}