Part 2 solution, slightly better than O(n2), using a copy to initialize the array, and the inner loop runs 2 fewer times than the outer.
namespacePlayground{classProgram{staticvoidMain(string[]args){varinput=newint[]{1,2,3,4,5};varoutput=ComputeProducts(input);foreach(intiinoutput){Console.Write($"{i}\t");}Console.ReadLine();}staticint[]ComputeProducts(int[]source){varcount=source.Length;// making a few assumptions for edge cases...if(count<2)returnsource;if(count==2)returnnew[]{source[1],source[0]};varresult=newint[count];// initialize the array, eliminating the need to do a "set to 1" stepArray.Copy(source,1,result,0,count-1);result[count-1]=source[0];// loop the resultfor(inti=0;i<count;i++){// we skip the current index, and the next one has already been set abovefor(intj=2;j<count;j++){result[i]*=source[(j+i)%count];}}returnresult;}}}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Part 2 solution, slightly better than O(n2), using a copy to initialize the array, and the inner loop runs 2 fewer times than the outer.