fork download
  1. #include<iostream>
  2. using namespace std;
  3. int prefixsum(int i,int a[])
  4. {
  5. int sum=0;
  6. for(int j=0;j<=i;j++)
  7. {
  8. sum+=a[j];
  9. }
  10. return sum;
  11. }
  12. int suffixsum(int i,int a[],int n)
  13. {
  14. int sum=0;
  15. for(int j=n-1;j>=i;j--)
  16. {
  17. sum+=a[j];
  18. }
  19. return sum;
  20. }
  21. int main()
  22. {
  23. // your code goes here
  24. int a[100],b[100],min,n,i,p,t,s,c[10];
  25. cout<<"\n Enter no. of test cases";
  26. cin>>t;
  27. for(s=0;s<t;s++)
  28. {
  29. cout<<"Enter the size of array";
  30. cout<<"\n";
  31. cin>>n;
  32. cout<<"Enter elements of array";
  33. for(i=0;i<n;i++)
  34. cin>>a[i];
  35. for(i=0;i<n;i++)
  36. {
  37. b[i]=prefixsum(i,a)+suffixsum(i,a,n);
  38. }
  39. min=b[0];
  40. p=1;
  41. for(i=1;i<n;i++)
  42. {
  43. //min=b[i];
  44. if(min>b[i])
  45. {
  46. min=b[i];
  47. p=i+1;
  48. }
  49. }
  50. c[s]=p;
  51. }
  52. cout<<"\n Output";
  53. for(s=0;s<t;s++)
  54. {
  55. cout<<"\n";
  56. cout<<c[s];
  57. }
  58. return 0;
  59. }
Success #stdin #stdout 0s 16048KB
stdin
1
2
1
2
stdout
 Enter no. of test casesEnter the size of array
Enter elements of array
 Output
1