给定一个由N 个正整数组成的数组arr[] ,任务是左移数组元素的数字,使数组修改为排序形式。如果存在多个解决方案,则打印其中任何一个。否则,打印-1 。
例子:
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
处理方法:按照以下步骤解决问题:
- 这个想法是左移每个数组元素的数字,使得当前元素是前一个数组元素中最近的较大元素。
- 遍历数组并以所有可能的方式移动数组元素的数字,并选择最小但大于前一个数组元素的数字。如果找不到这样的元素,则打印-1 。
- 否则,执行上述操作后打印数组元素
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if an array is
// sorted in increasing order or not
bool isIncreasing(vector arr)
{
// Traverse the array
for(int i = 0; i < arr.size() - 1; i++)
{
if (arr[i] > arr[i + 1])
return false;
}
return true;
}
// Function to sort the array
// by left shifting digits of
// array elements
vector sortArr(vector arr)
{
// Stores previous array
// element
int prev = -1;
// Travere the array arr[]
for(int i = 0; i < arr.size(); i++)
{
// Stores current element
int optEle = arr[i];
// Stores current element
// in string format
string strEle = to_string(arr[i]);
// Left-shift digits of current
// element in all possible ways
for(int idx = 0; idx < strEle.size(); idx++)
{
// Left-shift digits of current
// element by idx
string strEle2 = strEle.substr(idx) +
strEle.substr(0, idx);
int temp = stoi(strEle2);
// If temp greater than or equal to
// prev and temp less than optEle
if (temp >= prev && temp < optEle)
optEle = temp;
}
// Update arr[i]
arr[i] = optEle;
// Update prev
prev = arr[i];
}
// If arr is in increasing order
if (isIncreasing(arr))
return arr;
// Otherwise
else
{
arr = { -1 };
return arr;
}
}
// Driver Code
int main()
{
vector arr = { 511, 321, 323, 432, 433 };
vector res = sortArr(arr);
for(int i = 0; i < res.size(); i++)
cout << res[i] << " ";
return 0;
}
// This code is contributed by subhammahato348
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to check if an array is
// sorted in increasing order or not
static boolean isIncreasing(int []arr)
{
// Traverse the array
for(int i = 0; i < arr.length - 1; i++)
{
if (arr[i] > arr[i + 1])
return false;
}
return true;
}
// Function to sort the array
// by left shifting digits of
// array elements
static int[] sortArr(int []arr)
{
// Stores previous array
// element
int prev = -1;
// Travere the array arr[]
for(int i = 0; i < arr.length; i++)
{
// Stores current element
int optEle = arr[i];
// Stores current element
// in String format
String strEle = String.valueOf(arr[i]);
// Left-shift digits of current
// element in all possible ways
for(int idx = 0; idx < strEle.length(); idx++)
{
// Left-shift digits of current
// element by idx
String strEle2 = strEle.substring(idx) +
strEle.substring(0, idx);
int temp = Integer.valueOf(strEle2);
// If temp greater than or equal to
// prev and temp less than optEle
if (temp >= prev && temp < optEle)
optEle = temp;
}
// Update arr[i]
arr[i] = optEle;
// Update prev
prev = arr[i];
}
// If arr is in increasing order
if (isIncreasing(arr))
return arr;
// Otherwise
else
{
return new int[]{ -1 };
}
}
// Driver Code
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]+ " ");
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program for the above approach
# Function to check if an array is
# sorted in increasing order or not
def isIncreasing(arr):
# Traverse the array
for i in range(len(arr)-1):
if arr[i] > arr[i + 1]:
return False
return True
# Function to sort the array
# by left shifting digits of
# array elements
def sortArr(arr):
# Stores previous array
# element
prev = -1
# Travere the array arr[]
for i in range(len(arr)):
# Stores current element
optEle = arr[i]
# Stores current element
# in string format
strEle = str(arr[i])
# Left-shift digits of current
# element in all possible ways
for idx in range(len(strEle)):
# Left-shift digits of current
# element by idx
temp = int(strEle[idx:] + strEle[:idx])
# If temp greater than or equal to
# prev and temp less than optEle
if temp >= prev and temp < optEle:
optEle = temp
# Update arr[i]
arr[i] = optEle
# Update prev
prev = arr[i]
# If arr is in increasing order
if isIncreasing(arr):
return arr
# Otherwise
else:
return "-1"
# Driver Code
if __name__ == '__main__':
arr = [511, 321, 323, 432, 433]
res = sortArr(arr)
for i in res:
print(i, end = " ")
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to check if an array is
// sorted in increasing order or not
static bool isIncreasing(int []arr)
{
// Traverse the array
for(int i = 0; i < arr.Length - 1; i++)
{
if (arr[i] > arr[i + 1])
return false;
}
return true;
}
// Function to sort the array
// by left shifting digits of
// array elements
static int[] sortArr(int []arr)
{
// Stores previous array
// element
int prev = -1;
// Travere the array []arr
for(int i = 0; i < arr.Length; i++)
{
// Stores current element
int optEle = arr[i];
// Stores current element
// in String format
String strEle = String.Join("",arr[i]);
// Left-shift digits of current
// element in all possible ways
for(int idx = 0; idx < strEle.Length; idx++)
{
// Left-shift digits of current
// element by idx
String strEle2 = strEle.Substring(idx) +
strEle.Substring(0, idx);
int temp = Int32.Parse(strEle2);
// If temp greater than or equal to
// prev and temp less than optEle
if (temp >= prev && temp < optEle)
optEle = temp;
}
// Update arr[i]
arr[i] = optEle;
// Update prev
prev = arr[i];
}
// If arr is in increasing order
if (isIncreasing(arr))
return arr;
// Otherwise
else
{
return new int[]{ -1 };
}
}
// Driver Code
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]+ " ");
}
}
// This code is contributed by shikhasingrajput.
输出:
115 132 233 243 334
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live