Artemis: Refactor: Improve performance and readability of array and math functions #57
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This commit refactors array and math functions for improved performance and readability.
Specific changes include:
sumSquare(int n): Replaced nested loop and conditional with direct calculation ofi * i.sumTriangle(int n): Eliminated nested loop by using the formula for the nth triangular number (n*(n+1)/2). Loop now starts from 1 and correctly calculates triangular numbers T(1) to T(n) usingi * (i - 1) / 2.countPairs(int[] arr): Rewritten to use aHashMapto count occurrences, reducing time complexity from O(n^2) to O(n), with the additional calculation of dividing number of pairs by 2 to remove duplicates.countDuplicates(int[] arr0, int[] arr1): Replaced nested loop with a single loop, iterating only over the shared index range of the two arrays to avoidIndexOutOfBoundsException.These changes enhance efficiency and code clarity for common math and array operations.