Find if there is any subarray with sum equal to 0

June 24, 2022 · 1 min read

https://practice.geeksforgeeks.org/problems/subarray-with-0-sum/0

class Solution {
public:
  bool subArrayExists(int arr[], int n) {
    int pSum[n];
    partial_sum(arr, arr + n, pSum);

    set<int> st;

    for (int i = 0; i < n; i++) {
      if (pSum[i] == 0 || st.find(pSum[i]) != st.end())
        return true;
      st.insert(pSum[i]);
    }

    return false;
  }
};
Rearrange the array in alternating positive and negative items with O(1) extra space
Find factorial of a large number