Help me in solving FINALSALE problem

My issue

I want to understand the basic approach to this problem

My code

import java.util.Scanner;
import java.lang.*;
import java.io.*;

class Codechef
{
    public static void main(String[] args) throws java.lang.Exception
    {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        int sum = 0, largest = 0, index = 0, sales = 0;
        for (int i = 0; i < t; i++) {
            int n = sc.nextInt();
            int arr[] = new int[n];
            for (int j = 0; j < n; j++) {
                arr[j] = sc.nextInt();
            }
            for (int k = 0; k < n; k++) {
                for (int l = 0; l <= k; l++) {
                    if (l == k) {
                        sum = sum + (arr[l] * 2);
                    } else {
                        sum = sum + arr[l];
                    }
                }
                if (sum > largest) {
                    largest = sum;
                    index = k;
                }
            }System.out.println(largest);

            for (int p = 0; p < index; p++) {
                sales = sales + arr[p];
            }
            System.out.println(sales + (arr[index] * 2));
        }

    }
}

Problem Link: Sale Practice Coding Problem - CodeChef

You have a couple of issues which I commented in your code below:

  1. Your “largest” variable was initialized outside each test, breaking each-test largest logic
  2. You are using very large numbers (10^9). You should use long type instead of int.
  3. You waste too much time using nested loops. You could’ve used an auxilar variable to keep track of the sums, instead of summing each time (which also is never initialized within the test).
  4. You print something necessarily based on “index” something.
import java.util.Scanner;
import java.lang.*;
import java.io.*;

class Codechef
{
    public static void main(String[] args) throws java.lang.Exception
    {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        long sum = 0, largest = 0, index = 0, sales = 0;
        for (int i = 0; i < t; i++) {
            
            // Each test has its own largest
            largest = 0;
            
            int n = sc.nextInt();
            long arr[] = new long[n];
            for (int j = 0; j < n; j++) {
                arr[j] = sc.nextLong();
                
                // You base largest is the original sum of all days
                largest += arr[j];
            }
            
            // Don't waste loops. Just take into consideration your prev days
            long prevDays = 0;
            
            for (int k = 0; k < n; k++) {
                
                
                /*  This is so computationally heavy. Nested loops are the last option
                
                for (int l = 0; l <= k; l++) {
                    if (l == k) {
                        sum = sum + (arr[l] * 2);
                    } else {
                        sum = sum + arr[l];
                    }
                }*/
                
                sum = prevDays + arr[k]*2;
                
                if (sum > largest) {
                    largest = sum;
                    //index = k;        // Why do you want this??
                }
                
                // Sum the prev days
                prevDays += arr[k];
                
            }
            System.out.println(largest);
            
            /*
            for (int p = 0; p < index; p++) {
                sales = sales + arr[p];
            }
            System.out.println(sales + (arr[index] * 2));
            */
        }

    }
}

can you explain me the use of variable largest which you are using. As I am not able to understand what is the use of it and what actually it is pointing.

Each test has an array the numbers, the sales Chef can make, say:

A = [1,1,1,1]

“largest” (or biggest) is the maximum amount of sales Chef can do. In this example “largest” would be 5 (1+1+1+2*1)

A= [1,7,2,1]
largest = 15 (1+7*2)

“Largest” variable is compared with all the possibilities, and stores the maximum possible.