Spilt Array - Equal Sum

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

Solution :-
#include<stdio.h>
#include <stdlib.h>

int main()
{
    int  n;
    scanf("%d",&n);
    int arr[n],sum1=0,sum2=0,flag=0;
    
    for(int i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    
    for(int i=0;i<n;i++)
    {
        sum1=sum1+arr[i];
        sum2=0;
        for(int j=i+1;j<n;j++)
        {
            sum2+=arr[j];
            
        }
        if(sum1==sum2)
        {
            flag=1;
            break;
        }
        if(flag==1)
        {
            break;
        }
    }
    
    if(flag==1)
    printf("YES");
    else
    printf("NO");


}

Comments

Popular posts from this blog

Two Matrix Spiral Print

Alphabets Positions Reversed

Odd Factors - (Error Identification) skillrack program id - 7306