{"id":25871,"date":"2017-10-25T21:05:57","date_gmt":"2017-10-25T15:35:57","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25871"},"modified":"2017-10-25T21:05:57","modified_gmt":"2017-10-25T15:35:57","slug":"boolean-array-puzzle","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/boolean-array-puzzle\/","title":{"rendered":"A Boolean Array Puzzle"},"content":{"rendered":"<p><strong>Input:<\/strong> A array arr[] of two elements having value 0 and 1<\/p>\n<p><strong>Output:<\/strong> Make both elements 0.<\/p>\n<p><strong>Specifications:<\/strong> Following are the specifications to follow.<br \/>\n1) It is guaranteed that one element is 0 but we do not know its position.<br \/>\n2) We can\u2019t say about another element it can be 0 or 1.<br \/>\n3) We can only complement array elements, no other operation like and, or, multi, division, \u2026. etc.<br \/>\n4) We can\u2019t use if, else and loop constructs.<br \/>\n5) Obviously, we can\u2019t directly assign 0 to array elements.<\/p>\n<p>There are several ways we can do it as we are sure that always one Zero is there. Thanks to devendraiiit for suggesting following 3 methods.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Method 1<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">c<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">void changeToZero(int a[2])<br\/>{<br\/>   a[ a[1] ] = a[ !a[1] ];<br\/>}<br\/> <br\/>int main()<br\/>{<br\/>   int a[] = {1, 0};<br\/>   changeToZero(a);<br\/> <br\/>   printf(&quot; arr[0] = %d \\n&quot;, a[0]);<br\/>   printf(&quot; arr[1] = %d &quot;, a[1]);<br\/>   getchar();<br\/>   return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Method 2<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">c<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">void changeToZero(int a[2])<br\/>{<br\/>    a[ !a[0] ] = a[ !a[1] ]<br\/>}<\/code><\/pre> <\/div>\n<div class=\"line number1 index0 alt2\"><code class=\"c keyword bold\"><\/code><\/div>\n<div class=\"line number1 index0 alt2\">\n<p><strong>Method 3<\/strong><\/p>\n<p>This method doesn\u2019t even need complement.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">c<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">void changeToZero(int a[2])<br\/>{<br\/>    a[ a[1] ] = a[ a[0] ]<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Method 4<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">c<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">void changeToZero(int a[2])<br\/>{<br\/>  a[0] = a[a[0]];<br\/>  a[1] = a[0];<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><code class=\"c plain\"><\/code><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>A Boolean Array Puzzle &#8211; Bit Algorithm &#8211; Following are the specifications to follow. It is guaranteed that one element is 0 but we do not know its position<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,74852,83568],"tags":[75664,75665,75688,73135,75677,75683,75682,72715,75653,75675,75684,75654,75669,75656,75662,75657,75655,75680,75674,75667,75671,75651,74828,75652,75666,75686,75588,74064,75687,75678,75663,75661,75583,75590,75668,75660,72725,74093,75672,75670,75673,74059,73146,75659,75679,75685],"class_list":["post-25871","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-bit-algorithms","category-boolean-array","tag-algorithm-and-flowchart-in-c","tag-algorithm-flowchart","tag-algorithm-for-greatest-of-three-numbers","tag-ascending-order-program-in-c","tag-biggest-number","tag-c-program-for-largest-of-3-numbers","tag-c-program-to-find-average-of-3-numbers-using-function","tag-c-program-to-find-smallest-number-in-an-array","tag-c-program-to-find-the-greatest-of-three-numbers","tag-c-program-using-ternary-operator","tag-comparing-3-numbers-in-java","tag-conditional-operator-in-c","tag-example-of-flowchart","tag-example-of-problem-with-algorithm-and-flowchart","tag-flow-chart-example","tag-flowchart-algorithm","tag-flowchart-and-algorithm","tag-flowchart-example-problems","tag-flowchart-examples","tag-flowchart-examples-programming","tag-flowchart-in-c","tag-how-to-write-pseudocode","tag-if-else-c-programming","tag-largest-4-digit-number","tag-largest-5-digit-number","tag-max-of-3-numbers","tag-numbers-divisible-by-4","tag-numbers-divisible-by-7","tag-pl-sql-program-to-find-greatest-of-three-numbers","tag-program-to-find-greatest-of-three-numbers-in-java","tag-pseudocode-and-flowchart-examples","tag-pseudocode-examples","tag-smallest-2-digit-number","tag-smallest-4-digit-number","tag-smallest-5-digit-number","tag-smallest-8-digit-number","tag-smallest-c-program","tag-smallest-even-number","tag-smallest-five-digit-number","tag-smallest-four-digit-number","tag-smallest-one-digit-number","tag-smallest-three-digit-number","tag-sorting-program-in-c","tag-ternary-operator-in-c","tag-write-75-as-a-product-of-prime-factors","tag-write-a-program-to-find-largest-of-three-numbers"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25871","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25871"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25871\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}