Posts

Showing posts from November, 2025

A linear solution taking advantage of sorted indexes

 This is an interesting problem: you'll be taking advantage that the indexes in an array by definition are already sorted. Hence, although you'll be using the fact that the indexes are sorted, you won't be sorting anything leading to a linear optimized solution. The idea is to keep a list of indexes for each value in the array. The list by definition will be sorted. Then do a linear scan of each list, calculating the distance and storing the overall min one. Code is down below, cheers, ACC. Minimum Distance Between Three Equal Elements II - LeetCode ou are given an integer array  nums . A tuple  (i, j, k)  of 3  distinct  indices is  good  if  nums[i] == nums[j] == nums[k] . The  distance  of a  good  tuple is  abs(i - j) + abs(j - k) + abs(k - i) , where  abs(x)  denotes the  absolute value  of  x . Return an integer denoting the  minimum  possible  distance  of a  go...

ABCDE Problem

Problem proposed by Kamal Jain: Solution can be implemented with a simple backtracking, with a bit of pruning during the Check. Not the most efficient solution due to the string operations, but did the trick, and the answer is: 09754,14754,15078,15080,15104,20078,20080,20104 Gemini 2.5 Flash as the clear winner. Code is down below, cheers. List < string > solutions = new List < string >(); Combination ( "" , 5 , ref solutions ); for ( int i = 0 ; i < solutions . Count ; i ++)     Console . Write ( "{0}{1}" , solutions [ i ], i < solutions . Count - 1 ? ',' : ' ' ); void Combination ( string current ,                   int MAX ,                   ref List < string > retVal ) {     if ( current . Length == MAX )     {         if ( Check ( current , MAX , 20320 ))             retVal . A...