给定整数k和数组arr [] ,任务是将以下操作恰好重复k次:
在数组中找到最小的非零元素,将其打印,然后从数组的所有非零元素中减去该数字。如果数组的所有元素均小于0 ,则仅打印0 。
例子:
Input: arr[] = {3, 6, 4, 2}, k = 5
Output: 2 1 1 2 0
k = 1; Pick 2 and update arr[] = {1, 4, 2, 0}
k = 2; Pick 1, arr[] = {0, 3, 1, 0}
k = 3; Pick 1, arr[] = {0, 2, 0, 0}
k = 4; Pick 2, arr[] = {0, 0, 0, 0}
k = 5; Nothing to pick so print 0
Input: arr[] = {1, 2}, k = 3
Output: 1 1 0
方法:对数组进行排序,并采用一个名为sum的额外变量,该变量将存储变为0的先前元素。
取arr [] = {3,6,4,2}并在对数组进行排序后初始sum = 0 ,它将变为arr [] = {2,3,4,6} 。
现在sum = 0 ,我们打印第一个非零元素,即2,并分配sum = 2 。
在下一次迭代中,选择第二个元素(即3)并打印3 –和(即1作为2)已从所有其他非零元素中减去。完全重复这些步骤k次。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
#define ll long long int
// Function to perform the given operation on arr[]
void operations(int arr[], int n, int k)
{
sort(arr, arr + n);
ll i = 0, sum = 0;
while (k--) {
// Skip elements which are 0
while (i < n && arr[i] - sum == 0)
i++;
// Pick smallest non-zero element
if (i < n && arr[i] - sum > 0) {
cout << arr[i] - sum << " ";
sum = arr[i];
}
// If all the elements of arr[] are 0
else
cout << 0 << endl;
}
}
// Driver code
int main()
{
int k = 5;
int arr[] = { 3, 6, 4, 2 };
int n = sizeof(arr) / sizeof(arr[0]);
operations(arr, n, k);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Function to perform the given operation on arr[]
static void operations(int arr[], int n, int k)
{
Arrays.sort(arr);
int i = 0, sum = 0;
while (k-- > 0)
{
// Skip elements which are 0
while (i < n && arr[i] - sum == 0)
i++;
// Pick smallest non-zero element
if (i < n && arr[i] - sum > 0)
{
System.out.print(arr[i] - sum + " ");
sum = arr[i];
}
// If all the elements of arr[] are 0
else
System.out.println("0");
}
}
// Driver code
public static void main(String args[])
{
int k = 5;
int arr[] = { 3, 6, 4, 2 };
int n = arr.length;
operations(arr, n, k);
}
}
// This code is contributed by Princi Singh
Python3
# Python implementation of the approach
# Function to perform the given operation on arr[]
def operations(arr, n, k):
arr.sort();
i = 0; sum = 0;
while (k > 0):
# Skip elements which are 0
while (i < n and arr[i] - sum == 0):
i+=1;
# Pick smallest non-zero element
if (i < n and arr[i] - sum > 0):
print(arr[i] - sum, end= " ");
sum = arr[i];
# If all the elements of arr[] are 0
else:
print(0);
k-=1;
# Driver code
k = 5;
arr = [ 3, 6, 4, 2 ];
n = len(arr);
operations(arr, n, k);
# This code is contributed by PrinciRaj1992
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to perform the given operation on arr[]
static void operations(int []arr, int n, int k)
{
Array.Sort(arr);
int i = 0, sum = 0;
while (k-- > 0)
{
// Skip elements which are 0
while (i < n && arr[i] - sum == 0)
i++;
// Pick smallest non-zero element
if (i < n && arr[i] - sum > 0)
{
Console.Write(arr[i] - sum + " ");
sum = arr[i];
}
// If all the elements of arr[] are 0
else
Console.WriteLine("0");
}
}
// Driver code
public static void Main(String []args)
{
int k = 5;
int []arr = { 3, 6, 4, 2 };
int n = arr.Length;
operations(arr, n, k);
}
}
// This code has been contributed by 29AjayKumar
Javascript
输出:
2 1 1 2 0