Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

find subarray with given sum

import java.sql.Array;
import java.util.ArrayList;
import java.util.Arrays;

public class FindSubArr {
    public static void main(String[] args) {
        int [] arr = {0,1,2,3,4,5,6,9,2,1,1,1,10,2,2,2};
        int s = 6 ;
        int [] sub = findLongestSubArray( arr,s);
        System.out.println("longest SubArray Range ==> "+Arrays.toString(sub));

    }

    public static int[] findLongestSubArray(int [] arr, int s){

        int[] result = new int[]{-1};
        int sum=0,left=0,right=0;

        while(right < arr.length){
            sum += arr[right];
            while(left < right && sum > s){
                sum -= arr[left++];
            }
            if(sum == s && (result.length == 1 || result[1] - result[0] < right - left)){
                result = new int[]{left + 1, right +1};
            }
            right++;
        }
        return result;
    }
}
Comment

Subarray with given sum python

Input:
N = 10, S = 15
A[] = {1,2,3,4,5,6,7,8,9,10}
Output: 1 5
Explanation: The sum of elements 
from 1st position to 5th position
is 15.
Comment

find subarray with given sum

import java.util.*;

public class FindSubarrayWithGivenSum1 {
	static int findSubarray(int[] arr,int n,int sum){
		int start =0;
		int end = 1;
		int curr_sum= arr[0];

		while(end <=n){
			while(curr_sum>sum && start < end-1){
				curr_sum-=arr[start];
				start++;
			}

			if(curr_sum == sum){
				System.out.println("Subarray found at "+start+" "+(end-1));
				return 1;
			}

			if(end < n){
				curr_sum+=arr[end];
			}
			end++;
		}
		System.out.println("no subarray found.");
		return 0;

	}

	public static void main(String[] args){
		int[] arr = {1, 4, 20, 3, 10, 5};
		int n = arr.length;
		int s = 33;
		findSubarray(arr,n,s);
	}
}
Comment

Find subarrays with given sum in an array

class SubarraySum {
    /* Returns true if the there is
a subarray of arr[] with sum equal to
       'sum' otherwise returns false.
Also, prints the result */
    int subArraySum(int arr[], int n, int sum)
    {
        int currentSum = arr[0], start = 0, i;
 
        // Pick a starting point
        for (i = 1; i <= n; i++) {
            // If currentSum exceeds the sum,
            // then remove the starting elements
            while (currentSum > sum && start < i - 1) {
                currentSum = currentSum - arr[start];
                start++;
            }
 
            // If currentSum becomes equal to sum,
            // then return true
            if (currentSum == sum) {
                int p = i - 1;
                System.out.println(
                    "Sum found between indexes " + start
                    + " and " + p);
                return 1;
            }
 
            // Add this element to curr_sum
            if (i < n)
                currentSum = currentSum + arr[i];
        }
 
        System.out.println("No subarray found");
        return 0;
    }
 
    public static void main(String[] args)
    {
        SubarraySum arraysum = new SubarraySum();
        int arr[] = { 15, 2, 4, 8, 9, 5, 10, 23 };
        int n = arr.length;
        int sum = 23;
        arraysum.subArraySum(arr, n, sum);
    }
}
 
// This code has been contributed by Mayank
// Jaiswal(mayank_24)
Comment

PREVIOUS NEXT
Code Example
Java :: generics Interface in java 
Java :: java map sorted by key 
Java :: try catch in java 
Java :: spring boot basic authentication 
Java :: package javafx.fxml does not exist 
Java :: java pass by reference 
Java :: java string class 
Java :: calling a void method java 
Java :: java mahout get clusters centers 
Java :: eclipse versioning .classpath 
Java :: jdsu software download 
Java :: Java public no-arg constructor 
Java :: using for loop to search for an input java 
Java :: teimpo en segundos java 
Java :: jkora 
Java :: labeled break Statement Java 
Java :: android open app info programmatically 
Java :: android java string animations 
Java :: trier un tableau de string java 
Java :: how to remove all components from layeredPane java 
Java :: Get directory in android java 
Java :: Set value of input in android webview 
Java :: convert boolean to Boolean class 
Java :: jtable fill panel 
Java :: Was ist ein String in java 
Java :: java stream anymatch vs findany 
Java :: dialog background dimming in android 
Java :: isblank vs isempty java string utils 
Java :: Android kotlin send debug log as slack message 
Java :: transpose array in java 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =