📌  相关文章
📜  生成一个长度为 N 的数组,其中非递减子数组的长度最大化并且第一个和最后一个数组元素之间的差异最小

📅  最后修改于: 2022-05-13 01:56:09.198000             🧑  作者: Mango

生成一个长度为 N 的数组,其中非递减子数组的长度最大化并且第一个和最后一个数组元素之间的差异最小

给定一个大小为N的数组arr[ ] ,任务是打印一个长度为N的数组,其所有非递减子数组的长度之和最大,并且第一个和最后一个元素之间的差最小。

例子:

方法:问题可以贪婪地解决。请按照以下步骤解决问题:

  • 以非递减顺序对数组 arr[ ] 进行排序
  • 找到两个具有最小差异的连续元素的索引,比如ii + 1
  • arr[0]arr[i]交换,将arr[N]arr[i + 1]交换。
  • arr[1 : i – 1]arr[i + 2 : N – 1]交换。
  • 打印数组arr[ ]

下面是上述方法的实现:

C++
// C++ program for above approach
 
#include 
using namespace std;
 
// Function to print target array
void printArr(int arr[], int n)
{
 
    // Sort the given array
    sort(arr, arr + n);
 
    // Seeking for index of elements with minimum diff.
    int minDifference = INT_MAX;
    int minIndex = -1;
 
    // Seeking for index
    for (int i = 1; i < n; i++) {
 
        if (minDifference
            > abs(arr[i] - arr[i - 1])) {
 
            minDifference = abs(arr[i] - arr[i - 1]);
            minIndex = i - 1;
        }
    }
 
    // To store target array
    int Arr[n];
 
    Arr[0] = arr[minIndex];
    Arr[n - 1] = arr[minIndex + 1];
    int pos = 1;
 
    // Copying element
    for (int i = minIndex + 2; i < n; i++) {
 
        Arr[pos++] = arr[i];
    }
 
    // Copying remaining element
    for (int i = 0; i < minIndex; i++) {
 
        Arr[pos++] = arr[i];
    }
 
    // Printing target array
    for (int i = 0; i < n; i++) {
 
        cout << Arr[i] << " ";
    }
}
 
// Driver Code
int main()
{
    // Given Input
    int N = 8;
    int arr[] = { 4, 6, 2, 6, 8, 2, 6, 4 };
 
    // Function Call
    printArr(arr, N);
    return 0;
}


Java
// Java program for above approach
import java.io.*;
import java.util.*;
 
class GFG{
     
// Function to print target array    
public static void printArr(int arr[], int n)
{
     
    // Sort the given array
    Arrays.sort(arr);
 
    // Seeking for index of elements
    // with minimum diff.
    int minDifference = 1000000007;
    int minIndex = -1;
 
    // Seeking for index
    for(int i = 1; i < n; i++)
    {
        if (minDifference >
            Math.abs(arr[i] - arr[i - 1]))
        {
            minDifference = Math.abs(arr[i] -
                                     arr[i - 1]);
            minIndex = i - 1;
        }
    }
 
    // To store target array
    int Arr[] = new int[n];
 
    Arr[0] = arr[minIndex];
    Arr[n - 1] = arr[minIndex + 1];
    int pos = 1;
 
    // Copying element
    for(int i = minIndex + 2; i < n; i++)
    {
        Arr[pos++] = arr[i];
    }
 
    // Copying remaining element
    for(int i = 0; i < minIndex; i++)
    {
        Arr[pos++] = arr[i];
    }
 
    // Printing target array
    for(int i = 0; i < n; i++)
    {
        System.out.print(Arr[i] + " ");
    }
}
 
// Driver code
public static void main(String[] args)
{
    int N = 8;
    int arr[] = { 4, 6, 2, 6, 8, 2, 6, 4 };
 
    // Function Call
    printArr(arr, N);
}
}
 
// This code is contributed by maddler


Python3
# Python3 program for above approach
import sys
 
# Function to print target array
def printArr(arr, n):
   
    # Sort the given array
    arr.sort()
 
    # Seeking for index of elements with minimum diff.
    minDifference = sys.maxsize
    minIndex = -1
 
    # Seeking for index
    for i in range(1,n,1):
        if (minDifference > abs(arr[i] - arr[i - 1])):
 
            minDifference = abs(arr[i] - arr[i - 1])
            minIndex = i - 1
 
    # To store target array
    Arr = [0 for i in range(n)]
 
    Arr[0] = arr[minIndex]
    Arr[n - 1] = arr[minIndex + 1]
    pos = 1
 
    # Copying element
    for i in range(minIndex + 2,n,1):
        Arr[pos] = arr[i]
        pos += 1
 
    # Copying remaining element
    for i in range(minIndex):
        Arr[pos] = arr[i]
        pos += 1
 
    # Printing target array
    for i in range(n):
        print(Arr[i],end = " ")
 
# Driver Code
if __name__ == '__main__':
    # Given Input
    N = 8
    arr = [4, 6, 2, 6, 8, 2, 6, 4]
 
    # Function Call
    printArr(arr, N)
     
    # This code is contributed by bgangwar59.


C#
// C# program for above approach
using System;
 
class GFG{
     
// Function to print target array    
public static void printArr(int []arr, int n)
{
     
    // Sort the given array
    Array.Sort(arr);
 
    // Seeking for index of elements
    // with minimum diff.
    int minDifference = 1000000007;
    int minIndex = -1;
 
    // Seeking for index
    for(int i = 1; i < n; i++)
    {
        if (minDifference >
            Math.Abs(arr[i] - arr[i - 1]))
        {
            minDifference = Math.Abs(arr[i] -
                                     arr[i - 1]);
            minIndex = i - 1;
        }
    }
 
    // To store target array
    int []Arr = new int[n];
 
    Arr[0] = arr[minIndex];
    Arr[n - 1] = arr[minIndex + 1];
    int pos = 1;
 
    // Copying element
    for(int i = minIndex + 2; i < n; i++)
    {
        Arr[pos++] = arr[i];
    }
 
    // Copying remaining element
    for(int i = 0; i < minIndex; i++)
    {
        Arr[pos++] = arr[i];
    }
 
    // Printing target array
    for(int i = 0; i < n; i++)
    {
        Console.Write(Arr[i] + " ");
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 8;
    int []arr = { 4, 6, 2, 6, 8, 2, 6, 4 };
 
    // Function Call
    printArr(arr, N);
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:
2 4 4 6 6 6 8 2

时间复杂度: O(N*logN)
辅助空间: O(N)