📌  相关文章
📜  对数组元素进行处理时永远不会变为负数的最小数

📅  最后修改于: 2022-05-13 01:57:53.053000             🧑  作者: Mango

对数组元素进行处理时永远不会变为负数的最小数

给定一个大小为 n 的数组,您的目标是找到一个数字,使得当在下面给出的条件下针对从第 0 个索引开始到第 (n-1) 个索引的每个数组元素处理该数字时,它永远不会变为负数。

  1. 如果数字大于数组元素,则增加数字和数组元素的差。
  2. 如果数字小于数组元素,则减少数字和数组元素的差。

例子:

Input : arr[] = {3 4 3 2 4}
Output : 4
Explanation : 
If we process 4 from left to right
in given array, we get following :
When processed with 3, it becomes 5.
When processed with 5, it becomes 6
When processed with 3, it becomes 9
When processed with 2, it becomes 16
When processed with 4, it becomes 28
We always get a positive number. For 
all values lower than 4, it would
become negative for some value of the 
array.

Input: arr[] = {4 4}
Output : 3
Explanation : 
When processed with 4, it becomes 2
When processed with next 4, it becomes 1

简单方法:一种简单的方法是找到数组中的最大元素,并针对从 1 到最大元素的每个数字进行测试,它是否与 0 值交叉整个数组。

C++
// C++ program to find the smallest number
// that never becomes positive when processed
// with given array elements.
#include 
#define ll long long int
using namespace std;
 
ll suitable_num(ll a[], int n)
{
    // Finding max element in the array
    ll max = *max_element(a, a + n);
 
    for (int x = 1; x < max; x++) {
 
        // Creating copy of i since it's 
        // getting modified at later steps.
        int num = x;
 
        // Checking that num doesn't becomes
        // negative.
        int j;
        for (j = 0; j < n; j++)
        { 
            if (num > a[j])
                num += (num - a[j]);
            else if (a[j] > num)
                num -= (a[j] - num);
            if (num < 0)
                break;
        }
 
        if (j == n)
            return x;       
    }
 
    return max;
}
 
// Driver code
int main()
{
    ll a[] = { 3, 4, 3, 2, 4 };
    int n = sizeof(a)/(sizeof(a[0]));
    cout << suitable_num(a, n);
    return 0;
}


Java
// A Java program to find the smallest number
// that never becomes positive when processed
// with given array elements.
import java.util.Arrays;
public class Largest_Number_NotNegate
{
    static long suitable_num(long a[], int n)
    {
        // Finding max element in the array
        long max = Arrays.stream(a).max().getAsLong();
 
        for (int x = 1; x < max; x++) {
 
            // Creating copy of i since it's
            // getting modified at later steps.
            int num = x;
 
            // Checking that num doesn't becomes
            // negative.
            int j;
            for (j = 0; j < n; j++) {
                if (num > a[j])
                    num += (num - a[j]);
                else if (a[j] > num)
                    num -= (a[j] - num);
                if (num < 0)
                    break;
            }
 
            if (j == n)
                return x;
        }
 
        return max;
    }
     
    // Driver program to test above method
    public static void main(String[] args) {
 
        long a[] = { 3, 4, 3, 2, 4 };
        int n = a.length;
        System.out.println(suitable_num(a, n));
    }
}
// This code is contributed by Sumit Ghosh


Python3
# Python program to find the smallest number
# that never becomes positive when processed
# with given array elements.
def suitable_num(a):
    mx = max(a)
     
    for x in range(1, mx):
         
        # Creating copy of i since it's 
        # getting modified at later steps.
        num = x
         
        # Checking that num doesn't becomes
        # negative.
        j = 0;
        while j < len(a):
            if num > a[j]:
                num += num - a[j]
            else if a[j] > num:
                num -= (a[j] - num)
            if num < 0:
                break
            j += 1
        if j == len(a):
            return x
    return mx
 
# Driver code
a =[ 3, 4, 3, 2, 4 ]
print(suitable_num(a))
 
# This code is contributed by Sachin Bisht


C#
// A C# program to find the smallest number
// that never becomes positive when processed
// with given array elements.
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
 
class GFG
{
    static long suitable_num(long []a, int n)
    {
        // Finding max element in the array
        long max = a.Max();
 
        for (int x = 1; x < max; x++)
        {
 
            // Creating copy of i since it's
            // getting modified at later steps.
            long num = x;
 
            // Checking that num doesn't becomes
            // negative.
            int j;
            for (j = 0; j < n; j++)
            {
                if (num > a[j])
                    num += (num - a[j]);
                else if (a[j] > num)
                    num -= (a[j] - num);
                if (num < 0)
                    break;
            }
 
            if (j == n)
                return x;
        }
        return max;
    }
     
    // Driver Code
    public static void Main(String []args)
    {
        long []a = { 3, 4, 3, 2, 4 };
        int n = a.Length;
        Console.Write(suitable_num(a, n));
    }
}
 
// This code is contributed by Arnab Kundu


Javascript


C++
// Efficient C++ program to find the smallest
// number that never becomes positive when
// processed with given array elements.
#include 
#define ll long long int
using namespace std;
 
ll suitable_num(ll a[], int n)
{
    ll num = 0;
 
    // Calculating the suitable number at each step.
    for (int i = n - 1; i >= 0; i--)
        num = round((a[i] + num) / 2.0);
 
    return num;
}
 
