Smallest number that never becomes negative when processed against array elements
Last Updated :
19 Sep, 2023
Given an array of size n your goal is to find a number such that when the number is processed against each array element starting from the 0th index till the (n-1)-th index under the conditions given below, it never becomes negative.
- If the number is greater than an array element, then it is increased by the difference of the number and the array element.
- If the number is smaller than an array element, then it is decreased by the difference of the number and the array element.
Examples:
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
Simple Approach: A simple approach is to find the maximum element in the array and test against each number starting from 1 till the maximum element, that it crosses the whole array with 0 value or not.
Implementation:
C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll suitable_num(ll a[], int n)
{
ll max = *max_element(a, a + n);
for ( int x = 1; x < max; x++) {
int num = x;
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;
}
int main()
{
ll a[] = { 3, 4, 3, 2, 4 };
int n = sizeof (a)/( sizeof (a[0]));
cout << suitable_num(a, n);
return 0;
}
|
Java
import java.util.Arrays;
public class Largest_Number_NotNegate
{
static long suitable_num( long a[], int n)
{
long max = Arrays.stream(a).max().getAsLong();
for ( int x = 1 ; x < max; x++) {
int num = x;
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;
}
public static void main(String[] args) {
long a[] = { 3 , 4 , 3 , 2 , 4 };
int n = a.length;
System.out.println(suitable_num(a, n));
}
}
|
Python3
def suitable_num(a):
mx = max (a)
for x in range ( 1 , mx):
num = x
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
a = [ 3 , 4 , 3 , 2 , 4 ]
print (suitable_num(a))
|
C#
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class GFG
{
static long suitable_num( long []a, int n)
{
long max = a.Max();
for ( int x = 1; x < max; x++)
{
long num = x;
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;
}
public static void Main(String []args)
{
long []a = { 3, 4, 3, 2, 4 };
int n = a.Length;
Console.Write(suitable_num(a, n));
}
}
|
Javascript
<script>
function suitable_num(a,n)
{
let max = Math.max(...a)
for (let x = 1; x < max; x++) {
let num = x;
let 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;
}
let a=[3, 4, 3, 2, 4];
let n = a.length;
document.write(suitable_num(a, n));
</script>
|
Time Complexity: O (n^2)
Auxiliary Space: O (1)
Efficient Approach: Efficient approach to solving this problem would be to use the fact that when you reach the last array element, the value with which we started can be at least 0, which means suppose the last array element is a[n-1] then the value at a[n-2] must be greater than or equal to a[n-1]/2.
Implementation:
C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll suitable_num(ll a[], int n)
{
ll num = 0;
for ( int i = n - 1; i >= 0; i--)
num = round((a[i] + num) / 2.0);
return num;
}
int main()
{
ll a[] = { 3, 4, 3, 2, 4 };
int n = sizeof (a)/( sizeof (a[0]));
cout << suitable_num(a, n);
return 0;
}
|
Java
public class Largest_Number_NotNegate {
static long suitable_num( long a[], int n) {
long num = 0 ;
for ( int i = n - 1 ; i >= 0 ; i--)
num = Math.round((a[i] + num) / 2.0 );
return num;
}
public static void main(String[] args) {
long a[] = { 3 , 4 , 3 , 2 , 4 };
int n = a.length;
System.out.println(suitable_num(a, n));
}
}
|
Python3
def suitable_num(a):
num = 0
i = len (a) - 1
while i > = 0 :
num = round ((a[i] + num) / 2.0 )
i - = 1
return int (num)
a = [ 3 , 4 , 3 , 2 , 4 ]
print (suitable_num(a))
|
C#
using System;
class GFG
{
static long suitable_num( long [] a, int n)
{
long num = 0;
for ( int i = n - 1; i >= 0; i--)
num = ( long )Math.Round((a[i] + num) / 2.0,
MidpointRounding.AwayFromZero);
return num;
}
public static void Main()
{
long [] a = { 3, 4, 3, 2, 4 };
int n = a.Length;
Console.Write(suitable_num(a, n));
}
}
|
PHP
<?php
function suitable_num(& $a , $n )
{
$num = 0;
for ( $i = $n - 1; $i >= 0; $i --)
$num = round (( $a [ $i ] + $num ) / 2.0);
return $num ;
}
$a = array ( 3, 4, 3, 2, 4 );
$n = sizeof( $a );
echo suitable_num( $a , $n );
?>
|
Javascript
<script>
function suitable_num(a,n)
{
let num = 0;
for (let i = n - 1; i >= 0; i--)
num = Math.round((a[i] + num) / 2.0);
return num;
}
let a = [ 3, 4, 3, 2, 4 ];
let n = a.length;
document.write(suitable_num(a, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...