给定N个正整数的数组Arr [] 。任务是找到等于所有先前元素之和的所有元素的位置。如果不存在这样的元素,则打印-1。
例子:
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
方法:
在遍历数组Arr []时,请维护一个sum变量,该变量存储元素的总和,直到i – 1为止。将总和与当前元素Arr [i]进行比较。如果相等,则将该元素的索引推入答案向量。
下面是上述方法的实现:
C++
// C++ implementation
#include
using namespace std;
// function to return valid indexes
vector find_idx(int ar[], int n)
{
// Vector to store the answer
vector answer;
// Initial sum would always
// be first element
int sum = ar[0];
for (int i = 1; i < n; i++) {
// Check if sum till now
// is equal to current element
if (sum == ar[i]) {
answer.push_back(i + 1);
}
// Updating the sum by
// adding the current
// element in each
// iteration.
sum += ar[i];
}
return answer;
}
// Driver code
int main()
{
int ar[] = { 1, 2, 3, 6, 3, 15, 5 };
int n = sizeof(ar) / sizeof(int);
vector ans = find_idx(ar, n);
if (ans.size() != 0) {
for (int i : ans) {
cout << i << " ";
}
}
else {
cout << "-1";
}
cout << endl;
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// function to return valid indexes
static Vector find_idx(int ar[], int n)
{
// Vector to store the answer
Vector answer = new Vector();
// Initial sum would always
// be first element
int sum = ar[0];
for (int i = 1; i < n; i++)
{
// Check if sum till now
// is equal to current element
if (sum == ar[i])
{
answer.add(i + 1);
}
// Updating the sum by adding the
// current element in each iteration.
sum += ar[i];
}
return answer;
}
// Driver code
public static void main(String[] args)
{
int ar[] = { 1, 2, 3, 6, 3, 15, 5 };
int n = ar.length;
Vector ans = find_idx(ar, n);
if (ans.size() != 0)
{
for (int i : ans)
{
System.out.print(i + " ");
}
}
else
{
System.out.println("-1");
}
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the above approach
# function to return valid indexes
def find_idx(ar, n) :
# Vector to store the answer
answer = [];
# Initial sum would always
# be first element
sum = ar[0];
for i in range(1, n) :
# Check if sum till now
# is equal to current element
if (sum == ar[i]) :
answer.append(i + 1);
# Updating the sum by
# adding the current
# element in each
# iteration.
sum += ar[i];
return answer;
# Driver code
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();
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// function to return valid indexes
static List find_idx(int []ar, int n)
{
// Vector to store the answer
List answer = new List();
// Initial sum would always
// be first element
int sum = ar[0];
for (int i = 1; i < n; i++)
{
// Check if sum till now
// is equal to current element
if (sum == ar[i])
{
answer.Add(i + 1);
}
// Updating the sum by adding the
// current element in each iteration.
sum += ar[i];
}
return answer;
}
// Driver code
public static void Main(String[] args)
{
int []ar = { 1, 2, 3, 6, 3, 15, 5 };
int n = ar.Length;
List ans = find_idx(ar, n);
if (ans.Count != 0)
{
foreach (int i in ans)
{
Console.Write(i + " ");
}
}
else
{
Console.WriteLine("-1");
}
}
}
// This code is contributed by Princi Singh
输出:
3 4 6