0
\$\begingroup\$

I have asked to write a program to find if there is a sub-array with a sum equal to zero using JavaScript.

Is this a good solution? How can I improve it?

 /**
  * Model For to find if there is a sub-array with sum equal to zero
  */

 class Model {
    constructor(fst, scnd) {
        this.first = fst;
        this.second = scnd;
    }
}

/**
 * to find if there is a sub-array with sum equal to zero
 */
class ProblemStamentSecond {

    /**
     * This function finds sub array having zero sum
     * @input : array, lenth
     * @output : NA
     */
    findSubArrays(arr, n) {
        var dictnr = {};
        var isArrSumZero = [];
        var trackSum = 0;
        for (var i = 0; i < n; i++) {
            {
                trackSum += arr[i];
                if (trackSum === 0)
                    (isArrSumZero.push(new Model(0, i)) > 0);
                var al = [];
                if ((function (m, k) {
                        if (m.entries == null)
                            m.entries = [];
                        for (var i_1 = 0; i_1 < m.entries.length; i_1++)
                            if (m.entries[i_1].key.equals != null && m.entries[i_1].key.equals(k) || m.entries[i_1].key === k) {
                                return true;
                            } return false;
                    })(dictnr, trackSum)) {
                    al = (function (m, k) {
                        if (m.entries == null)
                            m.entries = [];
                        for (var i_2 = 0; i_2 < m.entries.length; i_2++)
                            if (m.entries[i_2].key.equals != null && m.entries[i_2].key.equals(k) || m.entries[i_2].key === k) {
                                return m.entries[i_2].value;
                            } return null;
                    })(dictnr, trackSum);
                    for (var it = 0; it < al.length; it++) {
                        {
                            (isArrSumZero.push(new Model(al[it] + 1, i)) > 0);
                        };
                    }
                }
                (al.push(i) > 0);
                (function (m, k, v) {
                    if (m.entries == null)
                        m.entries = [];
                    for (var i_3 = 0; i_3 < m.entries.length; i_3++)
                        if (m.entries[i_3].key.equals != null && m.entries[i_3].key.equals(k) || m.entries[i_3].key === k) {
                            m.entries[i_3].value = v;
                            return;
                        } m.entries.push({
                        key: k,
                        value: v,
                        getKey: function () {
                            return this.key;
                        },
                        getValue: function () {
                            return this.value;
                        }
                    });
                })(dictnr, trackSum, al);
            };
        }
        return isArrSumZero;
    }

    /**
    * This function prints logs
    * @input : {Model} to print
    * @output : NA
    */
    logger(arrSumZero) {
        for (var i = 0; i < arrSumZero.length; i++) {
            {
                var p = arrSumZero[i];
                console.info("There is a sub-array with zero sum from index " + p.first + " to " + p.second);
            };
        }
    }

    /**
     *
     * @input : na
     * @output : NA
     */
    main() {
        var arr = [4, 2, -3, 1, 6];
        var n = arr.length;
        var arrSumZero = this.findSubArrays(arr, n);
        if (arrSumZero.length === 0)
            console.info("There is NO a sub-array with zero");
        else
            this.logger(arrSumZero);
    }

}

var user = new ProblemStamentSecond();
user.main();
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.