Position of Elements which are equal to sum of all Preceding elements
Last Updated :
10 Mar, 2022
Given an array Arr[] of N of positive integers. The task is to find positions of all the elements which are equal to the sum of all preceding elements. If no such element exists print -1.
Examples:
Input : Arr[] = {1, 2, 3, 6, 3, 15, 5}
Output :3 4 6
Here, the element at index “3” i.e. 3 is equal to the sum of preceding elements (1 + 2).
Similarly, at index 4, 6 = 1+2+3 (sum of all preceding elements).
And element at index 6 i.e. 15 = 1 + 2 + 3 + 6 + 3.
Input: Arr[] = {7, 5, 17, 25}
Output: -1
Approach:
While traversing the array Arr[], maintain a sum variable that store the sum of elements till i – 1. Compare the sum with current element Arr[i]. If it is equal, push the index of this element into the answer vector.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > find_idx( int ar[], int n)
{
vector< int > answer;
int sum = ar[0];
for ( int i = 1; i < n; i++) {
if (sum == ar[i]) {
answer.push_back(i + 1);
}
sum += ar[i];
}
return answer;
}
int main()
{
int ar[] = { 1, 2, 3, 6, 3, 15, 5 };
int n = sizeof (ar) / sizeof ( int );
vector< int > ans = find_idx(ar, n);
if (ans.size() != 0) {
for ( int i : ans) {
cout << i << " " ;
}
}
else {
cout << "-1" ;
}
cout << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static Vector<Integer> find_idx( int ar[], int n)
{
Vector<Integer> answer = new Vector<Integer>();
int sum = ar[ 0 ];
for ( int i = 1 ; i < n; i++)
{
if (sum == ar[i])
{
answer.add(i + 1 );
}
sum += ar[i];
}
return answer;
}
public static void main(String[] args)
{
int ar[] = { 1 , 2 , 3 , 6 , 3 , 15 , 5 };
int n = ar.length;
Vector<Integer> ans = find_idx(ar, n);
if (ans.size() != 0 )
{
for ( int i : ans)
{
System.out.print(i + " " );
}
}
else
{
System.out.println( "-1" );
}
}
}
|
Python3
def find_idx(ar, n) :
answer = [];
sum = ar[ 0 ];
for i in range ( 1 , n) :
if ( sum = = ar[i]) :
answer.append(i + 1 );
sum + = ar[i];
return answer;
if __name__ = = "__main__" :
ar = [ 1 , 2 , 3 , 6 , 3 , 15 , 5 ];
n = len (ar);
ans = find_idx(ar, n);
if ( len (ans) ! = 0 ) :
for i in ans :
print (i, end = " " );
else :
print ( "-1" );
print ();
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static List< int > find_idx( int []ar, int n)
{
List< int > answer = new List< int >();
int sum = ar[0];
for ( int i = 1; i < n; i++)
{
if (sum == ar[i])
{
answer.Add(i + 1);
}
sum += ar[i];
}
return answer;
}
public static void Main(String[] args)
{
int []ar = { 1, 2, 3, 6, 3, 15, 5 };
int n = ar.Length;
List< int > ans = find_idx(ar, n);
if (ans.Count != 0)
{
foreach ( int i in ans)
{
Console.Write(i + " " );
}
}
else
{
Console.WriteLine( "-1" );
}
}
}
|
Javascript
<script>
function find_idx(ar, n) {
let answer = [];
let sum = ar[0];
for (let i = 1; i < n; i++) {
if (sum == ar[i]) {
answer.push(i + 1);
}
sum += ar[i];
}
return answer;
}
let ar = [1, 2, 3, 6, 3, 15, 5];
let n = ar.length;
let ans = find_idx(ar, n);
if (ans.length != 0) {
for (let i of ans) {
document.write(i + " " );
}
}
else {
document.write( "-1" );
}
document.write( "<br>" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...