# How to count the number of possible triangles from a given list in Dart

50 views

```// Triangle = the sum of any two values (sides) > than the third value (third side)

// A + B > C
// B + C > A
// C + A > B

import 'dart:io';

int CountTriangles(List<int> lst) {
var size = lst.length;
var count = 0;
for (var i = 0; i < size; i++) {
for (var j = i + 1; j < size; j++) {
for (var k = j + 1; k < size; k++) {
if (lst[i] + lst[j] > lst[k] && lst[i] + lst[k] > lst[j] && lst[k] + lst[j] > lst[i]) {
count++;
stdout.write((lst[i]).toString() + " + " + (lst[j]).toString() + " > " + (lst[k]).toString() + " | ");
stdout.write((lst[i]).toString() + " + " + (lst[k]).toString() + " > " + (lst[j]).toString() + " | ");
print((lst[k]).toString() + " + " + (lst[j]).toString() + " > " + (lst[i]).toString());
}
}
}
}
return count;
}

void main() {
List<int> lst = [120, 80, 13, 16, 9, 14, 19];

var total_triangles = CountTriangles(lst);

stdout.write("Total triangles : " + (total_triangles).toString());
}

/*
run:

13 + 16 > 9 | 13 + 9 > 16 | 9 + 16 > 13
13 + 16 > 14 | 13 + 14 > 16 | 14 + 16 > 13
13 + 16 > 19 | 13 + 19 > 16 | 19 + 16 > 13
13 + 9 > 14 | 13 + 14 > 9 | 14 + 9 > 13
13 + 9 > 19 | 13 + 19 > 9 | 19 + 9 > 13
13 + 14 > 19 | 13 + 19 > 14 | 19 + 14 > 13
16 + 9 > 14 | 16 + 14 > 9 | 14 + 9 > 16
16 + 9 > 19 | 16 + 19 > 9 | 19 + 9 > 16
16 + 14 > 19 | 16 + 19 > 14 | 19 + 14 > 16
9 + 14 > 19 | 9 + 19 > 14 | 19 + 14 > 9
Total triangles : 10

*/```

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