LC 1539. Kth Missing Positive Number

Nilanjan Deb · October 11, 2020

This is my CPP solution.

class Solution {
public:
    int sumOddLengthSubarrays(vector<int>& arr) {
        int sum = 0;
        int n = arr.size();
        int ans = 0;
        for(int i=0; i<n; i++) {
            int odd=1;
            int sum=0;
            for(int j=i; j<n; j++) {
                sum+=arr[j];
                if(odd%2==1) ans += sum;
                odd++;
            }
        }
        return ans;
    }
};


Dicussion Forum