Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Semrush - keyword research tool

Linux Foundation Training and Certification

Teach Your Child To Read

Disclosure: My content contains affiliate links.

32,309 questions

42,484 answers

573 users

How to find the sub list whose sum is equal to a given number N in Dart

1 Answer

0 votes
import 'dart:io';

void PrintSubarrayWithSumEqualToN(List<int>arr, int N) {
    var size = arr.length;
    
    for (var i = 0; i < size; i++) {
        var current_sum = arr[i];
        if (current_sum == N) {
            stdout.write("Sum found at index: " + (i).toString());
            return;
        }
        else {
                for (var j = i + 1; j < size; j++) {
                    current_sum += arr[j];
                    if (current_sum == N) {
                        print("Sum found between index " + i.toString() + " and " + j.toString());
                        for (var k = i; k <= j; k++) {
                            stdout.write((arr[k]).toString() + " ");
                        }
                        return;
                    }
                    else {
                        if (current_sum > N) {
                            break;
                        }
                    }
                }
            }
        }
    stdout.write("No subarray found");
}

void main() {
    List<int> arr = [2, 5, 8, 9, 1, 7, 12, 21, 19];
    
    var N = 52;
    
    PrintSubarrayWithSumEqualToN(arr, N);
}




/*
run:

Sum found between index 6 and 8
12 21 19 

*/

 



Learn & Practice Python
with the most comprehensive set of 13 hands-on online Python courses
Start now


answered Apr 16, 2023 by avibootz
...