{"id":18338,"date":"2022-09-06T18:01:59","date_gmt":"2022-09-06T12:31:59","guid":{"rendered":"https:\/\/copyassignment.com\/?p=18338"},"modified":"2022-12-06T13:38:05","modified_gmt":"2022-12-06T08:08:05","slug":"smart-number-hackerrank-solution","status":"publish","type":"post","link":"https:\/\/copyassignment.com\/smart-number-hackerrank-solution\/","title":{"rendered":"Smart Number Hackerrank Solution"},"content":{"rendered":"\n<p>Given some numbers, we need to find whether they are smart numbers or not. If they are smart we should print &#8220;YES&#8221; otherwise print &#8220;NO&#8221;.<\/p>\n\n\n\n<p>A smart number is defined as a number that has an odd number of factors.<\/p>\n\n\n\n<p>We have to perform debugging of the code. We can only modify one line of code and can not add or delete any line.<\/p>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Given Code<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import math\n\ndef is_smart_number(num):\n    val = int(math.sqrt(num))\n    if num \/ val == 1:\n        return True\n    return False\n\nfor _ in range(int(input())):\n    num = int(input())\n    ans = is_smart_number(num)\n    if ans:\n        print(\"YES\")\n    else:\n        print(\"NO\")<\/code><\/pre>\n\n\n\n<p>We can only modify one line in this code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Approach for Smart Number Hackerrank Solution<\/h2>\n\n\n\n<p>The different factors of a number form pairs among each other. If x is a factor of a number n, we know that <strong><em>n\/x<\/em><\/strong> will also be a factor of that number. For example, 2 is a factor of 10, then 10\/2, 5 is also a factor of 10.<\/p>\n\n\n\n<p>This is the approach that we use to find factors of a number in <strong><em>O(sqrt(n))<\/em><\/strong> time complexity. We just loop through all numbers till <strong><em>sqrt(n)<\/em><\/strong>. If it is a factor we add that number and also n divided by that number to the list of factors. So we always get a pair of factors.<\/p>\n\n\n\n<p>The only time we can have an odd number of factors is if <strong><em>x = n\/x<\/em><\/strong>. Thus the factor is its own pair. This equation can be simplified as <strong><em>n = x^2<\/em><\/strong> Or <strong><em>x = sqrt(n)<\/em><\/strong>.<\/p>\n\n\n\n<p>So, if an integer square root of n exists that is if n is a perfect square, we get an odd number of factors, making it a special number.<\/p>\n\n\n\n<p>Thus, we can update the condition to check for this.<\/p>\n\n\n\n<p><strong><em> if num \/ val == 1: <\/em><\/strong><\/p>\n\n\n\n<p>becomes<\/p>\n\n\n\n<p><strong><em> if num \/ val == val:<\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Smart Number Hackerrank Solution<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import math\n\ndef is_smart_number(num):\n    val = int(math.sqrt(num))\n    if num \/ val == val:\n        return True\n    return False\n\nfor _ in range(int(input())):\n    num = int(input())\n    ans = is_smart_number(num)\n    if ans:\n        print(\"YES\")\n    else:\n        print(\"NO\")<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output:<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"573\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/09\/image-31-1024x573.png\" alt=\"Output for Smart Number Hackerrank Solution\" class=\"wp-image-18342 lazyload\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/09\/image-31-1024x573.png 1024w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/09\/image-31-300x168.png 300w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/09\/image-31-768x430.png 768w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/09\/image-31-1026x574.png 1026w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/09\/image-31-675x378.png 675w, https:\/\/copyassignment.com\/wp-content\/uploads\/2022\/09\/image-31.png 1205w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/573;\" \/><\/figure>\n\n\n\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-9886351916045880\"\n     crossorigin=\"anonymous\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-9886351916045880\"\n     data-ad-slot=\"7933252109\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<div style=\"text-align:center\" class=\"wp-block-atomic-blocks-ab-button ab-block-button\"><a href=\"https:\/\/copyassignment.com\/top-100-python-projects-with-source-code\/\" class=\"ab-button ab-button-shape-rounded ab-button-size-medium\" style=\"color:#ffffff;background-color:#3373dc\">Best 100+ Python Projects with source code<\/a><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Also Read:<\/strong><\/p>\n\n\n<ul class=\"wp-block-latest-posts__list is-grid columns-3 wp-block-latest-posts\"><li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/download-1000-projects-all-b-tech-programming-notes-job-resume-interview-guide-and-more-get-your-ultimate-programming-bundle\/\">Download 1000+ Projects, All B.Tech &#038; Programming Notes, Job, Resume &#038; Interview Guide, and More &#8211; Get Your Ultimate Programming Bundle!<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-8-solution-in-python-dictionaries-and-maps\/\">HackerRank Day 8 Solution in Python: Dictionaries and Maps<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-7-solution-in-python-arrays\/\">HackerRank Day 7 Solution in Python: Arrays<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-6-solution-in-python-lets-review\/\">HackerRank Day 6 Solution in Python: Let&#8217;s review<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-5-solution-in-python-loops\/\">HackerRank Day 5 Solution in Python: Loops<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-4-solution-in-python\/\">HackerRank Day 4 Solution in Python: Class vs Instance<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-3-solution-in-python\/\">HackerRank Day 3 Solution in Python: Intro to Conditional Statements<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-2-solution-in-python-operators\/\">HackerRank Day 2 Solution in Python: Operators<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-1-solution-in-python-data-types\/\">HackerRank Day 1 Solution in Python: Data Types<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-0-solution-in-python-hello-world\/\">HackerRank Day 0 Solution in Python: Hello World<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-29-solution-in-python-bitwise-and\/\">HackerRank Day 29 Solution in Python: Bitwise AND<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-28-solution-in-python-regex-patterns-and-intro-to-databases\/\">HackerRank Day 28 Solution in Python: RegEx, Patterns, and Intro to databases<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-27-solution-in-python-testing\/\">HackerRank Day 27 Solution in Python: Testing<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-26-solution-in-python-nested-logic\/\">HackerRank Day 26 Solution in Python: Nested Logic<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-25-solution-in-python-running-time-and-complexity\/\">HackerRank Day 25 Solution in Python: Running Time and Complexity<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-24-solution-in-python-more-linked-lists\/\">HackerRank Day 24 Solution in Python: More Linked Lists<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-23-solution-in-python-bst-level-order-traversal\/\">HackerRank Day 23 Solution in Python: BST Level Order Traversal<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-22-solution-in-python-binary-search-trees\/\">HackerRank Day 22 Solution in Python: Binary Search Trees<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/find-peak-element-leetcode-162\/\">Find Peak Element LeetCode 162<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-20-solution-in-python-sorting\/\">HackerRank Day 20 Solution in Python: Sorting<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-19-solution-in-python-interfaces\/\">HackerRank Day 19 Solution in Python: Interfaces<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-18-solution-in-python-queues-and-stacks\/\">HackerRank Day 18 Solution in Python: Queues and Stacks<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-17-solution-in-python-more-exceptions\/\">HackerRank Day 17 Solution in Python: More Exceptions<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-16-solution-exceptions-string-to-integer\/\">HackerRank Day 16 Solution: Exceptions &#8211; String to Integer<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/explained-allocate-minimum-number-of-pages\/\">Explained: Allocate minimum number of pages<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-15-solution-in-python-linked-list\/\">HackerRank Day 15 Solution in Python: Linked List<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/search-a-2d-matrix-leetcode-74\/\">Search a 2D matrix: leetcode 74<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/maximum-subarray-sum-kadanes-algorithm\/\">Maximum Subarray Sum: Kadane&#8217;s Algorithm<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-13-solution-in-python-abstract-classes\/\">HackerRank Day 13 Solution in Python: Abstract Classes<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-14-solution-in-python-scope\/\">HackerRank Day 14 Solution in Python: Scope<\/a><\/li>\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Given some numbers, we need to find whether they are smart numbers or not. If they are smart we should print &#8220;YES&#8221; otherwise print &#8220;NO&#8221;&#8230;.<\/p>\n","protected":false},"author":62,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,1306],"tags":[],"class_list":["post-18338","post","type-post","status-publish","format-standard","hentry","category-allcategorites","category-competitive-programming","wpcat-22-id","wpcat-1306-id"],"_links":{"self":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/18338","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/users\/62"}],"replies":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/comments?post=18338"}],"version-history":[{"count":0,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/18338\/revisions"}],"wp:attachment":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/media?parent=18338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/categories?post=18338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/tags?post=18338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}