Sort by

recency

|

10 Discussions

|

  • + 1 comment

    Can anyone here help me correct this solution??

    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
        
        static long largestValue(int[] A) 
        {
            int size=A.length;
            long values;
            long sum;
            int i,j;
            values=A[0]*A[1];
            sum=A[0]+A[1];
            
            long largeValue=values;
            i=2;
            long sum1,sum2,values1,values2;
            j=0;
            while(i<size-1)
            {
                values1=values+sum*A[i];
                sum1=sum+A[i];
                sum2=sum1-A[j];
                values2=values1-A[j]*sum2;
                if(values1>values2)
                {
                    values=values1;
                    sum=sum1;
                }
                else if(values1==values2)
                {
                    if(sum1>sum2)
                    {
                        
                    values=values1;
                    sum=sum1;
                    }
                    else
                        {
                    values=values2;
                    sum=sum2;
                    j++;
                }
                        
                }
                else
                {
                    values=values2;
                    sum=sum2;
                    j++;
                }
                largeValue=largeValue>values?largeValue:values;
                i++;
            }
            return largeValue;
        }
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            int[] A = new int[n];
            for(int A_i = 0; A_i < n; A_i++){
                A[A_i] = in.nextInt();
            }
            long result = largestValue(A);
            System.out.println(result);
            in.close();
        }
    }
    
  • + 0 comments

    i had tried a dynamic programming approach with dp[n][n] and time complexity O(n^2) but this be able to pass only 7/21 test cases.As dp[100000][100000]could be a problem what could be an alternative to such a large 2D array

  • + 0 comments

    Contest is over, so I'm allowed to post code, right? Here's my partially correct solution. I would be very grateful if someone could help me with this.

    #!/bin/python3
    
    import sys
    
    def largestValue(n, A):
        
        dp = [0, A[0] * A[1]]
        rsum = [A[0]]
        for i in range(1, n):
            rsum += [rsum[-1] + A[i]]
        for i in range(2, n):
            dp += [dp[-1] + rsum[i - 1] * A[i]]
        
        m = max(dp)
        res = m
        for i in range(n):
            # remove if-statement for O(n^2) -> passes 100% for n <= 5000
            if(dp[i] != m):
                continue
                
            rrsum = rsum[i]
            dp2 = dp[i]
            for j in range(0, i):
                rrsum -= A[j]
                dp2 -= A[j] * rrsum
                res = max(dp2, res)
            
        return res
        
    if __name__ == "__main__":
        n = int(input().strip())
        A = list(map(int, input().strip().split(' ')))
        result = max(largestValue(n, A), largestValue(n, A[::-1]))
        print(result)
    

    I find the maximum value of arrays A[0:i] for all i and just "assume" A[max_i] is an endpoint of the desired subarray. Then I just find the maximum value of arrays containing one of those endpoints.

    Surprisingly, this passes for 70.4/80 points (18/21 correct). Is this because of generous test cases? Also, is A = [0,0,...,0] the only worst-case for this algorithm?

  • + 0 comments

    Try 5 7 -5 6 3 9 as subarray.

  • + 1 comment

    Is the sub array a contiguous one or any of the elements of array?