public class SplitArrayInTwoAverageEqual {
public static void main(String[] args) {
int[] arr = {1,5,7,2,0};
int n = arr.length;
int sum = 0;
for(int i=0;i<n;i++) {
sum+=arr[i];
}
int lsum = 0;
for(int i=0;i<n-1;i++) {
lsum+=arr[i];
int rsum = sum-lsum;
if(lsum*(n-i-1)==rsum*(i+1)) {
System.out.println("first is: 0 to "+i+" and other is: "+(i+1) +" to "+ (n-1));
}
}
}
}
output: first is: 0 to 1 and other is: 2 to 4
public static void main(String[] args) {
int[] arr = {1,5,7,2,0};
int n = arr.length;
int sum = 0;
for(int i=0;i<n;i++) {
sum+=arr[i];
}
int lsum = 0;
for(int i=0;i<n-1;i++) {
lsum+=arr[i];
int rsum = sum-lsum;
if(lsum*(n-i-1)==rsum*(i+1)) {
System.out.println("first is: 0 to "+i+" and other is: "+(i+1) +" to "+ (n-1));
}
}
}
}
output: first is: 0 to 1 and other is: 2 to 4
No comments:
Post a Comment