{"id":1504,"date":"2021-04-24T05:43:46","date_gmt":"2021-04-24T05:43:46","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=1504"},"modified":"2021-04-24T05:43:48","modified_gmt":"2021-04-24T05:43:48","slug":"smallest-integer","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2021\/04\/24\/smallest-integer\/","title":{"rendered":"Smallest Integer"},"content":{"rendered":"<p>Code available <a href=\"https:\/\/github.com\/samarthya\/smallestinteger\">here<\/a>.<\/p>\n\n\n<p>The ask was to identify the smallest possible integer (> 0) in the array of integers. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>1. &#91;-1,2] -> 1\n2. &#91;-1,-2] -> 1\n3. &#91;1,3] -> 2<\/code><\/pre>\n\n\n\n<p>I was initially thinking of using <code>sets<\/code> and then finding out the missing one, but then reaized it is simpler and I do not need sets<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\n    public int solution(int&#91;] A) {\n        \/\/ write your code in Java SE 8\n        int smallestInt = 1;\n        if(A.length == 0) return smallestInt;\n        \/\/ Sort\n        Arrays.sort(A);\n\n        \/\/ If the first element is greater than 1\n        if(A&#91;0] > 1) return smallestInt;\n\n        \/\/If the last element is less than 1\n        if(A&#91; A.length - 1] &lt;= 0 ) return smallestInt;\n    \n        \/\/Iterate through the array and process\n        for(int i = 0; i &lt; A.length; i++){\n            if(A&#91;i] == smallestInt){\n                smallestInt++;}\n        }\n\n        return smallestInt;\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Code available here. The ask was to identify the smallest possible integer (> 0) in the array of integers. Example I was initially thinking of using sets and then finding out the missing one, but then reaized it is simpler and I do not need sets<\/p>\n","protected":false},"author":2,"featured_media":1407,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[120,34],"tags":[181,180],"class_list":["post-1504","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-java","category-technical","tag-algos","tag-java8","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1504","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=1504"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1504\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/1407"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=1504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=1504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=1504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}