Output not coming

#include
using namespace std;
int main() {
int n,water=0,maxl,maxr,i,j;
cin>>n;
int *arr = new int[n];
int *arr1 = new int[n];
int *arr3 = new int[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
maxl = arr[0];
for( i=1,j=0;i<n&&j<n;i++,j++)
{
if(arr[i]>maxl)
{
maxl=arr[i];
arr1[j] = maxl;

	}
}
maxr=arr[n-1];
for( i=n-2,j=n-1;i>=0&&j>=0;i--,j--)
{
	if(arr[i]>maxr)
	{
		maxr=arr[i];
		arr3[j] = maxr;
	}
}
for( j=0;j<n;j++)
{
	if(arr1[j]<arr3[j])
	water =water +( arr1[j] - arr[i] );
	else
	water = water +(arr3[j]-arr[i]);
}
cout<<water;  


return 0;

}

hello Supratik,
what error u are getting?

I am getting output as 0

In your arr1 array , arr1[i] is maximum element value from 0 to i right?
and In your arr3 array , arr3[i] is maximum element value from i to n-1 right?

yes arr1 is maximum from left to right and arr3 is maximum from right to left

@supratik260699 hey check out this code i have made some changes - https://ide.codingblocks.com/s/173001

errors-

  1. in ur last loop if u see u are subtracting arr[i] but it should be arr[j]
    i.e water=water + (arr1[j]-arr[j])

  2. your arr1 and arr3 are not calculating correct values so i have made it simple
    if(arr[j]>maxl)
    {
    maxl=arr[j];
    }
    arr1[j] = maxl;

let me know if u still have any doubt

why did u didnt use i in any of the for loops for comparison of maximum value,you used j which is different array. dont we need i in any of the for loops?

see it does’nt matter whether I use i or j . also we can use same variable to access different array.

apart from this problem i want to give u another problem.
you are given an array and you need to store maximum from left to right in another array.
can u please write a program for this and share it here.
example

size 5
array 4 1 8 9 2

output 4 4 8 9 9
array

ok i am giving it to you

#include
using namespace std;
int main()
{
int n,max;
cin>>n;
int *arr = new int[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
max = arr[0];
for(int i=0;i<n;i++)
{
if(arr[i]>arr[0])
{
max = arr[i];
}
cout<<max<<" ";
}

}

check ur if statement do you think is it correct?

is there any other approach better than this beacuse i have done all the program of finding maximum like this only

for linear non sorted array above one is the only approach.also your code is not correct

your if statement should be like
if(arr[i]>max])
{
max = arr[i];
}

can u plz tell me what is wrong bcoz output is coming correctly

check your code output for this
size 5
array 4 1 9 8 2

O I have understood about the mistake it should be if(arr[i]>max)

ok so are u confident ?