array of numbers were given to find a number which has same sum of numbers in it’s either side.
I/p 1, 2, 3, 7, 6
O/p 7(has 1+ 2+3 in left 6 in right)
public class NewClass31 {
void side(int ar[])
{
int c=1,sum1;
for(int i=0;i<ar.length-2;i++)
{
sum1=0;
for(int j=0;j<ar.length;j++)
{
if(j<c)
sum1+=ar[j];
if(j>c)
sum1-=ar[j];
}
if(sum1==0)
System.out.println(ar[c]);
c++;
}
}
public static void main(String args[])
{
int ar[]={-2, 9, -9, 8, -1};
NewClass31 ob=new NewClass31();
ob.side(ar);
}
}
No comments:
Post a Comment