Continuous Subarray Sum

question–>


code–>

 class Solution {
    public:
        bool checkSubarraySum(vector<int>& nums, int k) {
            unordered_map<int,int> m;
            m[0]=-1;
           
            int* csum=new int[nums.size()];
            csum[0]=nums[0];
            for(int i=1;i<nums.size();i++){
                csum[i]=nums[i]+csum[i-1];
            }
            for(int i=0;i<nums.size();i++){
                if(k!=0){
                if(m.count(csum[i]%k)){
                    if(i-m[csum[i]%k]>1){
                        return true;
                    }
                }
                m[csum[i]%k]=i;}
                else if(k==0 and nums[i]==0 and i<nums.size()-1 and nums[i+1]==0 ){
                    return true;
                }
            }
            return false;
        }
    };

test case not passing
[0,0]
-1

@tusharaggarwal272 are you there?

hello @sheikhhaji18 yes what is your doubt?

the code above is not passing all testcases help

@sheikhhaji18 where your are submitting on leetcode?

yes iam submitting in leetcode @tusharaggarwal272

Go to the details of the test cases and you will got to know which test cases are not getting passes and what can you do and i am also trying.
Happy Learning!!

yes i am trying @tuasharaggarwal

@sheikhhaji18i think your logic is right but it should be framed agaoin to handle corner test cases.
class Solution {
public:
bool checkSubarraySum(vector& nums, int k) {

    int N = nums.size();
    
    for (int i = 0; i < N; i++)
    {
        int sum = nums[i];
        
        for (int j = i+1; j < N; j++)
        {
            sum += nums[j];
            
            if (sum == k) return true;
            
            if (k != 0 && sum % k == 0) return true;
        }
    }
    
    return false;
}

};
see this code it is passing every test case:
if you have any doubt you can ask here:
Happy Learnin!!

okay @tusharaggarwal272 i didnot see the constraint i just saw the video and implemented it ,it will work fine for n^2

@sheikhhaji18 yes its okay.
you can mark this doubt as resolved.
Happy Learning!!

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.