简体   繁体   中英

The below Code problem solution works for me in local IDE but somehow fails in Hackerrank IDE,does my code have any problem

Minimum Start Value

Start with a given array of integer and an arbitrary initial value x.Calculate the running sum of x plus each array element,left to right. The running sum must never get below 1.Determine the minimum value of x.

Example arr=[4,-2,3,1,-5] The first element is the size so the array you need to work on would be arr=[-2,3,1,-5] with size n=4

if x= 4, the following result would be obtained.

sum     arr[i]
-----   ------ 
4        -2
2         3
5         1
6        -5
1

So the minimum value is 4.

Similarly, for an array, arr=[10, -5, 4, -2, 3, 1, -1, -6, -1, 0, -5], the size would be 10 so the actual array is arr=[-5, 4, -2, 3, 1, -1, -6, -1, 0, -5].

if x=6, following result would be obtained.

 sum     arr[i]
        -----   ------
        6       -5
        1        4
        5       -2
        3        3
        6        1
        7       -1
        0       -6
                -1
                 0
                -5

So, during running sum, we got a 0 sum which is less than 1, so this x=6 is not the minimum value.

if x=11, following result would be obtained.

   sum     arr[i]
    -----   ------
    11       -5
    6         4
    10       -2
    8         3
    11        1
    12       -1
    11       -6
    5        -1
    4         0
    4        -5
   -1  

This is incorrect and we got a number less than 1,so its not minimum value.

if x=13, the following result would be obtained.

    sum     arr[i]
    -----   ------
    13       -5
    8         4
    12       -2
    10        3
    13        1
    14       -1
    13       -6
    7        -1
    6         0
    6        -5
    1

So, the minimum value would be 13

The below is the code I wrote for the above problem.The test has 5 test cases, all of them were successful in my local IDE, but all failed in the hackerrank IDE.

public static int minX(List<Integer> arr) {
        int x=0;
        arr.remove(0);
        boolean limitFound = false;
        while(!limitFound){
            int sum=x;
            for(Integer i: arr){
                sum+=i;
                if(sum<1){
                    break;
                }
            }
            limitFound = sum<1?false:true;
            if(limitFound){
                break;
            }
            x++;
        }
        return x;
        }

I really can't find anything wrong with my solution,Also, Please help me understand why it failed there and when I try same in my local IDE, it passed all the same test scenarios.Also, how to improve the code will be helpful.

The following code solved the problem:

public int minX(int[] nums) {
    int startValue = 0; //Integer.MIN_VALUE
    for (int x = startValue; x <= Integer.MAX_VALUE; x++) {
        int sum = x;
        for (int n : nums) {
            sum += n;
            if (sum < 1) {
                break;
            }
        }
        if (sum >= 1) {
            return x;
        }
    }
    return 0;
}
#include <iostream>

using namespace std;

int main() {
    int a[]={-2,3,1,-5};
    int sum=0;
    int ans=0;
    int n=sizeof(a)/sizeof(a[0]);
    for(int i=0;i<n;i++){
        sum+=a[i];
        if(sum<1){
            sum=-sum;
            ans+=sum+1;
            sum=1;
        }
    }
    cout << ans << "\n";
    return 0;
}

on removing arr.remove(0); in the third line it works. I think HackerRank IDE calculates inclusive of value 0

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int minX(vector<int> &arr)
{
    int sumwX = 1;
    for (auto it = arr.rbegin(); it != arr.rend(); ++it)
    {
        if(sumwX < 1) sumwX = 1;
        sumwX -= *it;
    }
    
    return sumwX;
}

int main()
{
    
    vector<int> M = {-5,4,-2,3,29,-1,-6,-1,0,-5}; // 6
    vector<int> N = {-2,3,1,-5}; //4
    vector<int> V = {-5,4,-2,3,1,-1,-6,-1,0,-5}; //13
    cout << minX(M) << endl;
    cout << minX(N) << endl;
    cout << minX(V) << endl;
    return 0;
}

Tried this..it worked (yes locally). If you can provide more details about the TCs for which it is failing in hacker rank and may be you can also try adding some sysout to get to know better. Possibly can you post the hacker rank que link too:)

public static int minX(List<Integer> arr) {
        int x = 0;
        arr.remove(0);
        while (true) {
            int sum = x;
            for (Integer i : arr) {
                sum += i;
            }
            if (sum == 1) {
                return x;
            } else {
                x++;
            }
        }
    }
import java.util.List;
import java.util.Arrays;
import static java.lang.System.out;
class Playground {
    private static boolean doesRunningTotalNeverGoBelowOneForSeed(
      List<Integer> list, 
      Integer seed 
    ) {
        boolean result = true;
        int total = seed;
        for (Integer i : list) {
            total += i;
            if (total < 1) {
                result = false;
                break;
            }
        }
        return result;
    }
    public static void main(String[ ] args) {
        Integer[] a = {-5, 4, -2, 3, 1, -1, -6, -1, 0, -5};//{-2,3,1,-5};
        List<Integer> list = Arrays.asList(a);
        out.println(doesRunningTotalNeverGoBelowOneForSeed(list, 13)); //4, 6
    }
      
}

This is the way I would have done it.

Here is output in Python. Hacker Rank Tested Output.

def findMin(arr):
    startValue = 0
    while True:
        sum = startValue;
        for n in arr:
            sum += n
            if sum < 1:
                break
        if sum >= 1:
            return startValue
        startValue += 1

if _name_ == '_main_':
    print(findMin([-2, 3, 1, -5])) #output 4
    print(findMin([-5, 4, -2, 3, 1]))  #output 6
    print(findMin([-5, 4, -2, 3, 1, -1, -6, -1, 0, -5]))  #output 13
    print(findMin([-5, 4, -2, 3, 1, -1, -6, -1, 0, 5]))  #output 8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM