📌  相关文章
📜  根据给定条件最大化最后一个Array元素

📅  最后修改于: 2021-05-17 23:05:33             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,请重新排列该数组,使其满足以下条件:

  1. arr [0]必须为1
  2. 相邻数组元素之间的差不应超过1 ,即,对于所有1≤i arr [i] – arr [i-1]≤1

允许的操作如下:

  1. 以任何方式重新排列元素。
  2. 减少到任何数目≥1的任何元件。

任务是找到可以放置在数组的最后一个索引处的最大可能值。

例子:

方法:
要解决给定的问题,请对给定的数组进行排序,并根据从左到右的给定条件对其进行平衡。请按照以下步骤解决问题:

  • 以升序对数组进行排序。
  • 如果第一个元素不是1 ,则将其设为1
  • 遍历索引[1,N – 1)上的数组并检查每个相邻元素的差值是否≤1
  • 如果不是,则减小该值,直到差值≤1为止。
  • 返回数组的最后一个元素。

下面是上述问题的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the maximum possible value
// that can be placed at the last index
int maximizeFinalElement(int arr[], int n)
{
    // Sort array in ascending order
    sort(arr, arr + n);
 
    // If the first element
    // is not equal to 1
    if (arr[0] != 1)
        arr[0] = 1;
 
    // Traverse the array to make
    // difference between adjacent
    // elements <=1
    for (int i = 1; i < n; i++) {
        if (arr[i] - arr[i - 1] > 1) {
            arr[i] = arr[i - 1] + 1;
        }
    }
    return arr[n - 1];
}
 
// Driver Code
int main()
{
    int n = 4;
    int arr[] = { 3, 1, 3, 4 };
 
    int max = maximizeFinalElement(arr, n);
    cout << max;
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to find the maximum possible value
// that can be placed at the last index
public static int maximizeFinalElement(int arr[],
                                       int n)
{
     
    // Sort the array elements
    // in ascending order
    Arrays.sort(arr);
 
    // If the first element is
    // is not equal to 1
    if (arr[0] != 1)
        arr[0] = 1;
 
    // Traverse the array to make
    // difference between adjacent
    // elements <=1
    for(int i = 1; i < n; i++)
    {
        if (arr[i] - arr[i - 1] > 1)
        {
            arr[i] = arr[i - 1] + 1;
        }
    }
    return arr[n - 1];
}
 
// Driver Code
public static void main (String[] args)
{
    int n = 4;
    int arr[] = { 3, 1, 3, 4 };
   
    int max = maximizeFinalElement(arr, n);
    System.out.print(max);
}
}


Python3
# Python3 program to implement
# the above approach
 
# Function to find the maximum possible value
# that can be placed at the last index
def maximizeFinalElement(arr, n):
     
    # Sort the array elements
    # in ascending order
    arr.sort();
 
    # If the first element is
    # is not equal to 1
    if (arr[0] != 1):
        arr[0] = 1;
 
    # Traverse the array to make
    # difference between adjacent
    # elements <=1
    for i in range(1, n):
        if (arr[i] - arr[i - 1] > 1):
            arr[i] = arr[i - 1] + 1;
 
    return arr[n - 1];
 
# Driver Code
if __name__ == '__main__':
     
    n = 4;
    arr = [3, 1, 3, 4];
     
    max = maximizeFinalElement(arr, n);
    print(max);
     
# This code is contributed by Princi Singh


C#
// C# Program to implement
// the above approach
using System;
class GFG{
 
// Function to find the maximum possible value
// that can be placed at the last index
public static int maximizeFinalElement(int []arr,
                                       int n)
{
    // Sort the array elements
    // in ascending order
    Array.Sort(arr);
 
    // If the first element is
    // is not equal to 1
    if (arr[0] != 1)
        arr[0] = 1;
 
    // Traverse the array to make
    // difference between adjacent
    // elements <=1
    for (int i = 1; i < n; i++)
    {
        if (arr[i] - arr[i - 1] > 1)
        {
            arr[i] = arr[i - 1] + 1;
        }
    }
 
    return arr[n - 1];
}
 
// Driver Code
public static void Main(String[] args)
{
    int n = 4;
    int []arr = { 3, 1, 3, 4 };
 
    int max = maximizeFinalElement(arr, n);
    Console.WriteLine(max);
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
4

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