Sort ugly numbers in an array at their relative positions
Last Updated :
21 Jun, 2022
Given an integer array arr[], the task is to sort only those elements which are ugly numbers at their relative positions in the array (positions of other elements must not be affected).
Ugly numbers are numbers whose only prime factors are 2, 3 or 5.
The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ….. shows first few ugly numbers. By convention, 1 is included.
Examples:
Input: arr[] = {13, 9, 11, 3, 2}
Output: 13 2 11 3 9
9, 3 and 2 are the only ugly numbers in the given array.
Input: arr[] = {1, 2, 3, 7, 12, 10}
Output: 1 2 3 7 10 12
Approach:
- Start traversing the array and for every element arr[i], if arr[i] is an ugly number then store it in an ArrayList and update arr[i] = -1
- After all the ugly numbers have been stored in the ArrayList, sort the updated ArrayList.
- Again traverse the array and for every element,
- If arr[i] = -1 then print the first element from the ArrayList that hasn’t been printed before.
- Else, print arr[i].
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
bool isUgly( int n)
{
while (n % 2 == 0)
n = n / 2;
while (n % 3 == 0)
n = n / 3;
while (n % 5 == 0)
n = n / 5;
if (n == 1)
return true ;
return false ;
}
void sortUglyNumbers( int arr[], int n)
{
vector< int > list;
int i;
for (i = 0; i < n; i++)
{
if (isUgly(arr[i]))
{
list.push_back(arr[i]);
arr[i] = -1;
}
}
sort(list.begin(),list.end());
int j = 0;
for (i = 0; i < n; i++)
{
if (arr[i] == -1)
cout << list[j++] << " " ;
else
cout << arr[i] << " " ;
}
}
int main()
{
int arr[] = { 1, 2, 3, 7, 12, 10 };
int n = sizeof (arr)/ sizeof (arr[0]);
sortUglyNumbers(arr, n);
}
|
Java
import java.util.ArrayList;
import java.util.Collections;
class GFG {
static boolean isUgly( int n)
{
while (n % 2 == 0 )
n = n / 2 ;
while (n % 3 == 0 )
n = n / 3 ;
while (n % 5 == 0 )
n = n / 5 ;
if (n == 1 )
return true ;
return false ;
}
static void sortUglyNumbers( int arr[], int n)
{
ArrayList<Integer> list = new ArrayList<>();
int i;
for (i = 0 ; i < n; i++) {
if (isUgly(arr[i])) {
list.add(arr[i]);
arr[i] = - 1 ;
}
}
Collections.sort(list);
int j = 0 ;
for (i = 0 ; i < n; i++) {
if (arr[i] == - 1 )
System.out.print(list.get(j++) + " " );
else
System.out.print(arr[i] + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 7 , 12 , 10 };
int n = arr.length;
sortUglyNumbers(arr, n);
}
}
|
Python3
def isUgly(n):
while n % 2 = = 0 :
n = n / / 2
while n % 3 = = 0 :
n = n / / 3
while n % 5 = = 0 :
n = n / / 5
if n = = 1 :
return True
return False
def sortUglyNumbers(arr, n):
list = []
for i in range ( 0 , n):
if isUgly(arr[i]):
list .append(arr[i])
arr[i] = - 1
list .sort()
j = 0
for i in range ( 0 , n):
if arr[i] = = - 1 :
print ( list [j], end = " " )
j + = 1
else :
print (arr[i], end = " " )
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 7 , 12 , 10 ]
n = len (arr)
sortUglyNumbers(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static bool isUgly( int n)
{
while (n % 2 == 0)
n = n / 2;
while (n % 3 == 0)
n = n / 3;
while (n % 5 == 0)
n = n / 5;
if (n == 1)
return true ;
return false ;
}
static void sortUglyNumbers( int []arr, int n)
{
List< int > list = new List< int >();
int i;
for (i = 0; i < n; i++)
{
if (isUgly(arr[i]))
{
list.Add(arr[i]);
arr[i] = -1;
}
}
list.Sort();
int j = 0;
for (i = 0; i < n; i++)
{
if (arr[i] == -1)
Console.Write(list[j++] + " " );
else
Console.Write(arr[i] + " " );
}
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 7, 12, 10 };
int n = arr.Length;
sortUglyNumbers(arr, n);
}
}
|
Javascript
<script>
function isUgly(n)
{
while (n % 2 == 0)
n = n / 2;
while (n % 3 == 0)
n = n / 3;
while (n % 5 == 0)
n = n / 5;
if (n == 1)
return true ;
return false ;
}
function sortUglyNumbers(arr, n)
{
var list = [];
var i;
for (i = 0; i < n; i++)
{
if (isUgly(arr[i]))
{
list.push(arr[i]);
arr[i] = -1;
}
}
list.sort((a,b)=>a-b);
var j = 0;
for (i = 0; i < n; i++)
{
if (arr[i] == -1)
document.write( list[j++] + " " );
else
document.write( arr[i] + " " );
}
}
var arr = [1, 2, 3, 7, 12, 10 ];
var n = arr.length;
sortUglyNumbers(arr, n);
</script>
|
Time Complexity: O(nlog(n))
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...