Skip to content

Conversation

@mike-turintech
Copy link
Member

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 of i * 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) using i * (i - 1) / 2.
  • countPairs(int[] arr): Rewritten to use a HashMap to 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 avoid IndexOutOfBoundsException.

These changes enhance efficiency and code clarity for common math and array operations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants