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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,887 questions

51,814 answers

573 users

How to find all pairs in an array that equal to a given sum in Dart

1 Answer

0 votes
class MyClass
{
    static void findAllPairs(List<int>arr, int sum) {
        var found = false;
        for (var i = 0; i < arr.length - 1; i++) {
            for (var j = i + 1; j < arr.length; j++) {
                if (arr[i] + arr[j] == sum) {
                    print("arr[" + (i).toString() + "](" + (arr[i]).toString() + 
                          ") + arr[" + (j).toString() + "](" + (arr[j]).toString() + ")");
                    found = true;
                }
            }
        }
        if (!found) {
            print("Pair not found");
        }
    }
    static void main()
    {
        List<int> arr = [2, 4, 1, 5, 6, 8, 1];
        
        var sum = 10;
        
        MyClass.findAllPairs(arr, sum);
    }
}

void main() {
	MyClass.main();
}





/*
run:

arr[0](2) + arr[5](8)
arr[1](4) + arr[4](6)

*/

 



answered Apr 16, 2023 by avibootz
edited Apr 16, 2023 by avibootz

Related questions

...