using System;
using System.Collections.Generic;

namespace MyCompiler {
    class Program {
        public static void Main(string[] args) {
            //=======================START=============================//
            //For above code below is the time and space complexity 
            //Time Complexity: O(N2)
            //Auxiliary Space: O(1)
            //int[] array = {15, -2, 2, -8, 1, 7, 10, 23};
            int[] array = {1,-1,3,2,-2,-8,1,7,10,23};
            int currentSum = 0 ;
            //int maxlength = 0;
            //for(int i=0; i< array.Length ; i++){
            //    currentSum = array[i];
            //    for(int j = i + 1 ; j< array.Length; j++){
            //        Console.WriteLine("Value of J {0}",j);
            //        Console.WriteLine("Current Sum {0}",currentSum);
            //        currentSum += array[j];
            //        if(currentSum == 0){
            //            Console.WriteLine("Max lenght will be set to this  {0}",j);
            //            maxlength = j;
            //        }
            //    }
            //}
            //Console.WriteLine("The Max Length of the longest subarray is {0}", maxlength);
            //=======================END=============================//

            //=======================START=============================//
            //Using Hashing
            Dictionary<int,int> dictOfSumzero = new Dictionary<int,int>();
            int maxlength = 0;
            int previndex = 0;
            for(int i = 0 ; i< array.Length; i++){
                currentSum += array[i];
                Console.WriteLine("Value in dictOfSumzero");
                Console.WriteLine(String.Join(",",dictOfSumzero));
                if(currentSum == 0){
                    maxlength = i+1;
                }
                else if(dictOfSumzero.ContainsKey(currentSum)) {
                    previndex = dictOfSumzero[currentSum];
                    maxlength = Math.Max(maxlength,(i-previndex));
                }
                else{
                    dictOfSumzero.Add(currentSum,i);
                }
                
            }
            Console.WriteLine("The Max Length of the longest subarray is {0}", maxlength);
            Console.WriteLine("Hello world!");
        }
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: