w3resource

Java Exercises: Find the total number of continuous subarrays in a specified array of integers

Java Basic: Exercise-202 with Solution

Write a Java program to find the total number of continuous subarrays in a given array of integers whose sum equals to an given integer.

Example:
Original Array: [4, 2, 3, 3, 7, 2, 4]
Value of k: 6 Total number of continuous subarrays: 3

Pictorial Presentation:

Java Basic Exercises: Find the total number of continuous subarrays in a specified array of integers

Sample Solution:

Java Code:

import java.util.*;
public class Solution {
    public static void main(String[] args) {		
        int[] nums = {4,2,3,3,7,2,4};
		int k = 6;
		System.out.print("Original Array: "+Arrays.toString(nums));
		System.out.print("\nValue of k: "+k);
		System.out.print("\nTotal number of continuous subarrays: "+max_SubArray(nums, k));       
    }     
   public static int max_SubArray(int[] nums, int k) {
   int ctr = 0, sum = 0;
        Map<Integer, Integer>map = new HashMap<>();
        
        map.put(0, 1);
        
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            
            if (map.containsKey(sum - k)) {
                ctr = ctr + map.get(sum - k);
            }
            
            if (map.containsKey(sum)) {
                map.put(sum, map.get(sum) + 1);
            } else {
                map.put(sum, 1);
            }
        }
        
        return ctr;
    }
}


Sample Output:

Original Array: [4, 2, 3, 3, 7, 2, 4]
Value of k: 6
Total number of continuous subarrays: 3

Flowchart:

Flowchart: Java exercises: Find the total number of continuous subarrays in a specified array of integers

Java Code Editor:

Company:  Google

Contribute your code and comments through Disqus.

Previous: Write a Java program to divide a given array of integers into given k non-empty subsets whose sums are all equal. Return true if all sums are equal otherwise return false.
Next: Write a Java program to find the contiguous subarray of given length k which has the maximum average value of a given array of integers. Display the maximum average value.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

Java: Tips of the Day

How to remove leading zeros from alphanumeric text?

Regex is the best tool for the job; what it should be depends on the problem specification. The following removes leading zeroes, but leaves one if necessary (i.e. it wouldn't just turn "0" to a blank string).

s.replaceFirst("^0+(?!$)", "")

The ^ anchor will make sure that the 0+ being matched is at the beginning of the input. The (?!$) negative lookahead ensures that not the entire string will be matched.

Test harness:

String[] in = {
    "01234",         // "[1234]"
    "0001234a",      // "[1234a]"
    "101234",        // "[101234]"
    "000002829839",  // "[2829839]"
    "0",             // "[0]"
    "0000000",       // "[0]"
    "0000009",       // "[9]"
    "000000z",       // "[z]"
    "000000.z",      // "[.z]"
};
for (String s : in) {
    System.out.println("[" + s.replaceFirst("^0+(?!$)", "") + "]");
}

Ref: https://bit.ly/2Qdcl8a