给定一个数组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[]的元素不能为负.
例子:
Input: N = 4, K = 2, arr[] = {4, 3, 2, 1}
Output: 2 2 3 3
Explanation:
Operation 1: Select indices 0 and 3, then subtract min of arr[0](=4) and K(=2) from arr[0] and add the minimum value i.e K(=2) in arr[3](=1). Now, the modified array is {2, 3, 2, 3}
Now, sort the modified array and print it.
Input: N = 3, K = 15, arr[] = {1, 2, 3}
Output: 0 0 6
Explanation:
Operation 1: Select indices 0 and 2, then subtract min of arr[0](=1) and K(=15) from arr[0] and add the minimum value i.e arr[0](=1) in arr[2](=3). Now the modified array is {0, 2, 4}.
Operation 2: Select indices 1 and 2, then subtract min of arr[1](=2) and K(=15) from arr[1] and add the minimum value i.e arr[1](=2) in arr[2](=4). Now the modified array is {0, 0, 6}.
Now, sort the modified array and print it.
方法:这个问题可以通过迭代数组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],并设置的K为0的值,并中断环路。
- 对数组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 现场工作专业课程和学生竞争性编程现场课程。