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

📅  最后修改于: 2021-10-26 05:25:18             🧑  作者: Mango

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

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

允许的操作如下:

  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)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程