Sort an array by left shifting digits of array elements
Last Updated :
30 Sep, 2022
Given an array arr[] consisting of N positive integers, the task is to left-shift the digits of array elements such that the array modifies to a sorted form. If multiple solutions exist, then print any one of them. Otherwise, print -1.
Examples:
Input: arr[] = { 511, 321, 323, 432 }
Output: { 115, 132, 233, 243 }
Explanation:
Left shift the digits of arr[0] by 1 modifies arr[0] to 115.
Left shift the digits of arr[1] by 2 modifies arr[1] to 132
Left shift the digits of arr[2] by 1 modifies arr[2] to 233
Left shift the digits of arr[3] by 1 modifies arr[3] to 243
Input: { 5, 1, 2, 3, 3 }
Output: -1
Approach: Follow the steps below to solve the problem:
- The idea is to left-shift the digits of each array element such that the current element is the nearest greater element of the previous array elements.
- Traverse the array and shift the digits of array elements in all possible ways and pick the one which is minimum, but greater than the previous array element. If it is not possible to find such elements, then print -1.
- Otherwise, print the array elements after performing the above operations
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isIncreasing(vector< int > arr)
{
for ( int i = 0; i < arr.size() - 1; i++)
{
if (arr[i] > arr[i + 1])
return false ;
}
return true ;
}
vector< int > sortArr(vector< int > arr)
{
int prev = -1;
for ( int i = 0; i < arr.size(); i++)
{
int optEle = arr[i];
string strEle = to_string(arr[i]);
for ( int idx = 0; idx < strEle.size(); idx++)
{
string strEle2 = strEle.substr(idx) +
strEle.substr(0, idx);
int temp = stoi(strEle2);
if (temp >= prev && temp < optEle)
optEle = temp;
}
arr[i] = optEle;
prev = arr[i];
}
if (isIncreasing(arr))
return arr;
else
{
arr = { -1 };
return arr;
}
}
int main()
{
vector< int > arr = { 511, 321, 323, 432, 433 };
vector< int > res = sortArr(arr);
for ( int i = 0; i < res.size(); i++)
cout << res[i] << " " ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean isIncreasing( int []arr)
{
for ( int i = 0 ; i < arr.length - 1 ; i++)
{
if (arr[i] > arr[i + 1 ])
return false ;
}
return true ;
}
static int [] sortArr( int []arr)
{
int prev = - 1 ;
for ( int i = 0 ; i < arr.length; i++)
{
int optEle = arr[i];
String strEle = String.valueOf(arr[i]);
for ( int idx = 0 ; idx < strEle.length(); idx++)
{
String strEle2 = strEle.substring(idx) +
strEle.substring( 0 , idx);
int temp = Integer.valueOf(strEle2);
if (temp >= prev && temp < optEle)
optEle = temp;
}
arr[i] = optEle;
prev = arr[i];
}
if (isIncreasing(arr))
return arr;
else
{
return new int []{ - 1 };
}
}
public static void main(String[] args)
{
int []arr = { 511 , 321 , 323 , 432 , 433 };
int []res = sortArr(arr);
for ( int i = 0 ; i < res.length; i++)
System.out.print(res[i]+ " " );
}
}
|
Python3
def isIncreasing(arr):
for i in range ( len (arr) - 1 ):
if arr[i] > arr[i + 1 ]:
return False
return True
def sortArr(arr):
prev = - 1
for i in range ( len (arr)):
optEle = arr[i]
strEle = str (arr[i])
for idx in range ( len (strEle)):
temp = int (strEle[idx:] + strEle[:idx])
if temp > = prev and temp < optEle:
optEle = temp
arr[i] = optEle
prev = arr[i]
if isIncreasing(arr):
return arr
else :
return "-1"
if __name__ = = '__main__' :
arr = [ 511 , 321 , 323 , 432 , 433 ]
res = sortArr(arr)
for i in res:
print (i, end = " " )
|
C#
using System;
public class GFG
{
static bool isIncreasing( int []arr)
{
for ( int i = 0; i < arr.Length - 1; i++)
{
if (arr[i] > arr[i + 1])
return false ;
}
return true ;
}
static int [] sortArr( int []arr)
{
int prev = -1;
for ( int i = 0; i < arr.Length; i++)
{
int optEle = arr[i];
String strEle = String.Join( "" ,arr[i]);
for ( int idx = 0; idx < strEle.Length; idx++)
{
String strEle2 = strEle.Substring(idx) +
strEle.Substring(0, idx);
int temp = Int32.Parse(strEle2);
if (temp >= prev && temp < optEle)
optEle = temp;
}
arr[i] = optEle;
prev = arr[i];
}
if (isIncreasing(arr))
return arr;
else
{
return new int []{ -1 };
}
}
public static void Main(String[] args)
{
int []arr = { 511, 321, 323, 432, 433 };
int []res = sortArr(arr);
for ( int i = 0; i < res.Length; i++)
Console.Write(res[i]+ " " );
}
}
|
Javascript
<script>
function isIncreasing(arr)
{
for (let i = 0; i < arr.length - 1; i++)
{
if (arr[i] > arr[i + 1])
return false ;
}
return true ;
}
function sortArr(arr)
{
let prev = -1;
for (let i = 0; i < arr.length; i++)
{
let optEle = arr[i];
let strEle = arr[i].toString();
for (let idx = 0; idx < strEle.length; idx++)
{
let strEle2 = strEle.substr(idx) +
strEle.substr(0, idx);
let temp = parseInt(strEle2);
if (temp >= prev && temp < optEle)
optEle = temp;
}
arr[i] = optEle;
prev = arr[i];
}
if (isIncreasing(arr))
return arr;
else
{
arr = [ -1 ];
return arr;
}
}
let arr = [ 511, 321, 323, 432, 433 ];
let res = sortArr(arr);
for (let i = 0; i < res.length; i++)
document.write(res[i] + " " );
</script>
|
Output:
115 132 233 243 334
Time Complexity: O(N)
Auxiliary Space:O(N)
Share your thoughts in the comments
Please Login to comment...