Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Subarray sum and Hashmap

  1. #1
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Subarray sum and Hashmap

    I'm looking for an explanation of an algorithm of a program solving the following problem: Write a Java program to get the index of the first number and the last number of a subarray where the sum of numbers is zero from a given array of integers.

    Here is the code :

    import java.util.*;
    public class Solution {
        public static List<Integer> subarraySum(int[] nums) {
            List<Integer> temp = new ArrayList<>();
            if (nums == null || nums.length == 0) {
                return temp;
            }
            int pre_Sum = 0;
            Map<Integer, Integer> map = new HashMap<>();
            map.put(pre_Sum, -1);
            for (int i = 0; i < nums.length; i++) {
                pre_Sum += nums[i];
                if (map.containsKey(pre_Sum)) {
                    temp.add(map.get(pre_Sum) + 1);
                    temp.add(i);
                    return temp;
                }
                map.put(pre_Sum, i);
            }
            return temp;
        }
     
    public static void main(String[] args) {
    		int [] nums = {1, 2, 3, -6, 5, 4};
    		System.out.println("Original Array : "+Arrays.toString(nums));
    		System.out.println("Index of the subarray of the said array where the sum of numbers is zero: "+subarraySum(nums));
    	}		
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Subarray sum and Hashmap

    I'm looking for an explanation of an algorithm
    Can you post the algorithm that you are asking about?

    To see what the code does, add some print statement that print out the values of variables as the executes. For example print out map and pre_Sum
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Subarray sum and Hashmap

    Sorry, I meant an explanation of this program. Also, I have another question, how useful is Hashmap in that kind of problem like this one.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Subarray sum and Hashmap

    To see what the code does, add some print statements that print out the values of variables as the executes. For example print out map and pre_Sum.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. HashMap
    By jocdrew21 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 2nd, 2014, 01:36 PM
  2. Help with hashmap and heap
    By aiojou in forum What's Wrong With My Code?
    Replies: 24
    Last Post: April 28th, 2014, 09:47 AM
  3. Hashmap
    By srkmca07 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 31st, 2013, 07:36 AM
  4. Hashmap
    By ryan12345 in forum Java Theory & Questions
    Replies: 6
    Last Post: November 2nd, 2012, 07:04 PM
  5. [SOLVED] Should i use a hashmap?
    By 6Sloth9 in forum Collections and Generics
    Replies: 2
    Last Post: May 1st, 2011, 08:58 PM

Tags for this Thread