📌  相关文章
📜  通过以最少的操作次数将 K 减少到 0 来打印字典序最小的数组

📅  最后修改于: 2021-10-25 09:17:19             🧑  作者: Mango

给定一个数组arr[]和一个整数K,任务是通过执行以下操作将K的值减少到0 。一种操作定义为选择2 个索引i, j并从arr[i] 中减去arr[i]K的最小值(即 X = min(arr[i], K) (即 arr[i] = arr [i] – X)并将最小值添加到arr[j] (arr[j] = arr[j] + X)并打印字典序最小的数组。请注意数组arr[]的元素不能为负.

例子:

方法:这个问题可以通过迭代数组arr[]来解决。请按照以下步骤解决问题:

  • 使用变量i在范围[0, N-1] 上迭代并执行以下步骤:
    • 如果arr[i]小于K,则执行以下步骤。
    • 从变量K 中减去arr[i] arr[i]的值添加到arr[n-1]并将arr[i]的值设置为0。
    • 否则,从ARR的值减去K [I],加入K值给Arr [N-1],并设置的K0的值,并中断环路。
  • 对数组arr[] 进行排序。
  • 执行上述步骤后,打印数组arr[]的元素。

下面是上述方法的实现:

C++
// C++ program for the above approach.
#include 
using namespace std;
 
// Function to find the resultant array.
void solve(int n, int k, int arr[])
{
 
    for (int i = 0; i < n - 1; i++) {
 
        // checking if aith value less than K
 
        if (arr[i] < k)
 
        {
            // substracting ai value from K
            k = k - arr[i];
 
            // Adding ai value to an-1
            arr[n - 1]
                = arr[n - 1]
                  + arr[i];
 
            arr[i] = 0;
        }
 
        // if arr[i] value is greater than  K
        else {
 
            arr[i] = arr[i] - k;
            arr[n - 1] = arr[n - 1] + k;
            k = 0;
        }
    }
 
    // sorting the given array
    // to know about this function
    // check gfg stl sorting article
    sort(arr, arr + n);
 
    // Displaying the final array
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Driver code
int main()
{
 
    int N = 6;
    int K = 2;
    int arr[N] = { 3, 1, 4, 6, 2, 5 };
 
    solve(N, K, arr);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.Arrays;
class GFG
{
   
  // Function to find the resultant array.
static void solve(int n, int k, int arr[])
{
 
    for (int i = 0; i < n - 1; i++) {
 
        // checking if aith value less than K
        if (arr[i] < k)
 
        {
           
            // substracting ai value from K
            k = k - arr[i];
 
            // Adding ai value to an-1
            arr[n - 1]
                = arr[n - 1]
                  + arr[i];
 
            arr[i] = 0;
        }
 
        // if arr[i] value is greater than  K
        else {
 
            arr[i] = arr[i] - k;
            arr[n - 1] = arr[n - 1] + k;
            k = 0;
        }
    }
 
    // sorting the given array
    // to know about this function
    // check gfg stl sorting article
    Arrays.sort(arr);
 
    // Displaying the final array
    for (int i = 0; i < n; i++)
        System.out.print( arr[i] + " ");
}
 
// Driver code
    public static void main (String[] args) {
         int N = 6;
    int K = 2;
    int arr[] = { 3, 1, 4, 6, 2, 5 };
 
    solve(N, K, arr);
    }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python program for the above approach.
# Function to find the resultant array.
def solve( n,  k,  arr):
     
    for i in range(n-1):
        # checking if aith value less than K
         
        if (arr[i] < k):
             
            # substracting ai value from K
            k = k - arr[i]
             
            # Adding ai value to an-1
            arr[n - 1] = arr[n - 1] + arr[i]
             
            arr[i] = 0
             
        # if arr[i] value is greater than  K
        else:
            arr[i] = arr[i] - k
            arr[n - 1] = arr[n - 1] + k
            k = 0
             
    # sorting the given array
    # to know about this function
    # check gfg stl sorting article
    arr.sort()
     
    # Displaying the final array
    for i in range(n):
        print(arr[i], end = " ")
 
# Driver code
N = 6
K = 2
arr = [ 3, 1, 4, 6, 2, 5 ]
 
solve(N, K, arr)
 
# This code is contributed by shivanisinghss2110


C#
// C# program for the above approach
using System;
 
public class GFG
{
 
  // Function to find the resultant array.
  static void solve(int n, int k, int []arr)
  {
 
    for (int i = 0; i < n - 1; i++) {
 
      // checking if aith value less than K
      if (arr[i] < k)
 
      {
 
        // substracting ai value from K
        k = k - arr[i];
 
        // Adding ai value to an-1
        arr[n - 1]
          = arr[n - 1]
          + arr[i];
 
        arr[i] = 0;
      }
 
      // if arr[i] value is greater than  K
      else {
 
        arr[i] = arr[i] - k;
        arr[n - 1] = arr[n - 1] + k;
        k = 0;
      }
    }
 
    // sorting the given array
    // to know about this function
    // check gfg stl sorting article
    Array.Sort(arr);
 
    // Displaying the readonly array
    for (int i = 0; i < n; i++)
      Console.Write( arr[i] + " ");
  }
 
  // Driver code
  public static void Main(String[] args)
  {
    int N = 6;
    int K = 2;
    int []arr = { 3, 1, 4, 6, 2, 5 };
 
    solve(N, K, arr);
  }
}
 
// This code is contributed by shikhasingrajput


Javascript


输出
1 1 2 4 6 7 

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

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