Sort by

recency

|

12 Discussions

|

  • + 0 comments

    Finally, got the right approach for optimization and solved it.

    This was one of the best problems I've come accross on HR so far, in terms of combining mathematical and programming skills, which did not require knowledge of a certain mathematical approach like game theory, graph theory etc, or it may be the case that I just re-invented them.

    1. Track the totals and product totals of certain indices (Please see item 3, we start tracking a new range only where the sign of two adjacent elements are different) of the array in two new arrays
    2. Keep a set for active ranges that we are interested in
    3. We don't have to start tracking a range for all the indices. If a[i] > 0 and a[i+1] > 0, the range starting from i will always have a greater product then the range starting at i+1 3.a This can also be prooved mathematically using just basic algebra
    4. Whenever the total of the range starting from a certain index changes sign, we can stop tracking it 4.a And this is because if the total of the sub-segment becomes ZERO at any point, any product calculations that will include this one on the left will be less than if they don't have it. 4.b This can be prooved mathematically, easily, again.
    5. The core of the optimization is to stop tracking ranges that; 5.a started from an element having the same sign (+ or -) 5.b is starting in the array later than some other range 5.c and whose current product is LESS than the current product of the other range at offset i 5.d This is because they have the same sign and if the later range has a smaller product at i, it will never have a greater product at any offset i compared to a range that started earlier.

    With all this, and with 70 lines of code, voila, I got full score.

    Very well problem definition and let me tell you, ChatGPT will never get you under O(n^2) :)

  • + 0 comments

    Easy to understand, detailed solution with step by step explanation and code for both brute force and optimized solution:

    https://www.code-recipe.com/post/two-sum

    Let me know in comments section if you have any doubts. I will be more than happy to answer.

    Please upvote if you found the solution useful. Thank You :)

  • + 0 comments

    can some one please explain the problem please i didn't understood what is mean by sub array in the problem

  • + 0 comments

    Can anybody Suggest what is the mistake in my code I belive the logic is correct :

    def subarray(A):
        start = next(x for x, val in enumerate(A) if val > 0) 
        end = next(n-x for x, val in enumerate(A[n:0:-1]) 
                                      if val > 0 )            
        largestValue(Arr=A[start:end])    
    def largestValue(Arr):         
        Sum=sum(Arr)
        result=0    
        #print(Arr,l)
        for i in range(len(Arr)-1):
            Sum-=Arr[i]
            result+=Arr[i] * (Sum)       
        print(result)    
    n = int(input())
    
    A = list(map(int, input().rstrip().split()))
    subarray(A)             
            
            
    
  • + 0 comments

    This task dose not decribe what defines what is a subarray.