原文:
给定一个 arr[] 和一个整数 k,的任务是通过执行以下操作将 k 的值减少到 0 。一个操作定义为选择 2 索引 i,j 并从 arr中减去 arr[i] 和 k(即 x = min(arr[i],k) 的最小值,并将最小值加到arr【t21 请注意arr【】的元素不能为负。
示例:
输入: n = 4,k = 2,arr[] = {4,3,2,1} 输出: 2 2 3 3 解释: 操作 1:选择指数 0 和 3、然后减去 arr 和 k 的最小值现在,修改后的数组是{2,3,2,3} 现在,对修改后的数组进行排序并打印出来。
输入: n = 3,k = 15,arr[] = {1,2,3} 输出: 0 0 6 解释: 操作 1:选择指数 0 和 2、然后减去 arr 和k(= 11)现在修改后的数组是{0,2,4}。 操作 2:选择指数 1 和 2、然后从 arr[1] 中减去 arr 和 k(=15) 的最小值,即 arr 中的 arr。现在修改后的数组是{0,0,6}。 现在,对修改后的数组进行排序并打印。
方法:这个问题可以通过迭代数组arr【】来解决。按照以下步骤解决问题:
- 【0,n-1】,并执行以下步骤:
- 如果 arr[i] 小于 k,则采取以下步骤。
- 从变量 k 中减去arr【i】,将arr【i】的值加到arr【n-1】上,将arr【i】的值设置为 0。****
- 否则,从 arr[i]的值中减去 k ,将 k 的值加到 arr[n-1] 并将 k 的值设置为 0 ,回路。
- t4【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
python 3
# 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
java 描述语言
output
1 1 2 4 6 7
时间复杂度: o(nlog(n))* 辅助空间: o(1)
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处