Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Delete Element
Problem Statement
Antonio is a professor at the University of Berland and does research on nice arrays.A nice array is an array such that :
There is an element in the array = (Sum of all the elements of the array)/2.Like array = [1,3,6,2] is a nice array as the third element of the array = (1+3+6+2)/2 = 6, but array = [1,2,4] is not a nice array as there is no suchelement.
Given an array A of length N, you have to tell if it is possible to remove exactly one element from array A such that the resulting array is a nice array.
If it’s possible then print the position of that element. If there are many such elements, print the minimum position.
If there is no such element in array A, print "-1" instead.
Input Format
First line contains a single integer denoting N.
The next line contains N space-separated integers denoting the elements of array A.
Output Format
Print the position of the element such that removing it makes array A a nice array. If there is no such element in array A, print "-1" instead.
Constraints
2<=N<=105
1<=Ai<=109
Sample Input 1
5
5 2 1 2 2
Sample Output 1
2
Explanation of Sample 1
Removing the 2nd element will make array A = [5,1,2,2].
Here the first element of array A = (Sum of all the elements of the array)/2 = (5+1+2+2)/2 = 5.


What I have tried:

Please help me to solve this problem I am unable to build logic
Posted
Updated 12-Nov-22 2:29am
v2

The logic is simple (at least, following a naive approach):
Make a function
C++
bool check_nice(int array[], int skip_index)

which checks if all the items of array but array[skip_index] form a nice array and then call it iteratively until it returns true or the whole array has been tested.
 
Share this answer
 
Comments
merano99 12-Nov-22 13:30pm    
+5
CPallini 12-Nov-22 16:22pm    
Thank you.
Sorry, you have come to the wrong place. This site offers help with code that you write, it does not provide code to order. If you do not understand the requirements then you should discuss the problem with your teacher.
 
Share this answer
 
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900