Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

Check if a subarray with 0 sum exists or not

#include <iostream>
#include <unordered_set>
using namespace std;

bool hasZeroSumSubarray(int nums[], int n){
    unordered_set<int> set;
    set.insert(0);
    int sum = 0;
 
    for (int i = 0; i < n; i++)
    {
        sum += nums[i];
        if (set.find(sum) != set.end()) {
            return true;
        }
        else {
            set.insert(sum);
        }
    }
    return false;
}
 
int main()
{
    int nums[] = { 4, 2 , 5 };
    int n = sizeof(nums)/sizeof(nums[0]);
 
    hasZeroSumSubarray(nums, n) ?
            cout << "Subarray exists":
            cout << "Subarray does not exist";

    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: mat-checkbox change 
Typescript :: angular date to string format 
Typescript :: mysql workbench an apparmor policy prevents this sender 
Typescript :: how to remove digits in string in python? 
Typescript :: whats the power house of the cell 
Typescript :: Hide all elements with class jQuery 
Typescript :: union of two sets python syntax 
Typescript :: vue 3 setup props typescript 
Typescript :: python all elements in list in another list 
Typescript :: requests to check is url exists in python using function 
Typescript :: mat stepper dont clickable 
Typescript :: get documents path c# 
Typescript :: remove empty objects from array lodash 
Typescript :: display current directory contents in a long format with user and group ids displayed numerically 
Typescript :: typescript remove whitespace from string 
Typescript :: npx creat redux typescript app 
Typescript :: how to connect postgress server in pgadmin 
Typescript :: typescript valueof interface 
Typescript :: laravel custom exists rule 
Typescript :: typescript bigint vs number 
Typescript :: how to use get element by id in angular 
Typescript :: what is endurance testing 
Typescript :: average of two lists python 
Typescript :: google reference static 
Typescript :: typescript with node on mac 
Typescript :: typescript checkbox event 
Typescript :: typescript array of mixed types 
Typescript :: typescript typecast 
Typescript :: moment datepicker 
Typescript :: select field where name starts a in sql 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =