原文:
给定一个大小为 n 的 arr[] ,任务是找到数组元素的索引,移除这些索引会使奇数和偶数索引元素的总和相等。如果不存在这样的元素,则打印 -1 。
示例:
输入: arr[] = {4,1,6,2} 输出: 1 解释: 移除 arr[1]将 arr[]修改为{4,6,2} 偶数索引数组元素之和= arr[0] arr[2] = 4 2 = 6 奇数索引数组元素之和= arr[1] = 6 因此,所需输出为 1。
输入: arr = {3,2,1} 输出: -1
天真法:解决这个问题最简单的方法是遍历数组并从数组中移除 i th 元素,检查奇数索引数组元素的和是否等于偶数索引数组元素的和。如果发现是真的,那么打印索引。
时间复杂度:o(n2) 辅助空间: o(n)
有效方法:上述方法可以使用进行优化。其思想是利用这样一个事实:如果一个数组元素从索引中移除,那么在那个索引之后,偶数索引的数组元素变成奇数索引的,反之亦然。按照以下步骤解决问题:
- 初始化两个数组,比如奇数[] 和偶数[] ,分别存储奇数和偶数索引数组元素的前缀和和偶数索引数组元素的前缀和。
- 遍历数组 arr[] ,计算奇数和偶数索引数组元素的前缀和。
- 初始化两个变量,比如 p = 0 和 q = 0 ,分别存储一个数组元素移除后的偶数和奇数索引数组元素的和。
- 遍历数组,逐个删除数组元素,并相应更新前缀和。检查偶数索引数组元素的和 p 是否等于奇数索引数组元素的和 q 。如果发现为真,则打印当前索引。
- 否则,打印 -1 。
下面是上述方法的实现:
c
// c program to implement
// the above approach
#include
using namespace std;
// function to find indices of array elements
// whose removal makes the sum of odd and
// even indexed array elements equal
void removeindicestomakesumequal(vector& arr)
{
// stores size of array
int n = arr.size();
// store prefix sum of odd
// index array elements
vector odd(n, 0);
// store prefix sum of even
// index array elements
vector even(n, 0);
// update even[0]
even[0] = arr[0];
// traverse the given array
for (int i = 1; i < n; i ) {
// update odd[i]
odd[i] = odd[i - 1];
// update even[i]
even[i] = even[i - 1];
// if the current index
// is an even number
if (i % 2 == 0) {
// update even[i]
even[i] = arr[i];
}
// if the current index
// is an odd number
else {
// update odd[i]
odd[i] = arr[i];
}
}
// check if at least one
// index found or not that
// satisfies the condition
bool find = 0;
// store odd indices sum by
// removing 0-th index
int p = odd[n - 1];
// store even indices sum by
// removing 0-th index
int q = even[n - 1] - arr[0];
// if p and q are equal
if (p == q) {
cout << "0 ";
find = 1;
}
// traverse the array arr[]
for (int i = 1; i < n; i ) {
// if i is an even number
if (i % 2 == 0) {
// update p by removing
// the i-th element
p = even[n - 1] - even[i - 1]
- arr[i] odd[i - 1];
// update q by removing
// the i-th element
q = odd[n - 1] - odd[i - 1]
even[i - 1];
}
else {
// update q by removing
// the i-th element
q = odd[n - 1] - odd[i - 1]
- arr[i] even[i - 1];
// update p by removing
// the i-th element
p = even[n - 1] - even[i - 1]
odd[i - 1];
}
// if odd index values sum is equal
// to even index values sum
if (p == q) {
// set the find variable
find = 1;
// print the current index
cout << i << " ";
}
}
// if no index found
if (!find) {
// print not possible
cout << -1;
}
}
// driver code
int main()
{
vector arr = { 4, 1, 6, 2 };
removeindicestomakesumequal(arr);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program to implement
// the above approach
import java.util.*;
class gfg{
// function to find indices of array elements
// whose removal makes the sum of odd and
// even indexed array elements equal
static void removeindicestomakesumequal(int []arr)
{
// stores size of array
int n = arr.length;
// store prefix sum of odd
// index array elements
int []odd = new int[n];
// store prefix sum of even
// index array elements
int []even = new int[n];
// update even[0]
even[0] = arr[0];
// traverse the given array
for(int i = 1; i < n; i )
{
// update odd[i]
odd[i] = odd[i - 1];
// update even[i]
even[i] = even[i - 1];
// if the current index
// is an even number
if (i % 2 == 0)
{
// update even[i]
even[i] = arr[i];
}
// if the current index
// is an odd number
else
{
// update odd[i]
odd[i] = arr[i];
}
}
// check if at least one
// index found or not that
// satisfies the condition
boolean find = false;
// store odd indices sum by
// removing 0-th index
int p = odd[n - 1];
// store even indices sum by
// removing 0-th index
int q = even[n - 1] - arr[0];
// if p and q are equal
if (p == q)
{
system.out.print("0 ");
find = true;
}
// traverse the array arr[]
for(int i = 1; i < n; i )
{
// if i is an even number
if (i % 2 == 0)
{
// update p by removing
// the i-th element
p = even[n - 1] - even[i - 1] -
arr[i] odd[i - 1];
// update q by removing
// the i-th element
q = odd[n - 1] - odd[i - 1]
even[i - 1];
}
else
{
// update q by removing
// the i-th element
q = odd[n - 1] - odd[i - 1] -
arr[i] even[i - 1];
// update p by removing
// the i-th element
p = even[n - 1] - even[i - 1]
odd[i - 1];
}
// if odd index values sum is equal
// to even index values sum
if (p == q)
{
// set the find variable
find = true;
// print the current index
system.out.print(i " ");
}
}
// if no index found
if (!find)
{
// print not possible
system.out.print(-1);
}
}
// driver code
public static void main(string[] args)
{
int[] arr = { 4, 1, 6, 2 };
removeindicestomakesumequal(arr);
}
}
// this code is contributed by amit katiyar
python 3
# python program to implement
# the above approach
# function to find indices of array elements
# whose removal makes the sum of odd and
# even indexed array elements equal
def removeindicestomakesumequal(arr):
# stores size of array
n = len(arr);
# store prefix sum of odd
# index array elements
odd = [0] * n;
# store prefix sum of even
# index array elements
even = [0] * n;
# update even[0]
even[0] = arr[0];
# traverse the given array
for i in range(1, n):
# update odd[i]
odd[i] = odd[i - 1];
# update even[i]
even[i] = even[i - 1];
# if the current index
# is an even number
if (i % 2 == 0):
# update even[i]
even[i] = arr[i];
# if the current index
# is an odd number
else:
# update odd[i]
odd[i] = arr[i];
# check if at least one
# index found or not that
# satisfies the condition
find = false;
# store odd indices sum by
# removing 0-th index
p = odd[n - 1];
# store even indices sum by
# removing 0-th index
q = even[n - 1] - arr[0];
# if p and q are equal
if (p == q):
print("0 ");
find = true;
# traverse the array arr
for i in range(1, n):
# if i is an even number
if (i % 2 == 0):
# update p by removing
# the i-th element
p = even[n - 1] - even[i - 1] - arr[i] odd[i - 1];
# update q by removing
# the i-th element
q = odd[n - 1] - odd[i - 1] even[i - 1];
else:
# update q by removing
# the i-th element
q = odd[n - 1] - odd[i - 1] - arr[i] even[i - 1];
# update p by removing
# the i-th element
p = even[n - 1] - even[i - 1] odd[i - 1];
# if odd index values sum is equal
# to even index values sum
if (p == q):
# set the find variable
find = true;
# print the current index
print(i, end = "");
# if no index found
if (find == false):
# print not possible
print(-1);
# driver code
if __name__ == '__main__':
arr = [4, 1, 6, 2];
removeindicestomakesumequal(arr);
# this code is contributed by shikhasingrajput
c
// c# program to implement
// the above approach
using system;
class gfg{
// function to find indices of array elements
// whose removal makes the sum of odd and
// even indexed array elements equal
static void removeindicestomakesumequal(int []arr)
{
// stores size of array
int n = arr.length;
// store prefix sum of odd
// index array elements
int []odd = new int[n];
// store prefix sum of even
// index array elements
int []even = new int[n];
// update even[0]
even[0] = arr[0];
// traverse the given array
for(int i = 1; i < n; i )
{
// update odd[i]
odd[i] = odd[i - 1];
// update even[i]
even[i] = even[i - 1];
// if the current index
// is an even number
if (i % 2 == 0)
{
// update even[i]
even[i] = arr[i];
}
// if the current index
// is an odd number
else
{
// update odd[i]
odd[i] = arr[i];
}
}
// check if at least one
// index found or not that
// satisfies the condition
bool find = false;
// store odd indices sum by
// removing 0-th index
int p = odd[n - 1];
// store even indices sum by
// removing 0-th index
int q = even[n - 1] - arr[0];
// if p and q are equal
if (p == q)
{
console.write("0 ");
find = true;
}
// traverse the array arr[]
for(int i = 1; i < n; i )
{
// if i is an even number
if (i % 2 == 0)
{
// update p by removing
// the i-th element
p = even[n - 1] - even[i - 1] -
arr[i] odd[i - 1];
// update q by removing
// the i-th element
q = odd[n - 1] - odd[i - 1]
even[i - 1];
}
else
{
// update q by removing
// the i-th element
q = odd[n - 1] - odd[i - 1] -
arr[i] even[i - 1];
// update p by removing
// the i-th element
p = even[n - 1] - even[i - 1]
odd[i - 1];
}
// if odd index values sum is equal
// to even index values sum
if (p == q)
{
// set the find variable
find = true;
// print the current index
console.write(i " ");
}
}
// if no index found
if (!find)
{
// print not possible
console.write(-1);
}
}
// driver code
public static void main(string[] args)
{
int[] arr = { 4, 1, 6, 2 };
removeindicestomakesumequal(arr);
}
}
// this code is contributed by ankthon
java 描述语言
output:
1
时间复杂度:o(n) t5辅助空间:** o(n)
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处