{"id":40,"date":"2025-07-23T07:00:18","date_gmt":"2025-07-23T07:00:18","guid":{"rendered":"https:\/\/medium.com\/p\/bdd2ebcd028e"},"modified":"2025-07-23T13:04:06","modified_gmt":"2025-07-23T13:04:06","slug":"sort-array-after-converting-elements-to-their-squares","status":"publish","type":"post","link":"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/","title":{"rendered":"Sort array after converting elements to their squares"},"content":{"rendered":"<p>Given an array of both positive and negative integers \u2018$a[]\u2019. The challenge is to sort the square of the numbers of the array.<\/p>\n<p>Examples:<\/p>\n<pre>Input : $a = [-9, -4, 1, 2, 9];\r\nOutput : $b = [1, 4, 16, 81, 81];<\/pre>\n<pre>Input : $a = [-5, -4, -2, 0, 7];\r\nOutput : $b = [0, 4, 16, 25, 49];<\/pre>\n<p>Simple solution is to first convert each array element to its square and then apply any sorting algorithm (i.e. O(nlogn)).<\/p>\n<pre>&lt;?php\r\n\/\/ Enter your code here, enjoy!\r\nfunction sortNegativePositive($a, $n) {\r\n$b = [];\r\nfor($i = 0; $i &lt; $n; $i++) {\r\n$a[$i] = $a[$i] * $a[$i];\r\n}\r\n\r\nsort($a);\r\nreturn $a;\r\n}\r\n\r\n\/\/ Execution will start from here.\r\n$a = [-9, -4, 1, 2, 9];\r\n$n = count($a);\r\n\r\nprint_r($a); \/\/ before sort\r\n\r\n$a = sortNegativePositive($a, $n);\r\n\r\nprint_r($a); \/\/ after sort;<\/pre>\n<p>And the output is looks like<\/p>\n<pre>Array\r\n(\r\n [0] =&gt; -9\r\n [1] =&gt; -4\r\n [2] =&gt; 1\r\n [3] =&gt; 2\r\n [4] =&gt; 9\r\n)\r\nArray\r\n(\r\n [0] =&gt; 1\r\n [1] =&gt; 4\r\n [2] =&gt; 16\r\n [3] =&gt; 81\r\n [4] =&gt; 81\r\n)<\/pre>\n<p>Time complexity is : O(n log n) as we use some sorting function (sorting algorithm) to sort it.<\/p>\n<p>The effective and complex solution to sort it is<\/p>\n<ol>\n<li>Divide array into Negative and Positive parts, (i.e. 2 parts).<\/li>\n<li>Merge both sorted array into single sorted array.<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/medium.com\/_\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=bdd2ebcd028e\" alt=\"\" width=\"1\" height=\"1\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of both positive and negative integers \u2018$a[]\u2019. The challenge is to sort the square of the numbers of the array. Examples: Input&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[5],"tags":[51,52,50,49],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-php","tag-array-squares","tag-convert-array-into-squares","tag-square-of-negative-values","tag-square-of-the-array"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Sort array after converting elements to their squares - Practical Kishor<\/title>\n<meta name=\"description\" content=\"Given an array of both positive and negative integers \u2018$a[]\u2019. The challenge is to sort the square of the numbers of the array.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Sort array after converting elements to their squares - Practical Kishor\" \/>\n<meta property=\"og:description\" content=\"Given an array of both positive and negative integers \u2018$a[]\u2019. The challenge is to sort the square of the numbers of the array.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/\" \/>\n<meta property=\"og:site_name\" content=\"Practical Kishor\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-23T07:00:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-07-23T13:04:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/medium.com\/_\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=bdd2ebcd028e\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\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\":\"Article\",\"@id\":\"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\/\/kishor10d.com\/blogs\/#\/schema\/person\/682d05c56912dd235403d4d49862750a\"},\"headline\":\"Sort array after converting elements to their squares\",\"datePublished\":\"2025-07-23T07:00:18+00:00\",\"dateModified\":\"2025-07-23T13:04:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/\"},\"wordCount\":105,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/kishor10d.com\/blogs\/#\/schema\/person\/682d05c56912dd235403d4d49862750a\"},\"image\":{\"@id\":\"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/medium.com\/_\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=bdd2ebcd028e\",\"keywords\":[\"array squares\",\"convert array into squares\",\"square of negative values\",\"square of the array\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/\",\"url\":\"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/\",\"name\":\"Sort array after converting elements to their squares - Practical Kishor\",\"isPartOf\":{\"@id\":\"https:\/\/kishor10d.com\/blogs\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/medium.com\/_\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=bdd2ebcd028e\",\"datePublished\":\"2025-07-23T07:00:18+00:00\",\"dateModified\":\"2025-07-23T13:04:06+00:00\",\"description\":\"Given an array of both positive and negative integers \u2018$a[]\u2019. The challenge is to sort the square of the numbers of the array.\",\"breadcrumb\":{\"@id\":\"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/#primaryimage\",\"url\":\"https:\/\/medium.com\/_\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=bdd2ebcd028e\",\"contentUrl\":\"https:\/\/medium.com\/_\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=bdd2ebcd028e\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/kishor10d.com\/blogs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Sort array after converting elements to their squares\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/kishor10d.com\/blogs\/#website\",\"url\":\"https:\/\/kishor10d.com\/blogs\/\",\"name\":\"Practical Kishor\",\"description\":\"The quick brown fox jumps over the lazy dog\",\"publisher\":{\"@id\":\"https:\/\/kishor10d.com\/blogs\/#\/schema\/person\/682d05c56912dd235403d4d49862750a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/kishor10d.com\/blogs\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/kishor10d.com\/blogs\/#\/schema\/person\/682d05c56912dd235403d4d49862750a\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"logo\":{\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g\"},\"sameAs\":[\"https:\/\/kishor10d.com\/blogs\"],\"url\":\"https:\/\/kishor10d.com\/blogs\/author\/kishor10d\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Sort array after converting elements to their squares - Practical Kishor","description":"Given an array of both positive and negative integers \u2018$a[]\u2019. The challenge is to sort the square of the numbers of the array.","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:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/","og_locale":"en_US","og_type":"article","og_title":"Sort array after converting elements to their squares - Practical Kishor","og_description":"Given an array of both positive and negative integers \u2018$a[]\u2019. The challenge is to sort the square of the numbers of the array.","og_url":"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/","og_site_name":"Practical Kishor","article_published_time":"2025-07-23T07:00:18+00:00","article_modified_time":"2025-07-23T13:04:06+00:00","og_image":[{"url":"https:\/\/medium.com\/_\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=bdd2ebcd028e","type":"","width":"","height":""}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/#article","isPartOf":{"@id":"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/"},"author":{"name":"admin","@id":"https:\/\/kishor10d.com\/blogs\/#\/schema\/person\/682d05c56912dd235403d4d49862750a"},"headline":"Sort array after converting elements to their squares","datePublished":"2025-07-23T07:00:18+00:00","dateModified":"2025-07-23T13:04:06+00:00","mainEntityOfPage":{"@id":"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/"},"wordCount":105,"commentCount":0,"publisher":{"@id":"https:\/\/kishor10d.com\/blogs\/#\/schema\/person\/682d05c56912dd235403d4d49862750a"},"image":{"@id":"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/#primaryimage"},"thumbnailUrl":"https:\/\/medium.com\/_\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=bdd2ebcd028e","keywords":["array squares","convert array into squares","square of negative values","square of the array"],"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/","url":"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/","name":"Sort array after converting elements to their squares - Practical Kishor","isPartOf":{"@id":"https:\/\/kishor10d.com\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/#primaryimage"},"image":{"@id":"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/#primaryimage"},"thumbnailUrl":"https:\/\/medium.com\/_\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=bdd2ebcd028e","datePublished":"2025-07-23T07:00:18+00:00","dateModified":"2025-07-23T13:04:06+00:00","description":"Given an array of both positive and negative integers \u2018$a[]\u2019. The challenge is to sort the square of the numbers of the array.","breadcrumb":{"@id":"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/#primaryimage","url":"https:\/\/medium.com\/_\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=bdd2ebcd028e","contentUrl":"https:\/\/medium.com\/_\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=bdd2ebcd028e"},{"@type":"BreadcrumbList","@id":"https:\/\/kishor10d.com\/blogs\/sort-array-after-converting-elements-to-their-squares\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kishor10d.com\/blogs\/"},{"@type":"ListItem","position":2,"name":"Sort array after converting elements to their squares"}]},{"@type":"WebSite","@id":"https:\/\/kishor10d.com\/blogs\/#website","url":"https:\/\/kishor10d.com\/blogs\/","name":"Practical Kishor","description":"The quick brown fox jumps over the lazy dog","publisher":{"@id":"https:\/\/kishor10d.com\/blogs\/#\/schema\/person\/682d05c56912dd235403d4d49862750a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kishor10d.com\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/kishor10d.com\/blogs\/#\/schema\/person\/682d05c56912dd235403d4d49862750a","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g","caption":"admin"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g"},"sameAs":["https:\/\/kishor10d.com\/blogs"],"url":"https:\/\/kishor10d.com\/blogs\/author\/kishor10d\/"}]}},"_links":{"self":[{"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":2,"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":72,"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/posts\/40\/revisions\/72"}],"wp:attachment":[{"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}