The program must accept an integer array of size N as the input. The program must print YES if it is possible to split the array into two sides so that the sum of the integers on one side is equal to the sum of the integers on the other side. Else the program must print NO as the output.
Boundary Condition(s):
2 <= N <= 100
1 <= Each integer value <= 10^5
Input Format:
The first line contains the value of N.
The second line contains N integers separated by a space.
Output Format:
The first line contains either YES or NO.
Example Input/Output 1:
Input:
5
5 4 7 1 1
Output:
YES
Explanation:
The array can be split as “5 4” and “7 1 1”.
The sum of the integers on one side is 9 (5 + 4).
The sum of the integers on the other side is 9 (7 + 1 + 1).
Here the sum of the integers on one side is equal to the sum of the integers on the other side.
Hence the output is YES
Example Input/Output 2:
Input:
6
2 6 8 3 4 1
Output:
NO
#include<stdio.h>
int main() {
int n;
scanf("%d", &n);
int arr[n], prefixSum[n], suffixSum[n];
// Read array and compute prefix sum
prefixSum[0] = 0;
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
prefixSum[i] = (i == 0) ? arr[i] : prefixSum[i-1] + arr[i];
}
// Compute suffix sum
suffixSum[n-1] = arr[n-1];
for(int i = n-2; i >= 0; i--) {
suffixSum[i] = suffixSum[i+1] + arr[i];
}
int flag = 0;
for(int i = 0; i < n-1; i++) {
if(prefixSum[i] == suffixSum[i+1]) {
flag = 1;
break;
}
}
printf(flag ? "YES" : "NO");
return 0;
}
n = int(input())
arr = list(map(int, input().split()))
# Compute prefix and suffix sums
prefixSum = [0] * n
suffixSum = [0] * n
prefixSum[0] = arr[0]
suffixSum[-1] = arr[-1]
for i in range(1, n):
prefixSum[i] = prefixSum[i-1] + arr[i]
for i in range(n-2, -1, -1):
suffixSum[i] = suffixSum[i+1] + arr[i]
# Check if there's a split point
for i in range(n-1):
if prefixSum[i] == suffixSum[i+1]:
print("YES")
break
else:
print("NO")
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector arr(n), prefixSum(n), suffixSum(n);
// Read array and compute prefix sum
cin >> arr[0];
prefixSum[0] = arr[0];
for(int i = 1; i < n; i++) {
cin >> arr[i];
prefixSum[i] = prefixSum[i-1] + arr[i];
}
// Compute suffix sum
suffixSum[n-1] = arr[n-1];
for(int i = n-2; i >= 0; i--) {
suffixSum[i] = suffixSum[i+1] + arr[i];
}
// Check if there's a split point
bool found = false;
for(int i = 0; i < n-1; i++) {
if(prefixSum[i] == suffixSum[i+1]) {
found = true;
break;
}
}
cout << (found ? "YES" : "NO");
return 0;
}
import java.util.Scanner;
public class SplitArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
int[] prefixSum = new int[n];
int[] suffixSum = new int[n];
// Read array and compute prefix sum
arr[0] = sc.nextInt();
prefixSum[0] = arr[0];
for(int i = 1; i < n; i++) {
arr[i] = sc.nextInt();
prefixSum[i] = prefixSum[i-1] + arr[i];
}
// Compute suffix sum
suffixSum[n-1] = arr[n-1];
for(int i = n-2; i >= 0; i--) {
suffixSum[i] = suffixSum[i+1] + arr[i];
}
// Check if there's a split point
boolean found = false;
for(int i = 0; i < n-1; i++) {
if(prefixSum[i] == suffixSum[i+1]) {
found = true;
break;
}
}
System.out.println(found ? "YES" : "NO");
}
}
Leave a Reply