Posts

Showing posts from 2025

The power and simplicity of IComparer V

Image
Easy problem but it shows the power of IComparer. There are multiple possible solutions here, hence you let the IComparer do the job by just comparing the absolute values in the (internal) QuickSort implementation. Code is down below, cheers, ACC. Sort Array By Absolute Value - LeetCode You are given an integer array  nums . Rearrange elements of  nums  in  non-decreasing  order of their absolute value. Return  any  rearranged array that satisfies this condition. Note : The absolute value of an integer x is defined as: x  if  x >= 0 -x  if  x < 0   Example 1: Input:   nums = [3,-1,-4,1,5] Output:   [-1,1,3,-4,5] Explanation: The absolute values of elements in  nums  are 3, 1, 4, 1, 5 respectively. Rearranging them in increasing order, we get 1, 1, 3, 4, 5. This corresponds to  [-1, 1, 3, -4, 5] . Another possible rearrangement is  [1, -1, 3, -4, 5]. Example 2: Input:   nums = [-100,100] ...

Sieve of Eratosthenes to solve a Leetcode problem III

Image
In this case the Sieve is applied directly to the main loop. Notice that you don't need to save off the elements in different arrays, only add them up. Also important to note that composite numbers can be created in multiple ways (for example, 2*6 == 3*4), hence keep track of which ones you have seen before to avoid double counting. No GAI involved, except at the very bottom to compute the Big-O time complexity, which I think it would have been of the order of NLogN, but the true analysis from OpenAI O3 (Advanced Reasoning) is down below. So is the code. Cheers, ACC. Split Array by Prime Indices - LeetCode You are given an integer array  nums . Split  nums  into two arrays  A  and  B  using the following rule: Elements at  prime  indices in  nums  must go into array  A . All other elements must go into array  B . Return the  absolute  difference between the sums of the two arrays:  |sum(A) - sum(B)| . Note: ...