Click here to Skip to main content
15,911,360 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k.


Error:

Line 9: Char 9: runtime error: variable length array bound evaluates to non-positive value 0 [solution.c]

Why I am getting this error?

Thank you very much!

What I have tried:

C++
bool containsNearbyDuplicate(int* nums, int numsSize, int k){
    int i=0;
    int max=nums[0];
    for(i=0;i<numsSize;i++)
    {
        if(max<nums[i])
            max=nums[i];
    }
    int hash[max+1];   
    for(i=0;i<max+1;i++)
    {
        hash[i]=0;
    }
    for(i=0;i<numsSize;i++)
    {
        hash[nums[i]]++;
    }
    int s=0,i1,i2,j;
    while(s<max+1)
    {
       if(hash[s]>1)
       {
           for(i=0;i<numsSize;i++)
           {
               if(nums[i]==s)    //
               {
                   i1=i;
                   j=i+1;
                   while(j<numsSize)
                   {
                       if(nums[j]==s)  //
                       {
                           i2=j;
                           if(i2-i1<=k)
                           {
                               return true;
                           }
                           else
                           {
                               j++;    //    
                           }
                       }
                       else
                       {
                           j++;   //
                       }
                   }
                   
               }
           }
           s++;    //
       }
        else
    {
        s++;
    }
    }
    
    return false;
}
Posted
Updated 17-Jun-21 2:11am

Use the debugger to find ist out. I guess it is a bad input value.

tip: write some testcode, beginning with simple test case.
 
Share this answer
 
Comments
_-_-_-me 17-Jun-21 3:32am    
I got it now!
Here In the question it was not specified that nums[] array would be only positive elements.
There are chances that the elements of nums[] array would be negative.
In that case the hash[] would access negative index which is not valid.
Thank you !
Quote:
Why I am getting this error?

C++
bool containsNearbyDuplicate(int* nums, int numsSize, int k){
    int i=0;
    int max=nums[0];
    for(i=0;i<numsSize;i++)
    {
        if(max<nums[i])
            max=nums[i];
    }
    int hash[max+1];   // What if max < 0 here ?


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
_-_-_-me 17-Jun-21 8:14am    
okay , Thank you !
In the case of max<0
The code:

C++
bool containsNearbyDuplicate(int* nums, int numsSize, int k){
    int i=0;
    int max=nums[0];
    int min=nums[0];
    for(i=0;i<numsSize;i++)
    {
        if(max<nums[i])
            max=nums[i];
        if(min>nums[i])
            min=nums[i];
    }
     
   
    if(max>=0)
    {
         int hash1[max+1];   
    for(i=0;i<max+1;i++)
    {
        hash1[i]=0;
    }
        
         for(i=0;i<numsSize;i++)
    {
        if(nums[i]>=0)
        {
            hash1[nums[i]]++;
        }
    }
        
        int s=0,i1,i2,j;
    while(s<max+1)
    {
       if(hash1[s]>1)
       {
           for(i=0;i<numsSize;i++)
           {
               if(nums[i]==s)    //
               {
                   i1=i;
                   j=i+1;
                   while(j<numsSize)
                   {
                       if(nums[j]==s)  //
                       {
                           i2=j;
                           if(i2-i1<=k)
                           {
                               return true;
                           }
                           else
                           {
                               j++;    //    
                           }
                       }
                       else
                       {
                           j++;   //
                       }
                   }
                   
               }
           }
           s++;    //
       }
        else
    {
        s++;
    }
    }
    }
    if(min<0)
    {
         int hash2[(-1*min)+1];   
    for(i=0;i<(-1*min)+1;i++)
    {
        hash2[i]=0;
    }
         for(i=0;i<numsSize;i++)
    {
        if(nums[i]<0)
        {
            hash2[-1*nums[i]]++;
        }

         }
        
         int s=0,i1,i2,j;
     while(s<(-1*min)+1)
    {
       if(hash2[s]>1)
       {
           for(i=0;i<numsSize;i++)
           {
               if(nums[i]==(-1*s))    //
               {
                   i1=i;
                   j=i+1;
                   while(j<numsSize)
                   {
                       if(nums[j]==(-1*s))  //
                       {
                           i2=j;
                           if(i2-i1<=k)
                           {
                               return true;
                           }
                           else
                           {
                               j++;    //    
                           }
                       }
                       else
                       {
                           j++;   //
                       }
                   }
                   
               }
           }
           s++;    //
       }
        else
    {
        s++;
    }
    }
    
         }
    
    
    return false;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900