Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Our little chef is fond of doing additions/sums in his free time. Today, he has an array A consisting of N positive integers and he will compute prefix and suffix sums over this array.

He first defines two functions prefixSum(i) and suffixSum(i) for the array as follows. The function prefixSum(i) denotes the sum of first i numbers of the array. Similarly, he defines suffixSum(i) as the sum of last N - i + 1 numbers of the array.

Little Chef is interested in finding the minimum index i for which the value prefixSum(i) + suffixSum(i) is the minimum. In other words, first you should minimize the value of prefixSum(i) + suffixSum(i), and then find the least index i for which this value is attained.


What I have tried:

Java
<pre>import java.util.*;
import java.lang.*;
import java.io.*;
 
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
	int t;
	Scanner sc=new Scanner(System.in);
	t=sc.nextInt();
	for(int i=0;i<t;i++)
	{
	    int n=sc.nextInt();
	    int sum=0,count=0,min=Integer.MAX_VALUE;
	    int[] arr=new int[n+1];
	    int[] prefix=new int[n+1];
	    int[] suffix=new int[n+1];
	    int[] array=new int[n];
        int[] array1=new int[n];
	    for(int j=0;j<n;j++)
	    {
	    arr[j]=sc.nextInt();
	    sum+=arr[j];
	    }
	    suffix[0]=sum;
	   for(int j=1;j<n;j++)
	   {
	       sum-=(arr[j-1]);
	       suffix[j]=sum;
	   }
	     prefix[0] = arr[0];
	     for (int j = 1; j < n; j++)
	     {
	         prefix[j] = prefix[j-1] + arr[j];
	        
	     }
	     int max=Integer.MAX_VALUE;
	     for(int j=0;j<n;j++)
	     {
	         array[j]=suffix[j]+prefix[j];
             if(min>=array[j])
                 min=array[j];
           //  array1[j]=array[j];
	        //System.out.print(array[j] +" ");
	    // min=Math.min(min,array[j]);
	     }
      
	   
//	     max=array[0];
	   for(int j=0;j<array.length;j++)
	   {
	       if(array[j]==min)
           {
               if(max>=j)
                   max=j;
           }
	     //  System.out.println(j);
	    // System.out.print(array[j]+" ");
	      
	   }
       System.out.println(max+1);
        
	}
	}
}
Posted
Updated 7-Sep-17 13:28pm
Comments
Mehdi Gholam 7-Sep-17 8:38am    
Looks like homework.
ZurdoDev 7-Sep-17 9:05am    
So? If they can ask a direct question there is nothing wrong with helping someone with their homework.
OriginalGriff 7-Sep-17 8:39am    
And? What does it do that you didn't expect, or not do that you did?
What have you tried to work out what the problem is?
What help do you need?
ZurdoDev 7-Sep-17 9:05am    
And what is your question?
Member 12156949 10-Sep-17 11:55am    
what changes are to be made into my code?

1 solution

Quote:
Chef and sum with prefix and suffix(getting some test case failed)

Showing a reduced example of failure (no more than 10 values) would have helped to understand your problem.

You know what your code is supposed to do, use the debugger to find where your code do not behave as expected.
Pay special attention to how you set prefix, as I understand the problem, the prefix of first element should be 0.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900