通过在每个元素上使用 ceil 或 floor 使数组总和为 0
给定一个由N个浮点整数组成的数组arr[] ,任务是通过对每个数组元素执行 ceil() 或 floor() 来修改数组,使数组元素的总和接近0 。
例子:
Input: arr[] = {6.455, -1.24, -3.87, 2.434, -4.647}
Output: {6, -2, -3, 3, -4}
Explanation:
Perform the floor operation on array elements at indices {0, 1} and ceil operation at indices {2, 3, 4} modifies the array to {6, -2, -3, 3, -4} whose sum of elements is 0, which is closest to 0.
Input: arr[] = {-12.42, 9.264, 24.24, -13.04, 4.0, -9.66, -2.99}
Output: {-13, 9, 24, -13, 4, -9, -2}
方法:给定的问题可以通过找到所有数组元素的 ceil() 值的总和来解决,如果数组的总和为正,则找到该元素数量的 ceil,使得 ceil 变得最接近于值 0。以下步骤可解决给定问题:
- 初始化一个变量,比如sum为0 ,它存储数组元素的总和。
- 初始化一个数组,比如A[] ,它存储更新的数组元素。
- 遍历数组arr[]并找到数组元素的ceil()和并将A[i]的值更新为值ceil(arr[i]) 。
- 如果sum的值为正,则找到某个数组元素的下限以将和减少到最接近0 ,并且此类数组元素的计数由sum和N的最小值给出。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to modify the array element
// such that the sum is close to 0
void setSumtoZero(double arr[], int N)
{
// Stores the modified elements
int A[N];
// Stores the sum of array element
int sum = 0;
// Stores minimum size of the array
int m = INT_MIN;
// Traverse the array and find the
// sum
for (int i = 0; i < N; i++) {
sum += ceil(arr[i]);
A[i] = ceil(arr[i]);
}
// If sum is positive
if (sum > 0) {
// Find the minimum number of
// elements that must be changed
m = min(sum, N);
// Iterate until M elements are
// modified or the array end
for (int i = 0; i < N && m > 0; i++) {
// Update the current array
// elements to its floor
A[i] = floor(arr[i]);
if (A[i] != floor(arr[i]))
m--;
}
}
// Print the resultant array
for (int i = 0; i < N; i++) {
cout << A[i] << " ";
}
}
// Driver Code
int main()
{
double arr[] = { -2, -2, 4.5 };
int N = sizeof(arr) / sizeof(arr[0]);
setSumtoZero(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to modify the array element
// such that the sum is close to 0
static void setSumtoZero(double arr[], int N)
{
// Stores the modified elements
int []A = new int[N];
// Stores the sum of array element
int sum = 0;
// Stores minimum size of the array
int m = Integer.MIN_VALUE;
// Traverse the array and find the
// sum
for (int i = 0; i < N; i++) {
sum += Math.ceil(arr[i]);
A[i] = (int) Math.ceil(arr[i]);
}
// If sum is positive
if (sum > 0) {
// Find the minimum number of
// elements that must be changed
m = Math.min(sum, N);
// Iterate until M elements are
// modified or the array end
for (int i = 0; i < N && m > 0; i++) {
// Update the current array
// elements to its floor
A[i] = (int) Math.floor(arr[i]);
if (A[i] != Math.floor(arr[i]))
m--;
}
}
// Print the resultant array
for (int i = 0; i < N; i++) {
System.out.print(A[i]+ " ");
}
}
// Driver Code
public static void main(String[] args)
{
double arr[] = { -2, -2, 4.5 };
int N = arr.length;
setSumtoZero(arr, N);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python 3 program for the above approach
import sys
from math import ceil,floor
# Function to modify the array element
# such that the sum is close to 0
def setSumtoZero(arr, N):
# Stores the modified elements
A = [0 for i in range(N)]
# Stores the sum of array element
sum = 0
# Stores minimum size of the array
m = -sys.maxsize-1
# Traverse the array and find the
# sum
for i in range(N):
sum += ceil(arr[i])
A[i] = ceil(arr[i])
# If sum is positive
if (sum > 0):
# Find the minimum number of
# elements that must be changed
m = min(sum, N)
# Iterate until M elements are
# modified or the array end
i = 0
while(i < N and m > 0):
# Update the current array
# elements to its floor
A[i] = floor(arr[i])
if (A[i] != floor(arr[i])):
m -= 1
i += 1
# Print the resultant array
for i in range(N):
print(A[i],end = " ")
# Driver Code
if __name__ == '__main__':
arr = [-2, -2, 4.5]
N = len(arr)
setSumtoZero(arr, N)
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
public class GFG{
// Function to modify the array element
// such that the sum is close to 0
static void setSumtoZero(double []arr, int N)
{
// Stores the modified elements
int []A = new int[N];
// Stores the sum of array element
int sum = 0;
// Stores minimum size of the array
int m = int.MinValue;
// Traverse the array and find the
// sum
for (int i = 0; i < N; i++) {
sum += (int)Math.Ceiling(arr[i]);
A[i] = (int) Math.Ceiling(arr[i]);
}
// If sum is positive
if (sum > 0) {
// Find the minimum number of
// elements that must be changed
m = Math.Min(sum, N);
// Iterate until M elements are
// modified or the array end
for (int i = 0; i < N && m > 0; i++) {
// Update the current array
// elements to its floor
A[i] = (int) Math.Floor(arr[i]);
if (A[i] != Math.Floor(arr[i]))
m--;
}
}
// Print the resultant array
for (int i = 0; i < N; i++) {
Console.Write(A[i]+ " ");
}
}
// Driver Code
public static void Main(String[] args)
{
double []arr = { -2, -2, 4.5 };
int N = arr.Length;
setSumtoZero(arr, N);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
-2 -2 4
时间复杂度: O(N)
辅助空间: O(N)