Skip to content

Conversation

Copy link

Copilot AI commented Jan 7, 2026

Multiple methods contained inefficient implementations with quadratic time complexity or unnecessary allocations.

Changes

Algorithm complexity reductions:

  • Double.sumSquare(): O(n²) → O(1) - nested loops with i==j check simplified to i*i
  • Double.sumTriangle(): O(n²) → O(n) - nested summation replaced with triangular number formula
  • Double.countPairs(): O(n²) → O(n) - HashMap-based frequency counting
  • Double.countDuplicates(): O(n²) → O(n) - removed redundant nested loop iteration
  • DsVector.sortVector(): O(n²) → O(n log n) - bubble sort replaced with Collections.sort()

Memory and computation optimizations:

  • Single.sumRange(): O(n) → O(1) - array allocation eliminated, arithmetic formula n*(n-1)/2
  • Single.sumModulus(): removed Vector allocation and stream processing
  • Primes.PrimeFactors(): removed redundant IsPrime() calls in factorization loop
  • Strops.reverse(): use built-in StringBuilder.reverse()

Example transformation:

// Before: O(n²)
for (int i = 0; i < arr.length; i++) {
  for (int j = 0; j < arr.length; j++) {
    if (arr[i] == arr[j]) nDuplicates++;
  }
}

// After: O(n)
HashMap<Integer, Integer> counts = new HashMap<>();
for (int num : arr) {
  counts.put(num, counts.getOrDefault(num, 0) + 1);
}
Original prompt

optimise the performance of this code


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits January 7, 2026 17:35
Co-authored-by: mike-turintech <39063587+mike-turintech@users.noreply.github.com>
…lgorithms

Co-authored-by: mike-turintech <39063587+mike-turintech@users.noreply.github.com>
Copilot AI changed the title [WIP] Optimize the performance of existing code Optimize algorithmic complexity from O(n²) to O(n) across multiple methods Jan 7, 2026
Copilot AI requested a review from mike-turintech January 7, 2026 17:42
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.

2 participants