Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Beautiful Index
EASY
16 mins
21 upvotes
Arrays
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Arrays
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Beautiful Index

Contributed by
Nihal Srivastava
Easy
yellow-spark
0/40
Avg time to solve 16 mins
Success Rate 91 %
Share
21 upvotes

Problem Statement

You are given an array ‘A’ of length ‘N’. You say an index ‘i’ is beautiful if the sum of the first ‘i - 1’ elements of the array ‘A’ equals the sum of the last ‘N - i’ elements of the array ‘A’, where ‘i’ is in 1-based indexing. Now you wonder which is the leftmost beautiful index.

Note: If you select the first index, then the sum of the prefix will be ‘0’, and if you select the last index, then the sum of the suffix will be ‘0’.

Note: You have to print the index in 1-based indexing.

For example:

Let’s say the array ‘A’ = [1, 3, 1, 5], then if we select index ‘2’, the sum of the prefix is ‘1’, and the sum of the suffix is 1 + 5 = 6. Since the sum is not the same, hence index ‘2’ is not a beautiful index.
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input-1
2
3
1 1 1
3
1 2 3
Sample Output-1
2
-1
Explanation for Sample Input 1:
For test case 1:
    Index ‘2’ is the leftmost beautiful index. The left sum is 1 and the right sum is also 1.
For test case 2:
    No index is beautiful.
Sample Input -2
2
6
1 7 3 6 5 6
3
2 1 -1
Sample Output -2
4
1
Reset Code
Full screen
Auto
copy-code
Console