fork(1) download
  1. function getLoveTrianglesCount(preferences = []) {
  2. // your implementation
  3. let length = preferences.length;
  4. let count = 0;
  5. for (let i = 0; i<length; i++) {
  6. let next = preferences[i] - 1;
  7. if (next == i) continue;
  8. let nextnext = preferences[next] - 1;
  9. if (next == nextnext) continue;
  10. let nextnextnext = preferences[nextnext] - 1;
  11. if (nextnextnext == nextnext) continue;
  12. if (nextnextnext == i) {
  13. count++;
  14. }
  15. }
  16. return count / 3;
  17. }
  18.  
  19. print(getLoveTrianglesCount([5,5,2,3,3]));
  20. print(getLoveTrianglesCount([4,3,3,6,6,1]));
  21. print(getLoveTrianglesCount([4,3,3,6,8,1,5,7]));
  22.  
  23.  
Success #stdin #stdout 0s 105856KB
stdin
Standard input is empty
stdout
1
1
2