📌  相关文章
📜  通过数组元素的左移数字对数组进行排序

📅  最后修改于: 2021-04-26 07:01:55             🧑  作者: Mango

给定由N个正整数组成的数组arr [] ,任务是将数组元素的数字左移,以使数组修改为排序形式。如果存在多个解决方案,则打印其中任何一种。否则,打印-1

例子:

方法:请按照以下步骤解决问题:

  1. 想法是将每个数组元素的数字左移,以使当前元素是先前数组元素中最接近的较大元素。
  2. 遍历数组并以所有可能的方式移动数组元素的位数,并选择一个最小的位数,但大于前一个数组元素的位数。如果找不到此类元素,则打印-1
  3. 否则,请在执行上述操作后打印数组元素

下面是上述方法的实现:

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)