Given a number N, the task is to remove the adjacent duplicates of a number N and then sort the digits of the number N in decreasing order.
Example:
Input: N=11123134
Output: 433211Input: N=22133455
Output: 54321
Approach: Follow the below steps to solve the below problem:
- Create a map to store the frequency of all characters, say mp.
- Create a function getDigits which will accept an integer as a parameter and will return it in a form of a string.
- Iterate on that string and only increment the frequency of the digits that don’t have a duplicate at their previous index.
- Iterate on the map mp and create a string of integers, which will automatically be in sorted order.
- Convert that string to integer and print the answer accordingly.
Below is the implementation of the above approach.
C++
// C++ code for the above approach #include <bits/stdc++.h> using namespace std;
// Function to change integer N to string string getDigits( int N) { return to_string(N); }
// Function to remove the adjacent duplicates // of a number N and then sort the digits // of the number N in decreasing order int getSolution( int N)
{ string s = getDigits(N);
if (s.size() == 1) {
return stoi(s);
}
map< int , int > mp;
for ( int i = 1; i < s.size(); ++i) {
if (s[i] != s[i - 1]) {
mp[s[i - 1] - '0' ]++;
}
}
mp[s[s.size() - 1] - '0' ]++;
// Creating the number
int mul = 1;
int ans = 0;
for ( auto x : mp) {
for ( int i = 0; i < x.second; i++) {
ans += x.first * mul;
mul *= 10;
}
}
// Returning the created number
return ans;
} // Driver Code int main()
{ int N = 11123134;
cout << getSolution(N);
} |
Java
// Java code for the above approach import java.util.*;
class GFG{
// Function to change integer N to String static String getDigits( int N) { return String.valueOf(N); }
// Function to remove the adjacent duplicates // of a number N and then sort the digits // of the number N in decreasing order static int getSolution( int N)
{ char []s = getDigits(N).toCharArray();
if (s.length == 1 ) {
return Integer.valueOf(String.valueOf(s));
}
HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>();
for ( int i = 1 ; i < s.length; ++i) {
if (s[i] != s[i - 1 ]) {
if (mp.containsKey(s[i - 1 ] - '0' )){
mp.put(s[i - 1 ] - '0' , mp.get(s[i - 1 ] - '0' )+ 1 );
}
else
mp.put(s[i - 1 ] - '0' , 1 );
}
}
if (mp.containsKey(s[s.length - 1 ] - '0' )){
mp.put(s[s.length - 1 ] - '0' , mp.get(s[s.length - 1 ] - '0' )+ 1 );
}
else
mp.put(s[s.length - 1 ] - '0' , 1 );
// Creating the number
int mul = 1 ;
int ans = 0 ;
for (Map.Entry<Integer,Integer> x : mp.entrySet()) {
for ( int i = 0 ; i < x.getValue(); i++) {
ans += x.getKey() * mul;
mul *= 10 ;
}
}
// Returning the created number
return ans;
} // Driver Code public static void main(String[] args)
{ int N = 11123134 ;
System.out.print(getSolution(N));
} } // This code is contributed by 29AjayKumar |
Python3
# Python code for the above approach # Function to change integer N to string def getDigits(N) :
return str (N);
# Function to remove the adjacent duplicates # of a number N and then sort the digits # of the number N in decreasing order def getSolution(N):
s = getDigits(N);
if ( len (s) = = 1 ):
return int (s);
mp = {}
for i in range ( 1 , len (s)):
if (s[i] ! = s[i - 1 ]):
if (( ord (s[i - 1 ]) - ord ( '0' )) in mp):
mp[ ord (s[i - 1 ]) - ord ( '0' )] + = 1 ;
else :
mp[ ord (s[i - 1 ]) - ord ( '0' )] = 1 ;
if (s[ - 1 ] in mp):
mp[ ord (s[ - 1 ]) - ord ( '0' )] + = 1
else :
mp[ ord (s[ - 1 ]) - ord ( '0' )] = 1
# Creating the number
mul = 1 ;
ans = 0 ;
for x in mp:
for i in range (mp[x]):
ans = ans + x * mul;
mul = mul * 10 ;
# Returning the created number
return ans;
# Driver Code N = 11123134 ;
print (getSolution(N));
# This code is contributed by gfgking |
C#
// C# code for the above approach using System;
using System.Collections.Generic;
class GFG{
// Function to change integer N to String static string getDigits( int N)
{ return (N).ToString();
} // Function to remove the adjacent duplicates // of a number N and then sort the digits // of the number N in decreasing order static int getSolution( int N)
{ char [] s = getDigits(N).ToCharArray();
if (s.Length == 1)
{
return Int32.Parse((s).ToString());
}
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 1; i < s.Length; ++i)
{
if (s[i] != s[i - 1])
{
if (mp.ContainsKey(s[i - 1] - '0' ))
{
mp[s[i - 1] - '0' ]++;
}
else
mp[s[i - 1] - '0' ] = 1;
}
}
if (mp.ContainsKey(s[s.Length - 1] - '0' ))
{
mp[s[s.Length - 1] - '0' ] += 1;
}
else
mp[s[s.Length - 1] - '0' ] = 1;
// Creating the number
int mul = 1;
int ans = 0;
foreach (KeyValuePair< int , int > x in mp)
{
for ( int i = 0; i < x.Value; i++)
{
ans += x.Key * mul;
mul *= 10;
}
}
// Returning the created number
return ans;
} // Driver Code public static void Main( string [] args)
{ int N = 11123134;
Console.WriteLine(getSolution(N));
} } // This code is contributed by ukasp |
Javascript
<script> // JavaScript code for the above approach // Function to change integer N to string function getDigits(N)
{ return N.toString();
} // Function to remove the adjacent duplicates // of a number N and then sort the digits // of the number N in decreasing order function getSolution(N)
{ let s = getDigits(N);
if (s.length == 1)
{
return parseInt(s);
}
let mp = new Map();
for (let i = 1; i < s.length; ++i)
{
if (s[i] != s[i - 1])
{
if (mp.has(s[i - 1].charCodeAt(0) -
'0' .charCodeAt(0)))
{
mp.set(s[i - 1].charCodeAt(0) -
'0' .charCodeAt(0),
mp.get(s[i - 1].charCodeAt(0) -
'0' .charCodeAt(0)) + 1);
}
else
{
mp.set(s[i - 1].charAt(0) -
'0' .charAt(0), 1);
}
}
}
if (mp.has(s[s.length - 1]))
mp.set(s[s.length - 1].charCodeAt(0) -
'0' .charCodeAt(0),
mp.get(s[s.length - 1].charCodeAt(0) -
'0' .charCodeAt(0)) + 1);
else
mp.set(s[s.length - 1].charCodeAt(0) -
'0' .charCodeAt(0), 1)
// Creating the number
let mul = 1;
let ans = 0;
for (let [key, value] of mp)
{
for (let i = 0; i < value; i++)
{
ans = ans + key * mul;
mul = mul * 10;
}
}
// Returning the created number
return ans;
} // Driver Code let N = 11123134; document.write(getSolution(N)); // This code is contributed by Potta Lokesh </script> |
Output
433211
Time Complexity: O(N)
Auxiliary Space: O(N)