diff --git a/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.big.testcases.json b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.big.testcases.json new file mode 100644 index 00000000..ca1cfea6 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.big.testcases.json @@ -0,0 +1,55 @@ +[ + { + "title": "Test case 4", + "nodes": [[-1, 2], [3, 4], [5, 6], [7, -1], [8, -1], [9, 10], [11, 12], [-1, 13], [14, 15], [-1, -1], [-1, 16], [17, -1], [18, 19], [20, 21], [22, 23], [-1, -1], [24, 25], [26, -1], [27, 28], [29, 30], [31, -1], [32, 33], [34, 35], [36, 37], [38, 39], [-1, 40], [-1, 41], [-1, 42], [-1, -1], [43, 44], [-1, -1], [45, 46], [47, 48], [-1, -1], [-1, 49], [-1, 50], [51, 52], [-1, 53], [54, 55], [-1, 56], [57, -1], [-1, 58], [59, -1], [60, 61], [-1, 62], [-1, 63], [-1, -1], [-1, 64], [65, -1], [66, -1], [-1, 67], [-1, -1], [-1, 68], [-1, 69], [70, -1], [71, -1], [72, 73], [74, 75], [-1, -1], [76, -1], [77, -1], [-1, -1], [78, -1], [-1, -1], [79, 80], [81, 82], [-1, 83], [84, -1], [85, -1], [86, -1], [-1, 87], [-1, -1], [-1, -1], [-1, 88], [-1, -1], [89, 90], [-1, -1], [91, -1], [-1, 92], [93, -1], [94, 95], [-1, -1], [96, 97], [98, -1], [-1, 99], [100, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1]], + "flattened": [1, 8, 26, 40, 71, 87, 56, 18, 13, 27, 72, 57, 73, 41, 19, 28, 42, 74, 88, 58, 75, 5, 3, 29, 20, 59, 43, 30, 89, 76, 90, 60, 44, 77, 61, 14, 31, 21, 9, 45, 62, 32, 46, 91, 78, 63, 22, 47, 33, 48, 64, 15, 34, 23, 35, 79, 92, 65, 93, 80, 49, 6, 10, 2, 11, 16, 7, 36, 94, 81, 95, 66, 82, 50, 24, 51, 67, 96, 83, 97, 37, 52, 17, 38, 53, 98, 84, 68, 25, 54, 85, 99, 69, 39, 100, 86, 70, 55, 12, 4], + "queries": [8, 6, 2, 3, 5, 7, 7, 5, 7, 1, 5, 6, 2, 8, 4, 6, 8, 12, 10, 6, 12, 11, 12, 8, 1, 7, 9, 3, 8, 7, 2, 7, 8, 3, 8, 9, 7, 9, 9, 8, 11, 10, 4, 12, 4], + "expected": [ + [1, 8, 40, 71, 87, 56, 26, 18, 13, 72, 57, 73, 41, 27, 19, 42, 74, 88, 58, 75, 28, 5, 3, 29, 20, 89, 76, 90, 60, 44, 77, 61, 30, 59, 43, 14, 31, 21, 9, 46, 91, 78, 63, 32, 45, 62, 22, 48, 64, 33, 47, 15, 34, 23, 79, 92, 65, 93, 80, 49, 35, 6, 10, 2, 11, 16, 7, 94, 81, 95, 66, 82, 50, 36, 24, 52, 37, 51, 67, 96, 83, 97, 17, 53, 98, 84, 68, 38, 25, 100, 86, 70, 55, 39, 54, 85, 99, 69, 12, 4], + [1, 8, 72, 57, 73, 41, 27, 19, 42, 74, 88, 58, 75, 28, 13, 40, 71, 87, 56, 26, 18, 5, 3, 31, 21, 14, 29, 20, 89, 76, 90, 60, 44, 77, 61, 30, 59, 43, 9, 34, 23, 79, 92, 65, 93, 80, 49, 35, 15, 46, 91, 78, 63, 32, 45, 62, 22, 48, 64, 33, 47, 6, 10, 2, 11, 16, 7, 53, 98, 84, 68, 38, 25, 100, 86, 70, 55, 39, 54, 85, 99, 69, 17, 94, 81, 95, 66, 82, 50, 36, 24, 52, 37, 51, 67, 96, 83, 97, 12, 4], + [1, 36, 82, 66, 94, 81, 95, 50, 24, 51, 96, 83, 97, 67, 37, 52, 17, 38, 53, 68, 98, 84, 25, 54, 69, 85, 99, 39, 70, 100, 86, 55, 12, 7, 11, 16, 4, 2, 5, 8, 26, 40, 56, 71, 87, 18, 13, 27, 73, 57, 72, 41, 19, 28, 42, 75, 58, 74, 88, 3, 10, 6, 29, 20, 59, 43, 30, 60, 89, 76, 90, 44, 61, 77, 14, 31, 21, 9, 45, 62, 32, 46, 63, 91, 78, 22, 47, 33, 48, 64, 15, 34, 23, 35, 93, 80, 65, 79, 92, 49], + [1, 4, 38, 68, 98, 84, 53, 25, 69, 85, 99, 54, 39, 55, 70, 100, 86, 17, 36, 50, 82, 66, 94, 81, 95, 24, 96, 83, 97, 67, 51, 37, 52, 12, 7, 11, 16, 2, 10, 6, 31, 21, 14, 29, 20, 43, 59, 30, 61, 77, 44, 60, 89, 76, 90, 9, 34, 23, 35, 49, 93, 80, 65, 79, 92, 15, 62, 45, 32, 63, 91, 78, 46, 22, 47, 33, 64, 48, 3, 5, 8, 27, 41, 73, 57, 72, 19, 28, 75, 58, 74, 88, 42, 13, 26, 56, 71, 87, 40, 18], + [1, 4, 12, 38, 98, 84, 68, 53, 25, 85, 99, 69, 54, 39, 55, 100, 86, 70, 17, 36, 50, 94, 81, 95, 66, 82, 24, 67, 96, 83, 97, 51, 37, 52, 7, 16, 11, 2, 10, 6, 34, 23, 35, 49, 79, 92, 65, 93, 80, 15, 62, 45, 32, 91, 78, 63, 46, 22, 47, 33, 64, 48, 9, 31, 21, 14, 29, 20, 43, 59, 30, 77, 61, 44, 89, 76, 90, 60, 3, 5, 27, 41, 72, 57, 73, 19, 28, 74, 88, 58, 75, 42, 13, 26, 71, 87, 56, 40, 18, 8], + [1, 4, 12, 85, 99, 69, 54, 39, 55, 100, 86, 70, 25, 38, 98, 84, 68, 53, 17, 67, 96, 83, 97, 51, 37, 52, 24, 36, 50, 94, 81, 95, 66, 82, 7, 16, 11, 2, 10, 6, 35, 49, 79, 92, 65, 93, 80, 23, 34, 15, 47, 33, 64, 48, 22, 62, 45, 32, 91, 78, 63, 46, 9, 21, 31, 14, 43, 59, 30, 77, 61, 44, 89, 76, 90, 60, 20, 29, 3, 5, 28, 74, 88, 58, 75, 42, 19, 27, 41, 72, 57, 73, 13, 18, 26, 71, 87, 56, 40, 8], + [1, 4, 12, 38, 98, 84, 68, 53, 25, 85, 99, 69, 54, 39, 55, 100, 86, 70, 17, 36, 50, 94, 81, 95, 66, 82, 24, 67, 96, 83, 97, 51, 37, 52, 7, 16, 11, 2, 10, 6, 34, 23, 35, 49, 79, 92, 65, 93, 80, 15, 62, 45, 32, 91, 78, 63, 46, 22, 47, 33, 64, 48, 9, 31, 21, 14, 29, 20, 43, 59, 30, 77, 61, 44, 89, 76, 90, 60, 3, 5, 27, 41, 72, 57, 73, 19, 28, 74, 88, 58, 75, 42, 13, 26, 71, 87, 56, 40, 18, 8], + [1, 4, 38, 68, 98, 84, 53, 25, 69, 85, 99, 54, 39, 55, 70, 100, 86, 17, 36, 50, 82, 66, 94, 81, 95, 24, 96, 83, 97, 67, 51, 37, 52, 12, 7, 11, 16, 2, 10, 6, 31, 21, 14, 29, 20, 43, 59, 30, 61, 77, 44, 60, 89, 76, 90, 9, 34, 23, 35, 49, 93, 80, 65, 79, 92, 15, 62, 45, 32, 63, 91, 78, 46, 22, 47, 33, 64, 48, 3, 5, 8, 27, 41, 73, 57, 72, 19, 28, 75, 58, 74, 88, 42, 13, 26, 56, 71, 87, 40, 18], + [1, 4, 69, 85, 99, 54, 39, 55, 70, 100, 86, 25, 38, 68, 98, 84, 53, 17, 96, 83, 97, 67, 51, 37, 52, 24, 36, 50, 82, 66, 94, 81, 95, 12, 7, 11, 16, 2, 10, 6, 21, 31, 14, 43, 59, 30, 61, 77, 44, 60, 89, 76, 90, 20, 29, 9, 35, 49, 93, 80, 65, 79, 92, 23, 34, 15, 47, 33, 64, 48, 22, 62, 45, 32, 63, 91, 78, 46, 3, 5, 8, 28, 75, 58, 74, 88, 42, 19, 27, 41, 73, 57, 72, 13, 18, 26, 56, 71, 87, 40], + [40, 87, 71, 56, 26, 18, 13, 72, 57, 73, 41, 27, 19, 42, 88, 74, 58, 75, 28, 8, 5, 3, 46, 78, 91, 63, 32, 45, 62, 22, 48, 64, 33, 47, 15, 34, 23, 92, 79, 65, 80, 93, 49, 35, 9, 29, 20, 90, 76, 89, 60, 44, 77, 61, 30, 59, 43, 14, 31, 21, 6, 10, 2, 16, 11, 7, 12, 95, 81, 94, 66, 82, 50, 36, 24, 52, 37, 51, 67, 97, 83, 96, 17, 53, 84, 98, 68, 38, 25, 86, 100, 70, 55, 39, 54, 99, 85, 69, 4, 1], + [8, 40, 56, 87, 71, 26, 18, 13, 73, 57, 72, 41, 27, 19, 42, 75, 58, 88, 74, 28, 5, 3, 29, 20, 60, 90, 76, 89, 44, 61, 77, 30, 59, 43, 14, 31, 21, 9, 46, 63, 78, 91, 32, 45, 62, 22, 48, 64, 33, 47, 15, 34, 23, 80, 93, 65, 92, 79, 49, 35, 6, 10, 2, 11, 16, 7, 82, 66, 95, 81, 94, 50, 36, 24, 52, 37, 51, 97, 83, 96, 67, 17, 53, 68, 84, 98, 38, 25, 70, 86, 100, 55, 39, 54, 69, 99, 85, 12, 4, 1], + [8, 73, 57, 72, 41, 27, 19, 42, 75, 58, 88, 74, 28, 13, 40, 56, 87, 71, 26, 18, 5, 3, 31, 21, 14, 29, 20, 60, 90, 76, 89, 44, 61, 77, 30, 59, 43, 9, 34, 23, 80, 93, 65, 92, 79, 49, 35, 15, 46, 63, 78, 91, 32, 45, 62, 22, 48, 64, 33, 47, 6, 10, 2, 11, 16, 7, 53, 68, 84, 98, 38, 25, 70, 86, 100, 55, 39, 54, 69, 99, 85, 17, 82, 66, 95, 81, 94, 50, 36, 24, 52, 37, 51, 97, 83, 96, 67, 12, 4, 1], + [36, 95, 81, 94, 66, 82, 50, 24, 51, 67, 97, 83, 96, 37, 52, 17, 38, 53, 84, 98, 68, 25, 54, 99, 85, 69, 39, 86, 100, 70, 55, 12, 7, 11, 16, 4, 2, 5, 8, 26, 40, 87, 71, 56, 18, 13, 27, 72, 57, 73, 41, 19, 28, 42, 88, 74, 58, 75, 3, 10, 6, 29, 20, 59, 43, 30, 90, 76, 89, 60, 44, 77, 61, 14, 31, 21, 9, 45, 62, 32, 46, 78, 91, 63, 22, 47, 33, 48, 64, 15, 34, 23, 35, 92, 79, 65, 80, 93, 49, 1], + [95, 81, 94, 66, 82, 50, 36, 24, 52, 37, 51, 67, 97, 83, 96, 17, 53, 84, 98, 68, 38, 25, 86, 100, 70, 55, 39, 54, 99, 85, 69, 12, 7, 11, 16, 4, 2, 5, 8, 40, 87, 71, 56, 26, 18, 13, 72, 57, 73, 41, 27, 19, 42, 88, 74, 58, 75, 28, 3, 10, 6, 29, 20, 90, 76, 89, 60, 44, 77, 61, 30, 59, 43, 14, 31, 21, 9, 46, 78, 91, 63, 32, 45, 62, 22, 48, 64, 33, 47, 15, 34, 23, 92, 79, 65, 80, 93, 49, 35, 1], + [11, 16, 7, 36, 95, 81, 94, 66, 82, 50, 24, 51, 67, 97, 83, 96, 37, 52, 17, 38, 53, 84, 98, 68, 25, 54, 99, 85, 69, 39, 86, 100, 70, 55, 12, 4, 2, 8, 26, 40, 87, 71, 56, 18, 13, 27, 72, 57, 73, 41, 19, 28, 42, 88, 74, 58, 75, 5, 3, 29, 20, 59, 43, 30, 90, 76, 89, 60, 44, 77, 61, 14, 31, 21, 9, 45, 62, 32, 46, 78, 91, 63, 22, 47, 33, 48, 64, 15, 34, 23, 35, 92, 79, 65, 80, 93, 49, 6, 10, 1], + [11, 16, 7, 38, 53, 84, 98, 68, 25, 54, 99, 85, 69, 39, 86, 100, 70, 55, 17, 36, 95, 81, 94, 66, 82, 50, 24, 51, 67, 97, 83, 96, 37, 52, 12, 4, 2, 8, 27, 72, 57, 73, 41, 19, 28, 42, 88, 74, 58, 75, 13, 26, 40, 87, 71, 56, 18, 5, 3, 31, 21, 14, 29, 20, 59, 43, 30, 90, 76, 89, 60, 44, 77, 61, 9, 34, 23, 35, 92, 79, 65, 80, 93, 49, 15, 45, 62, 32, 46, 78, 91, 63, 22, 47, 33, 48, 64, 6, 10, 1], + [11, 16, 7, 53, 84, 98, 68, 38, 25, 86, 100, 70, 55, 39, 54, 99, 85, 69, 17, 95, 81, 94, 66, 82, 50, 36, 24, 52, 37, 51, 67, 97, 83, 96, 12, 4, 2, 8, 72, 57, 73, 41, 27, 19, 42, 88, 74, 58, 75, 28, 13, 40, 87, 71, 56, 26, 18, 5, 3, 31, 21, 14, 29, 20, 90, 76, 89, 60, 44, 77, 61, 30, 59, 43, 9, 34, 23, 92, 79, 65, 80, 93, 49, 35, 15, 46, 78, 91, 63, 32, 45, 62, 22, 48, 64, 33, 47, 6, 10, 1], + [11, 16, 7, 53, 84, 98, 68, 38, 25, 86, 100, 70, 55, 39, 54, 99, 85, 69, 17, 95, 81, 94, 66, 82, 50, 36, 24, 52, 37, 51, 67, 97, 83, 96, 12, 4, 2, 8, 72, 57, 73, 41, 27, 19, 42, 88, 74, 58, 75, 28, 13, 40, 87, 71, 56, 26, 18, 5, 3, 31, 21, 14, 29, 20, 90, 76, 89, 60, 44, 77, 61, 30, 59, 43, 9, 34, 23, 92, 79, 65, 80, 93, 49, 35, 15, 46, 78, 91, 63, 32, 45, 62, 22, 48, 64, 33, 47, 6, 10, 1], + [11, 16, 7, 53, 68, 84, 98, 38, 25, 70, 86, 100, 55, 39, 54, 69, 99, 85, 17, 82, 66, 95, 81, 94, 50, 36, 24, 52, 37, 51, 97, 83, 96, 67, 12, 4, 2, 8, 73, 57, 72, 41, 27, 19, 42, 75, 58, 88, 74, 28, 13, 40, 56, 87, 71, 26, 18, 5, 3, 31, 21, 14, 29, 20, 60, 90, 76, 89, 44, 61, 77, 30, 59, 43, 9, 34, 23, 80, 93, 65, 92, 79, 49, 35, 15, 46, 63, 78, 91, 32, 45, 62, 22, 48, 64, 33, 47, 6, 10, 1], + [11, 16, 7, 82, 66, 95, 81, 94, 50, 36, 24, 52, 37, 51, 97, 83, 96, 67, 17, 53, 68, 84, 98, 38, 25, 70, 86, 100, 55, 39, 54, 69, 99, 85, 12, 4, 2, 8, 40, 56, 87, 71, 26, 18, 13, 73, 57, 72, 41, 27, 19, 42, 75, 58, 88, 74, 28, 5, 3, 29, 20, 60, 90, 76, 89, 44, 61, 77, 30, 59, 43, 14, 31, 21, 9, 46, 63, 78, 91, 32, 45, 62, 22, 48, 64, 33, 47, 15, 34, 23, 80, 93, 65, 92, 79, 49, 35, 6, 10, 1], + [11, 16, 7, 82, 66, 95, 81, 94, 50, 36, 24, 52, 37, 51, 97, 83, 96, 67, 17, 53, 68, 84, 98, 38, 25, 70, 86, 100, 55, 39, 54, 69, 99, 85, 12, 4, 2, 8, 40, 56, 87, 71, 26, 18, 13, 73, 57, 72, 41, 27, 19, 42, 75, 58, 88, 74, 28, 5, 3, 29, 20, 60, 90, 76, 89, 44, 61, 77, 30, 59, 43, 14, 31, 21, 9, 46, 63, 78, 91, 32, 45, 62, 22, 48, 64, 33, 47, 15, 34, 23, 80, 93, 65, 92, 79, 49, 35, 6, 10, 1], + [11, 16, 7, 82, 66, 94, 81, 95, 50, 36, 24, 52, 37, 51, 96, 83, 97, 67, 17, 53, 68, 98, 84, 38, 25, 70, 100, 86, 55, 39, 54, 69, 85, 99, 12, 4, 2, 8, 40, 56, 71, 87, 26, 18, 13, 73, 57, 72, 41, 27, 19, 42, 75, 58, 74, 88, 28, 5, 3, 29, 20, 60, 89, 76, 90, 44, 61, 77, 30, 59, 43, 14, 31, 21, 9, 46, 63, 91, 78, 32, 45, 62, 22, 48, 64, 33, 47, 15, 34, 23, 93, 80, 65, 79, 92, 49, 35, 6, 10, 1], + [11, 16, 7, 82, 66, 94, 81, 95, 50, 36, 24, 52, 37, 51, 96, 83, 97, 67, 17, 53, 68, 98, 84, 38, 25, 70, 100, 86, 55, 39, 54, 69, 85, 99, 12, 4, 2, 8, 40, 56, 71, 87, 26, 18, 13, 73, 57, 72, 41, 27, 19, 42, 75, 58, 74, 88, 28, 5, 3, 29, 20, 60, 89, 76, 90, 44, 61, 77, 30, 59, 43, 14, 31, 21, 9, 46, 63, 91, 78, 32, 45, 62, 22, 48, 64, 33, 47, 15, 34, 23, 93, 80, 65, 79, 92, 49, 35, 6, 10, 1], + [11, 16, 7, 36, 82, 66, 94, 81, 95, 50, 24, 51, 96, 83, 97, 67, 37, 52, 17, 38, 53, 68, 98, 84, 25, 54, 69, 85, 99, 39, 70, 100, 86, 55, 12, 4, 2, 8, 26, 40, 56, 71, 87, 18, 13, 27, 73, 57, 72, 41, 19, 28, 42, 75, 58, 74, 88, 5, 3, 29, 20, 59, 43, 30, 60, 89, 76, 90, 44, 61, 77, 14, 31, 21, 9, 45, 62, 32, 46, 63, 91, 78, 22, 47, 33, 48, 64, 15, 34, 23, 35, 93, 80, 65, 79, 92, 49, 6, 10, 1], + [1, 10, 6, 49, 92, 79, 65, 80, 93, 35, 23, 34, 15, 64, 48, 33, 47, 22, 78, 91, 63, 46, 32, 62, 45, 9, 21, 31, 14, 77, 61, 44, 90, 76, 89, 60, 30, 43, 59, 20, 29, 3, 5, 88, 74, 58, 75, 42, 28, 19, 41, 72, 57, 73, 27, 13, 18, 87, 71, 56, 40, 26, 8, 2, 4, 12, 55, 86, 100, 70, 39, 99, 85, 69, 54, 25, 84, 98, 68, 53, 38, 17, 52, 37, 67, 97, 83, 96, 51, 24, 50, 95, 81, 94, 66, 82, 36, 7, 16, 11], + [1, 10, 6, 34, 23, 49, 92, 79, 65, 80, 93, 35, 15, 78, 91, 63, 46, 32, 62, 45, 22, 64, 48, 33, 47, 9, 31, 21, 14, 29, 20, 77, 61, 44, 90, 76, 89, 60, 30, 43, 59, 3, 5, 41, 72, 57, 73, 27, 19, 88, 74, 58, 75, 42, 28, 13, 87, 71, 56, 40, 26, 18, 8, 2, 4, 12, 84, 98, 68, 53, 38, 25, 55, 86, 100, 70, 39, 99, 85, 69, 54, 17, 50, 95, 81, 94, 66, 82, 36, 24, 52, 37, 67, 97, 83, 96, 51, 7, 16, 11], + [1, 10, 6, 34, 23, 92, 79, 65, 80, 93, 49, 35, 15, 46, 78, 91, 63, 32, 45, 62, 22, 48, 64, 33, 47, 9, 31, 21, 14, 29, 20, 90, 76, 89, 60, 44, 77, 61, 30, 59, 43, 3, 5, 72, 57, 73, 41, 27, 19, 42, 88, 74, 58, 75, 28, 13, 40, 87, 71, 56, 26, 18, 8, 2, 4, 12, 53, 84, 98, 68, 38, 25, 86, 100, 70, 55, 39, 54, 99, 85, 69, 17, 95, 81, 94, 66, 82, 50, 36, 24, 52, 37, 51, 67, 97, 83, 96, 7, 16, 11], + [1, 5, 87, 71, 56, 40, 26, 18, 13, 41, 72, 57, 73, 27, 19, 88, 74, 58, 75, 42, 28, 8, 3, 10, 6, 78, 91, 63, 46, 32, 62, 45, 22, 64, 48, 33, 47, 15, 34, 23, 49, 92, 79, 65, 80, 93, 35, 9, 29, 20, 77, 61, 44, 90, 76, 89, 60, 30, 43, 59, 14, 31, 21, 2, 12, 50, 95, 81, 94, 66, 82, 36, 24, 52, 37, 67, 97, 83, 96, 51, 17, 84, 98, 68, 53, 38, 25, 55, 86, 100, 70, 39, 99, 85, 69, 54, 7, 16, 11, 4], + [1, 5, 26, 87, 71, 56, 40, 18, 13, 27, 41, 72, 57, 73, 19, 28, 88, 74, 58, 75, 42, 8, 3, 10, 6, 62, 45, 32, 78, 91, 63, 46, 22, 47, 33, 64, 48, 15, 34, 23, 35, 49, 92, 79, 65, 80, 93, 9, 29, 20, 43, 59, 30, 77, 61, 44, 90, 76, 89, 60, 14, 31, 21, 2, 12, 36, 50, 95, 81, 94, 66, 82, 24, 67, 97, 83, 96, 51, 37, 52, 17, 38, 84, 98, 68, 53, 25, 99, 85, 69, 54, 39, 55, 86, 100, 70, 7, 16, 11, 4], + [1, 5, 18, 26, 87, 71, 56, 40, 13, 28, 88, 74, 58, 75, 42, 19, 27, 41, 72, 57, 73, 8, 3, 10, 6, 47, 33, 64, 48, 22, 62, 45, 32, 78, 91, 63, 46, 15, 35, 49, 92, 79, 65, 80, 93, 23, 34, 9, 43, 59, 30, 77, 61, 44, 90, 76, 89, 60, 20, 29, 14, 21, 31, 2, 12, 67, 97, 83, 96, 51, 37, 52, 24, 36, 50, 95, 81, 94, 66, 82, 17, 99, 85, 69, 54, 39, 55, 86, 100, 70, 25, 38, 84, 98, 68, 53, 7, 16, 11, 4], + [1, 16, 11, 7, 12, 55, 70, 86, 100, 39, 69, 99, 85, 54, 25, 68, 84, 98, 53, 38, 17, 52, 37, 97, 83, 96, 67, 51, 24, 50, 82, 66, 95, 81, 94, 36, 4, 2, 75, 58, 88, 74, 42, 28, 19, 41, 73, 57, 72, 27, 13, 18, 56, 87, 71, 40, 26, 8, 5, 3, 49, 80, 93, 65, 92, 79, 35, 23, 34, 15, 64, 48, 33, 47, 22, 63, 78, 91, 46, 32, 62, 45, 9, 21, 31, 14, 61, 77, 44, 60, 90, 76, 89, 30, 43, 59, 20, 29, 6, 10], + [1, 16, 11, 7, 12, 68, 84, 98, 53, 38, 25, 55, 70, 86, 100, 39, 69, 99, 85, 54, 17, 50, 82, 66, 95, 81, 94, 36, 24, 52, 37, 97, 83, 96, 67, 51, 4, 2, 41, 73, 57, 72, 27, 19, 75, 58, 88, 74, 42, 28, 13, 56, 87, 71, 40, 26, 18, 8, 5, 3, 34, 23, 49, 80, 93, 65, 92, 79, 35, 15, 63, 78, 91, 46, 32, 62, 45, 22, 64, 48, 33, 47, 9, 31, 21, 14, 29, 20, 61, 77, 44, 60, 90, 76, 89, 30, 43, 59, 6, 10], + [1, 16, 11, 7, 12, 38, 68, 84, 98, 53, 25, 69, 99, 85, 54, 39, 55, 70, 86, 100, 17, 36, 50, 82, 66, 95, 81, 94, 24, 97, 83, 96, 67, 51, 37, 52, 4, 2, 27, 41, 73, 57, 72, 19, 28, 75, 58, 88, 74, 42, 13, 26, 56, 87, 71, 40, 18, 8, 5, 3, 34, 23, 35, 49, 80, 93, 65, 92, 79, 15, 62, 45, 32, 63, 78, 91, 46, 22, 47, 33, 64, 48, 9, 31, 21, 14, 29, 20, 43, 59, 30, 61, 77, 44, 60, 90, 76, 89, 6, 10], + [1, 4, 16, 11, 7, 12, 36, 82, 66, 95, 81, 94, 50, 24, 51, 97, 83, 96, 67, 37, 52, 17, 38, 53, 68, 84, 98, 25, 54, 69, 99, 85, 39, 70, 86, 100, 55, 2, 45, 62, 32, 46, 63, 78, 91, 22, 47, 33, 48, 64, 15, 34, 23, 35, 80, 93, 65, 92, 79, 49, 9, 29, 20, 59, 43, 30, 60, 90, 76, 89, 44, 61, 77, 14, 31, 21, 6, 10, 3, 26, 40, 56, 87, 71, 18, 13, 27, 73, 57, 72, 41, 19, 28, 42, 75, 58, 88, 74, 8, 5], + [1, 4, 16, 11, 7, 12, 82, 66, 95, 81, 94, 50, 36, 24, 52, 37, 51, 97, 83, 96, 67, 17, 53, 68, 84, 98, 38, 25, 70, 86, 100, 55, 39, 54, 69, 99, 85, 2, 46, 63, 78, 91, 32, 45, 62, 22, 48, 64, 33, 47, 15, 34, 23, 80, 93, 65, 92, 79, 49, 35, 9, 29, 20, 60, 90, 76, 89, 44, 61, 77, 30, 59, 43, 14, 31, 21, 6, 10, 3, 40, 56, 87, 71, 26, 18, 13, 73, 57, 72, 41, 27, 19, 42, 75, 58, 88, 74, 28, 8, 5], + [1, 4, 16, 11, 7, 12, 50, 82, 66, 95, 81, 94, 36, 24, 52, 37, 97, 83, 96, 67, 51, 17, 68, 84, 98, 53, 38, 25, 55, 70, 86, 100, 39, 69, 99, 85, 54, 2, 63, 78, 91, 46, 32, 62, 45, 22, 64, 48, 33, 47, 15, 34, 23, 49, 80, 93, 65, 92, 79, 35, 9, 29, 20, 61, 77, 44, 60, 90, 76, 89, 30, 43, 59, 14, 31, 21, 6, 10, 3, 56, 87, 71, 40, 26, 18, 13, 41, 73, 57, 72, 27, 19, 75, 58, 88, 74, 42, 28, 8, 5], + [1, 4, 16, 11, 7, 12, 52, 37, 97, 83, 96, 67, 51, 24, 50, 82, 66, 95, 81, 94, 36, 17, 55, 70, 86, 100, 39, 69, 99, 85, 54, 25, 68, 84, 98, 53, 38, 2, 64, 48, 33, 47, 22, 63, 78, 91, 46, 32, 62, 45, 15, 49, 80, 93, 65, 92, 79, 35, 23, 34, 9, 61, 77, 44, 60, 90, 76, 89, 30, 43, 59, 20, 29, 14, 21, 31, 6, 10, 3, 18, 56, 87, 71, 40, 26, 13, 75, 58, 88, 74, 42, 28, 19, 41, 73, 57, 72, 27, 8, 5], + [1, 4, 16, 11, 7, 12, 52, 37, 51, 97, 83, 96, 67, 24, 82, 66, 95, 81, 94, 50, 36, 17, 70, 86, 100, 55, 39, 54, 69, 99, 85, 25, 53, 68, 84, 98, 38, 2, 48, 64, 33, 47, 22, 46, 63, 78, 91, 32, 45, 62, 15, 80, 93, 65, 92, 79, 49, 35, 23, 34, 9, 60, 90, 76, 89, 44, 61, 77, 30, 59, 43, 20, 29, 14, 21, 31, 6, 10, 3, 18, 40, 56, 87, 71, 26, 13, 42, 75, 58, 88, 74, 28, 19, 73, 57, 72, 41, 27, 8, 5], + [1, 4, 16, 11, 7, 12, 52, 37, 97, 83, 96, 67, 51, 24, 50, 82, 66, 95, 81, 94, 36, 17, 55, 70, 86, 100, 39, 69, 99, 85, 54, 25, 68, 84, 98, 53, 38, 2, 64, 48, 33, 47, 22, 63, 78, 91, 46, 32, 62, 45, 15, 49, 80, 93, 65, 92, 79, 35, 23, 34, 9, 61, 77, 44, 60, 90, 76, 89, 30, 43, 59, 20, 29, 14, 21, 31, 6, 10, 3, 18, 56, 87, 71, 40, 26, 13, 75, 58, 88, 74, 42, 28, 19, 41, 73, 57, 72, 27, 8, 5], + [1, 4, 16, 11, 7, 12, 97, 83, 96, 67, 51, 37, 52, 24, 36, 50, 82, 66, 95, 81, 94, 17, 69, 99, 85, 54, 39, 55, 70, 86, 100, 25, 38, 68, 84, 98, 53, 2, 47, 33, 64, 48, 22, 62, 45, 32, 63, 78, 91, 46, 15, 35, 49, 80, 93, 65, 92, 79, 23, 34, 9, 43, 59, 30, 61, 77, 44, 60, 90, 76, 89, 20, 29, 14, 21, 31, 6, 10, 3, 18, 26, 56, 87, 71, 40, 13, 28, 75, 58, 88, 74, 42, 19, 27, 41, 73, 57, 72, 8, 5], + [1, 4, 16, 11, 7, 12, 96, 83, 97, 67, 51, 37, 52, 24, 36, 50, 82, 66, 94, 81, 95, 17, 69, 85, 99, 54, 39, 55, 70, 100, 86, 25, 38, 68, 98, 84, 53, 2, 47, 33, 64, 48, 22, 62, 45, 32, 63, 91, 78, 46, 15, 35, 49, 93, 80, 65, 79, 92, 23, 34, 9, 43, 59, 30, 61, 77, 44, 60, 89, 76, 90, 20, 29, 14, 21, 31, 6, 10, 3, 18, 26, 56, 71, 87, 40, 13, 28, 75, 58, 74, 88, 42, 19, 27, 41, 73, 57, 72, 8, 5], + [1, 4, 16, 11, 7, 12, 67, 96, 83, 97, 51, 37, 52, 24, 36, 50, 94, 81, 95, 66, 82, 17, 85, 99, 69, 54, 39, 55, 100, 86, 70, 25, 38, 98, 84, 68, 53, 2, 47, 33, 64, 48, 22, 62, 45, 32, 91, 78, 63, 46, 15, 35, 49, 79, 92, 65, 93, 80, 23, 34, 9, 43, 59, 30, 77, 61, 44, 89, 76, 90, 60, 20, 29, 14, 21, 31, 6, 10, 3, 18, 26, 71, 87, 56, 40, 13, 28, 74, 88, 58, 75, 42, 19, 27, 41, 72, 57, 73, 8, 5], + [1, 4, 12, 52, 37, 67, 96, 83, 97, 51, 24, 50, 94, 81, 95, 66, 82, 36, 17, 55, 100, 86, 70, 39, 85, 99, 69, 54, 25, 98, 84, 68, 53, 38, 7, 16, 11, 2, 10, 6, 64, 48, 33, 47, 22, 91, 78, 63, 46, 32, 62, 45, 15, 49, 79, 92, 65, 93, 80, 35, 23, 34, 9, 77, 61, 44, 89, 76, 90, 60, 30, 43, 59, 20, 29, 14, 21, 31, 3, 5, 18, 71, 87, 56, 40, 26, 13, 74, 88, 58, 75, 42, 28, 19, 41, 72, 57, 73, 27, 8], + [1, 4, 12, 52, 37, 67, 96, 83, 97, 51, 24, 50, 94, 81, 95, 66, 82, 36, 17, 55, 100, 86, 70, 39, 85, 99, 69, 54, 25, 98, 84, 68, 53, 38, 7, 16, 11, 2, 10, 6, 64, 48, 33, 47, 22, 91, 78, 63, 46, 32, 62, 45, 15, 49, 79, 92, 65, 93, 80, 35, 23, 34, 9, 77, 61, 44, 89, 76, 90, 60, 30, 43, 59, 20, 29, 14, 21, 31, 3, 5, 18, 71, 87, 56, 40, 26, 13, 74, 88, 58, 75, 42, 28, 19, 41, 72, 57, 73, 27, 8], + [1, 4, 16, 11, 7, 12, 67, 96, 83, 97, 51, 37, 52, 24, 36, 50, 94, 81, 95, 66, 82, 17, 85, 99, 69, 54, 39, 55, 100, 86, 70, 25, 38, 98, 84, 68, 53, 2, 47, 33, 64, 48, 22, 62, 45, 32, 91, 78, 63, 46, 15, 35, 49, 79, 92, 65, 93, 80, 23, 34, 9, 43, 59, 30, 77, 61, 44, 89, 76, 90, 60, 20, 29, 14, 21, 31, 6, 10, 3, 18, 26, 71, 87, 56, 40, 13, 28, 74, 88, 58, 75, 42, 19, 27, 41, 72, 57, 73, 8, 5] + ] + } +] diff --git a/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.py b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.py index 3a611efe..272c2c91 100644 --- a/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.py +++ b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.py @@ -67,21 +67,23 @@ def traverse_in_order_collector( def build_tree(indexes: List[List[int]]) -> Node: + indexes_copy = indexes[:] root: Node = Node(__ROOT_VALUE__) - node_collector: Dict[int, list[Node]] = {} + node_collector: Dict[int, list[Node]] - while len(indexes) > 0: + while len(indexes_copy) > 0: + node_collector = {} traverse_in_order_collector( root, node_collector, __INITIAL_LEVEL__, callback_collect_nodes) - last_level: int = list(node_collector)[-1] + last_level: int = sorted(list(node_collector))[-1] - for i in range(0, min(len(indexes), len(node_collector[last_level]))): + for i in range(0, min(len(indexes_copy), len(node_collector[last_level]))): current_node: Node = node_collector[last_level][i] - new_element: List[int] = indexes.pop(0) + new_element: List[int] = indexes_copy.pop(0) if new_element[0] != -1: current_node.left = Node(new_element[0]) @@ -91,7 +93,7 @@ def build_tree(indexes: List[List[int]]) -> Node: return root -def plain_tree(root: Node) -> List[int]: +def flatten_tree(root: Node) -> List[int]: node_collector: Dict[int, list[Node]] = {} node_collector = traverse_in_order_collector( @@ -136,7 +138,7 @@ def swap_nodes(indexes: List[List[int]], queries: List[int]) -> List[List[int]]: node_collector = dict(sorted(node_collector.items())) - plain = plain_tree(tree) # original + plain = flatten_tree(tree) # original LOGGER.debug('Plain tree: %s', plain) @@ -146,7 +148,7 @@ def swap_nodes(indexes: List[List[int]], queries: List[int]) -> List[List[int]]: for node in node_list: swap_branch(node) - plain = plain_tree(tree) + plain = flatten_tree(tree) output.append(plain) return output diff --git a/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.testcases.json b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.testcases.json index ccb283d0..dc1c8751 100644 --- a/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.testcases.json +++ b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.testcases.json @@ -2,21 +2,24 @@ { "title": "Sample 0", "nodes": [[2, 3], [-1, -1], [-1, -1]], + "flattened": [2, 1, 3], "queries": [1, 1], - "answer": [[3, 1, 2], [2, 1, 3]] + "expected": [[3, 1, 2], [2, 1, 3]] }, { "title": "Sample 1", "nodes": [[2, 3], [-1, 4], [-1, 5], [-1, -1], [-1, -1]], + "flattened": [2, 4, 1, 3, 5], "queries": [2], - "answer": [[4, 2, 1, 5, 3]] + "expected": [[4, 2, 1, 5, 3]] }, { "title": "Sample 2", "nodes": [[2, 3], [4, -1], [5, -1], [6, -1], [7, 8], [-1, 9], [-1, -1], [10, 11], [-1, -1], [-1, -1], [-1, -1]], + "flattened": [6, 9, 4, 2, 1, 7, 5, 10, 8, 11, 3], "queries": [2, 4], - "answer": [[2, 9, 6, 4, 1, 3, 7, 5, 11, 8, 10], + "expected": [[2, 9, 6, 4, 1, 3, 7, 5, 11, 8, 10], [2, 6, 9, 4, 1, 3, 7, 5, 10, 8, 11]] }, { @@ -24,8 +27,9 @@ "nodes": [[2, 3], [4, 5], [6, -1], [-1, 7], [8, 9], [10, 11], [12, 13], [-1, 14], [-1, -1], [15, -1], [16, 17], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1]], + "flattened": [4, 12, 7, 13, 2, 8, 14, 5, 9, 1, 15, 10, 6, 16, 11, 17, 3], "queries": [2, 3], - "answer": [[14, 8, 5, 9, 2, 4, 13, 7, 12, 1, 3, 10, 15, 6, 17, 11, 16], + "expected": [[14, 8, 5, 9, 2, 4, 13, 7, 12, 1, 3, 10, 15, 6, 17, 11, 16], [9, 5, 14, 8, 2, 13, 7, 12, 4, 1, 3, 17, 11, 16, 6, 10, 15]] } ] diff --git a/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo_big_test.py b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo_big_test.py new file mode 100644 index 00000000..184ac6f3 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo_big_test.py @@ -0,0 +1,28 @@ +import unittest +from pathlib import Path + +from ....hackerrank.lib.loader import load_test_cases +from .swap_nodes_algo import swap_nodes, build_tree, flatten_tree + +FILE_PATH = str(Path(__file__).resolve().parent) +BIG_TEST_CASES = load_test_cases(FILE_PATH + '/swap_nodes_algo.big.testcases.json') + + +class TestSwapNodesAlgoBig(unittest.TestCase): + def test_build_tree_and_flatten_big(self): + + for _, _tt in enumerate(BIG_TEST_CASES): + t_result: list[int] = flatten_tree(build_tree(_tt['nodes'])) + + self.assertEqual( + t_result, _tt['flattened'], + f"{_} | flatten_tree({_tt['nodes']}) must be {_tt['expected']}") + + def test_swap_nodes_big(self): + + for _, _tt in enumerate(BIG_TEST_CASES): + + self.assertEqual( + swap_nodes(_tt['nodes'], _tt['queries']), _tt['expected'], + f"{_} | swap_nodes({_tt['nodes'], _tt['queries']}) must be " + f"=> {_tt['expected']}") diff --git a/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo_test.py b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo_test.py index 4467e520..f4544eee 100644 --- a/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo_test.py +++ b/src/hackerrank/interview_preparation_kit/search/swap_nodes_algo_test.py @@ -2,46 +2,55 @@ from pathlib import Path from ....hackerrank.lib.loader import load_test_cases -from .swap_nodes_algo import swap_nodes, build_tree, plain_tree, swap_branch +from .swap_nodes_algo import swap_nodes, build_tree, flatten_tree, swap_branch FILE_PATH = str(Path(__file__).resolve().parent) TEST_CASES = load_test_cases(FILE_PATH + '/swap_nodes_algo.testcases.json') class TestSwapNodesAlgo(unittest.TestCase): - def test_swap_nodes(self): for _, _tt in enumerate(TEST_CASES): self.assertEqual( - swap_nodes(_tt['nodes'], _tt['queries']), _tt['answer'], + swap_nodes(_tt['nodes'], _tt['queries']), _tt['expected'], f"{_} | swap_nodes({_tt['nodes'], _tt['queries']}) must be " - f"=> {_tt['answer']}") + f"=> {_tt['expected']}") def test_swap_branch(self): t_input = None t_result = swap_branch(t_input) - answer = None + expected = None self.assertEqual( t_result, - answer, + expected, f"swap_branch({t_input}) must be " - f"=> {answer}" + f"=> {expected}" ) + def test_build_tree_and_plain(self): + + for _, _tt in enumerate(TEST_CASES): + + t_result: list[int] = flatten_tree(build_tree(_tt['nodes'])) + + self.assertEqual( + t_result, _tt['flattened'], + f"{_} | flatten_tree({_tt['nodes']}) must be {_tt['expected']}") + def test_build_tree_empty(self): t_input = [] t_to_test = build_tree(t_input) - t_result = plain_tree(t_to_test) - answer = [1] + t_result = flatten_tree(t_to_test) + expected = [1] self.assertEqual( t_result, - answer, + expected, f"build_tree({t_input}) must be " - f"=> {answer}" + f"=> {expected}" )