// Driver code
int main()
{
    ll a[] = { 3, 4, 3, 2, 4 };
    int n = sizeof(a)/(sizeof(a[0]));
    cout << suitable_num(a, n);
    return 0;
}


Java
// Efficient Java program to find the smallest
// number that never becomes positive when
// processed with given array elements.
public class Largest_Number_NotNegate {
    static long suitable_num(long a[], int n) {
        long num = 0;
 
        // Calculating the suitable number at each step.
        for (int i = n - 1; i >= 0; i--)
            num = Math.round((a[i] + num) / 2.0);
 
        return num;
    }
 
    // Driver Program to test above function
    public static void main(String[] args) {
 
        long a[] = { 3, 4, 3, 2, 4 };
        int n = a.length;
        System.out.println(suitable_num(a, n));
 
    }
}
// This code is contributed by Sumit Ghosh


Python3
# Efficient Python program to find the smallest
# number that never becomes positive when
# processed with given array elements.
def suitable_num(a):
    num = 0
     
    # Calculating the suitable number at each step.
    i = len(a) - 1
    while i >= 0:
        num = round((a[i] + num) / 2.0)
     
        i -= 1
    return int(num)
 
# Driver code
a = [ 3, 4, 3, 2, 4 ]
print (suitable_num(a))
 
# This code is contributed by Sachin Bisht


C#
// Efficient C# program to find the smallest
// number that never becomes positive when
// processed with given array elements.
using System;
 
class GFG
{
static long suitable_num(long[] a, int n)
{
    long num = 0;
 
    // Calculating the suitable number
    // at each step.
    for (int i = n - 1; i >= 0; i--)
        num = (long)Math.Round((a[i] + num) / 2.0,
                                MidpointRounding.AwayFromZero);
 
    return num;
}
 
// Driver Code
public static void Main()
{
    long[] a = { 3, 4, 3, 2, 4 };
    int n = a.Length;
    Console.Write(suitable_num(a, n));
}
}
 
// This code is contributed by ita_c


PHP
= 0; $i--)
        $num = round(($a[$i] + $num) / 2.0);
 
    return $num;
}
 
// Driver code
$a = array( 3, 4, 3, 2, 4 );
$n = sizeof($a);
echo suitable_num($a, $n);
 
// This code is contributed by ita_c
?>


Javascript


输出:

4

时间复杂度:O (n^2)
辅助空间:O (1)

有效的方法:解决这个问题的有效方法是利用这样一个事实:当你到达最后一个数组元素时,我们开始的值可以至少为 0,这意味着假设最后一个数组元素是 a[n-1]那么 a[n-2] 处的值必须大于或等于 a[n-1]/2。

C++

// Efficient C++ program to find the smallest
// number that never becomes positive when
// processed with given array elements.
#include 
#define ll long long int
using namespace std;
 
ll suitable_num(ll a[], int n)
{
    ll num = 0;
 
    // Calculating the suitable number at each step.
    for (int i = n - 1; i >= 0; i--)
        num = round((a[i] + num) / 2.0);
 
    return num;
}
 
// Driver code
int main()
{
    ll a[] = { 3, 4, 3, 2, 4 };
    int n = sizeof(a)/(sizeof(a[0]));
    cout << suitable_num(a, n);
    return 0;
}

Java

// Efficient Java program to find the smallest
// number that never becomes positive when
// processed with given array elements.
public class Largest_Number_NotNegate {
    static long suitable_num(long a[], int n) {
        long num = 0;
 
        // Calculating the suitable number at each step.
        for (int i = n - 1; i >= 0; i--)
            num = Math.round((a[i] + num) / 2.0);
 
        return num;
    }
 
    // Driver Program to test above function
    public static void main(String[] args) {
 
        long a[] = { 3, 4, 3, 2, 4 };
        int n = a.length;
        System.out.println(suitable_num(a, n));
 
    }
}
// This code is contributed by Sumit Ghosh

Python3

# Efficient Python program to find the smallest
# number that never becomes positive when
# processed with given array elements.
def suitable_num(a):
    num = 0
     
    # Calculating the suitable number at each step.
    i = len(a) - 1
    while i >= 0:
        num = round((a[i] + num) / 2.0)
     
        i -= 1
    return int(num)
 
# Driver code
a = [ 3, 4, 3, 2, 4 ]
print (suitable_num(a))
 
# This code is contributed by Sachin Bisht

C#

// Efficient C# program to find the smallest
// number that never becomes positive when
// processed with given array elements.
using System;
 
class GFG
{
static long suitable_num(long[] a, int n)
{
    long num = 0;
 
    // Calculating the suitable number
    // at each step.
    for (int i = n - 1; i >= 0; i--)
        num = (long)Math.Round((a[i] + num) / 2.0,
                                MidpointRounding.AwayFromZero);
 
    return num;
}
 
// Driver Code
public static void Main()
{
    long[] a = { 3, 4, 3, 2, 4 };
    int n = a.Length;
    Console.Write(suitable_num(a, n));
}
}
 
// This code is contributed by ita_c

PHP

= 0; $i--)
        $num = round(($a[$i] + $num) / 2.0);
 
    return $num;
}
 
// Driver code
$a = array( 3, 4, 3, 2, 4 );
$n = sizeof($a);
echo suitable_num($a, $n);
 
// This code is contributed by ita_c
?>

Javascript


输出:

4

时间复杂度: O (n)
辅助空间: O (1)