原文:
给定一个由 n 个整数组成的数组arr【】,任务是打印需要移除的两个数组元素的索引,这样给定的。如果不可能,则打印-1”。
示例:
输入: arr[] = {2,5,12,7,19,4,3} 输出: 2 4 解释: 移除 arr[2]和 arr[4]将 arr[]修改为{2,5,7,4,3}。 子阵{arr[0],arr[1]}之和= 7。 arr[2] = 7。 子阵{arr[3],arr[4]}之和= 7。
输入: arr[] = {2,1,13,5,14} 输出: -1
天真方法:最简单的方法是,对于每一对,检查移除这些对是否可以从给定阵列生成三个相等和的子阵列。
下面是上述方法的实现:
c
// c program for the above approach
#include
using namespace std;
// function to check if array can
// be split into three equal sum
// subarrays by removing two elements
void findsplit(int arr[], int n)
{
for (int l = 1; l <= n - 4; l ) {
for (int r = l 2; r <= n - 2; r ) {
// stores sum of all three subarrays
int lsum = 0, rsum = 0, msum = 0;
// sum of left subarray
for (int i = 0; i <= l - 1; i ) {
lsum = arr[i];
}
// sum of middle subarray
for (int i = l 1; i <= r - 1; i ) {
msum = arr[i];
}
// sum of right subarray
for (int i = r 1; i < n; i ) {
rsum = arr[i];
}
// check if sum of subarrays are equal
if (lsum == rsum && rsum == msum) {
// print the possible pair
cout << l << " " << r << endl;
return;
}
}
}
// if no pair exists, print -1
cout << -1 << endl;
}
// driver code
int main()
{
// given array
int arr[] = { 2, 5, 12, 7, 19, 4, 3 };
// size of the array
int n = sizeof(arr) / sizeof(arr[0]);
findsplit(arr, n);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program for the above approach
import java.util.*;
class gfg
{
// function to check if array can
// be split into three equal sum
// subarrays by removing two elements
static void findsplit(int arr[], int n)
{
for (int l = 1; l <= n - 4; l )
{
for (int r = l 2; r <= n - 2; r )
{
// stores sum of all three subarrays
int lsum = 0, rsum = 0, msum = 0;
// sum of left subarray
for (int i = 0; i <= l - 1; i ) {
lsum = arr[i];
}
// sum of middle subarray
for (int i = l 1; i <= r - 1; i ) {
msum = arr[i];
}
// sum of right subarray
for (int i = r 1; i < n; i ) {
rsum = arr[i];
}
// check if sum of subarrays are equal
if (lsum == rsum && rsum == msum) {
// print the possible pair
system.out.println( l " " r );
return;
}
}
}
// if no pair exists, print -1
system.out.print(-1 );
}
// driver code
public static void main(string[] args)
{
// given array
int arr[] = { 2, 5, 12, 7, 19, 4, 3 };
// size of the array
int n = arr.length;
findsplit(arr, n);
}
}
// this code is contributed by sanjoy_62.
python 3
# pyhton 3 program for the above approach
# function to check if array can
# be split into three equal sum
# subarrays by removing two elements
def findsplit(arr, n):
for l in range(1, n - 3, 1):
for r in range(l 2, n - 1, 1):
# stores sum of all three subarrays
lsum = 0
rsum = 0
msum = 0
# sum of left subarray
for i in range(0, l, 1):
lsum = arr[i]
# sum of middle subarray
for i in range(l 1, r, 1):
msum = arr[i]
# sum of right subarray
for i in range(r 1, n, 1):
rsum = arr[i]
# check if sum of subarrays are equal
if (lsum == rsum and rsum == msum):
# print the possible pair
print(l, r)
return
# if no pair exists, print -1
print(-1)
# driver code
if __name__ == '__main__':
# given array
arr = [2, 5, 12, 7, 19, 4, 3]
# size of the array
n = len(arr)
findsplit(arr, n)
# this code is contributed by surendra_gangwar.
c
// c# program for the above approach
using system;
class gfg
{
// function to check if array can
// be split into three equal sum
// subarrays by removing two elements
static void findsplit(int []arr, int n)
{
for (int l = 1; l <= n - 4; l )
{
for (int r = l 2; r <= n - 2; r )
{
// stores sum of all three subarrays
int lsum = 0, rsum = 0, msum = 0;
// sum of left subarray
for (int i = 0; i <= l - 1; i ) {
lsum = arr[i];
}
// sum of middle subarray
for (int i = l 1; i <= r - 1; i ) {
msum = arr[i];
}
// sum of right subarray
for (int i = r 1; i < n; i ) {
rsum = arr[i];
}
// check if sum of subarrays are equal
if (lsum == rsum && rsum == msum) {
// print the possible pair
console.writeline( l " " r );
return;
}
}
}
// if no pair exists, print -1
console.write(-1 );
}
// driver code
public static void main(string[] args)
{
// given array
int []arr = { 2, 5, 12, 7, 19, 4, 3 };
// size of the array
int n = arr.length;
findsplit(arr, n);
}
}
// this code is contributed by ankthon
java 描述语言
output:
2 4
时间复杂度:o(n3) 辅助空间: o(1)
高效方法:对上述方法进行优化,思路是利用在恒定时间内找到所有子阵和。按照以下步骤解决问题:
- 初始化一个大小为 n 的 和来存储数组的前缀和。
- 初始化两个变量,比如说 l & r、来存储两个要删除的索引,以便将数组拆分成 3 个相等和的子数组。
- 三个子阵列的总和将是总和【l–1】、总和【r–1】–总和【l】和总和【n–1】–总和【r】。
- 使用变量 l: 迭代范围【1,n–4】
- 使用变量 r 迭代范围【l 2,n–2】,并检查在任何一点,左子阵和是否等于中子阵和和右子阵和,然后打印 l & r 的值并返回。
- 如果不存在这样的配对,打印 -1。
下面是上述方法的实现:
c
// c program for the above approach
#include
using namespace std;
// function to check if array can
// be split into three equal sum
// subarrays by removing a pair
void findsplit(int arr[], int n)
{
// stores prefix sum array
vector sum(n);
// copy array elements
for (int i = 0; i < n; i ) {
sum[i] = arr[i];
}
// traverse the array
for (int i = 1; i < n; i ) {
sum[i] = sum[i - 1];
}
for (int l = 1; l <= n - 4; l ) {
for (int r = l 2; r <= n - 2; r ) {
// stores sums of all three subarrays
int lsum = 0, rsum = 0, msum = 0;
// sum of left subarray
lsum = sum[l - 1];
// sum of middle subarray
msum = sum[r - 1] - sum[l];
// sum of right subarray
rsum = sum[n - 1] - sum[r];
// check if sum of subarrays are equal
if (lsum == rsum && rsum == msum) {
// print the possible pair
cout << l << " " << r << endl;
return;
}
}
}
// if no such pair exists, print -1
cout << -1 << endl;
}
// driver code
int main()
{
// given array
int arr[] = { 2, 5, 12, 7, 19, 4, 3 };
// size of the array
int n = sizeof(arr) / sizeof(arr[0]);
findsplit(arr, n);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program for the above approach
import java.util.*;
class gfg
{
// function to check if array can
// be split into three equal sum
// subarrays by removing a pair
static void findsplit(int arr[], int n)
{
// stores prefix sum array
int []sum = new int[n];
// copy array elements
for (int i = 0; i < n; i )
{
sum[i] = arr[i];
}
// traverse the array
for (int i = 1; i < n; i )
{
sum[i] = sum[i - 1];
}
for (int l = 1; l <= n - 4; l ) {
for (int r = l 2; r <= n - 2; r ) {
// stores sums of all three subarrays
int lsum = 0, rsum = 0, msum = 0;
// sum of left subarray
lsum = sum[l - 1];
// sum of middle subarray
msum = sum[r - 1] - sum[l];
// sum of right subarray
rsum = sum[n - 1] - sum[r];
// check if sum of subarrays are equal
if (lsum == rsum && rsum == msum) {
// print the possible pair
system.out.print(l " " r "\n");
return;
}
}
}
// if no such pair exists, print -1
system.out.print(-1 "\n");
}
// driver code
public static void main(string[] args)
{
// given array
int arr[] = { 2, 5, 12, 7, 19, 4, 3 };
// size of the array
int n = arr.length;
findsplit(arr, n);
}
}
// this code is contributed by shikhasingrajput
python 3
# python3 program for the above approach
# function to check if array can
# be split into three equal sum
# subarrays by removing a pair
def findsplit(arr, n):
# stores prefix sum array
sum = [i for i in arr]
# traverse the array
for i in range(1, n):
sum[i] = sum[i - 1]
for l in range(1, n - 3):
for r in range(l 2, n - 1):
# stores sums of all three subarrays
lsum , rsum , msum =0, 0, 0
# sum of left subarray
lsum = sum[l - 1]
# sum of middle subarray
msum = sum[r - 1] - sum[l]
# sum of right subarray
rsum = sum[n - 1] - sum[r]
# check if sum of subarrays are equal
if (lsum == rsum and rsum == msum):
# print possible pair
print(l, r)
return
# if no such pair exists, print -1
print (-1)
# driver code
if __name__ == '__main__':
# given array
arr = [2, 5, 12, 7, 19, 4, 3 ]
# size of the array
n = len(arr)
findsplit(arr, n)
# this code is contributed by mohit kumar 29.
c
// c# program for the above approach
using system;
public class gfg
{
// function to check if array can
// be split into three equal sum
// subarrays by removing a pair
static void findsplit(int []arr, int n)
{
// stores prefix sum array
int []sum = new int[n];
// copy array elements
for (int i = 0; i < n; i )
{
sum[i] = arr[i];
}
// traverse the array
for (int i = 1; i < n; i )
{
sum[i] = sum[i - 1];
}
for (int l = 1; l <= n - 4; l ) {
for (int r = l 2; r <= n - 2; r ) {
// stores sums of all three subarrays
int lsum = 0, rsum = 0, msum = 0;
// sum of left subarray
lsum = sum[l - 1];
// sum of middle subarray
msum = sum[r - 1] - sum[l];
// sum of right subarray
rsum = sum[n - 1] - sum[r];
// check if sum of subarrays are equal
if (lsum == rsum && rsum == msum) {
// print the possible pair
console.write(l " " r "\n");
return;
}
}
}
// if no such pair exists, print -1
console.write(-1 "\n");
}
// driver code
public static void main(string[] args)
{
// given array
int []arr = { 2, 5, 12, 7, 19, 4, 3 };
// size of the array
int n = arr.length;
findsplit(arr, n);
}
}
// this code is contributed by 29ajaykumar
java 描述语言
output:
2 4
时间复杂度:o(n2) 辅助空间: o(n)
最佳方案:最佳方案是在使用的同时,使用。按照以下步骤解决问题:
- 初始化一个大小为 n 的向量来存储数组的前缀和。
- 初始化两个变量,比如说 l & r ,使用遍历数组。
- 遍历数组直到 l < r 或者直到所有三个和都相等:
- 如果左子阵列的总和大于右子阵列的总和,则在右子阵列上增加一个额外的元素。因此,将 r 的值减少 1 。
- 如果右子阵列的总和大于左子阵列的总和,则在左子阵列上添加一个元素。因此,用 1 增加 l 。
- 如果左右子阵列之和相等,但不等于中间子阵列之和,则增加 1 的 l ,减少 r 的 1 。
- 如果不存在这样的配对,打印 -1。
下面是上述方法的实现:
c
// c program for the above approach
#include
using namespace std;
// function to check if array can
// be split into three equal sum
// subarrays by removing a pair
void findsplit(int arr[], int n)
{
// two pointers l and r
int l = 1, r = n - 2;
int lsum, msum, rsum;
// stores prefix sum array
vector sum(n);
sum[0] = arr[0];
// traverse the array
for (int i = 1; i < n; i ) {
sum[i] = sum[i - 1] arr[i];
}
// two pointer approach
while (l < r) {
// sum of left subarray
lsum = sum[l - 1];
// sum of middle subarray
msum = sum[r - 1] - sum[l];
// sum of right subarray
rsum = sum[n - 1] - sum[r];
// print split indices if sum is equal
if (lsum == msum and msum == rsum) {
cout << l << " " << r << endl;
return;
}
// move left pointer if lsum < rsum
if (lsum < rsum)
l ;
// move right pointer if rsum > lsum
else if (lsum > rsum)
r--;
// move both pointers if lsum = rsum
// but they are not equal to msum
else {
l ;
r--;
}
}
// if no possible pair exists, print -1
cout << -1 << endl;
}
// driver code
int main()
{
// given array
int arr[] = { 2, 5, 12, 7, 19, 4, 3 };
// size of the array
int n = sizeof(arr) / sizeof(arr[0]);
findsplit(arr, n);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program for the above approach
public class gfg
{
// function to check if array can
// be split into three equal sum
// subarrays by removing a pair
static void findsplit(int []arr, int n)
{
// two pointers l and r
int l = 1, r = n - 2;
int lsum, msum, rsum;
// stores prefix sum array
int sum[] = new int[n];
sum[0] = arr[0];
// traverse the array
for (int i = 1; i < n; i ) {
sum[i] = sum[i - 1] arr[i];
}
// two pointer approach
while (l < r) {
// sum of left subarray
lsum = sum[l - 1];
// sum of middle subarray
msum = sum[r - 1] - sum[l];
// sum of right subarray
rsum = sum[n - 1] - sum[r];
// print split indices if sum is equal
if (lsum == msum && msum == rsum) {
system.out.println(l " " r);
return;
}
// move left pointer if lsum < rsum
if (lsum < rsum)
l ;
// move right pointer if rsum > lsum
else if (lsum > rsum)
r--;
// move both pointers if lsum = rsum
// but they are not equal to msum
else {
l ;
r--;
}
}
// if no possible pair exists, print -1
system.out.println(-1);
}
// driver code
public static void main (string[] args)
{
// given array
int []arr = { 2, 5, 12, 7, 19, 4, 3 };
// size of the array
int n = arr.length;
findsplit(arr, n);
}
}
// this code is contributed by ankthon
python 3
# python3 program for the above approach
# function to check if array can
# be split into three equal sum
# subarrays by removing a pair
def findsplit(arr, n) :
# two pointers l and r
l = 1; r = n - 2;
# stores prefix sum array
sum = [0]*n;
sum[0] = arr[0];
# traverse the array
for i in range(1, n) :
sum[i] = sum[i - 1] arr[i];
# two pointer approach
while (l < r) :
# sum of left subarray
lsum = sum[l - 1];
# sum of middle subarray
msum = sum[r - 1] - sum[l];
# sum of right subarray
rsum = sum[n - 1] - sum[r];
# print split indices if sum is equal
if (lsum == msum and msum == rsum) :
print(l,r);
return;
# move left pointer if lsum < rsum
if (lsum < rsum) :
l = 1;
# move right pointer if rsum > lsum
elif (lsum > rsum) :
r -= 1;
# move both pointers if lsum = rsum
# but they are not equal to msum
else :
l = 1;
r -= 1;
# if no possible pair exists, print -1
print(-1);
# driver code
if __name__ == "__main__" :
# given array
arr = [ 2, 5, 12, 7, 19, 4, 3 ];
# size of the array
n = len(arr);
findsplit(arr, n);
# this code is contributed by ankthon
c
// c# program for the above approach
using system;
class gfg {
// function to check if array can
// be split into three equal sum
// subarrays by removing a pair
static void findsplit(int[] arr, int n)
{
// two pointers l and r
int l = 1, r = n - 2;
int lsum, msum, rsum;
// stores prefix sum array
int[] sum = new int[n];
sum[0] = arr[0];
// traverse the array
for (int i = 1; i < n; i ) {
sum[i] = sum[i - 1] arr[i];
}
// two pointer approach
while (l < r) {
// sum of left subarray
lsum = sum[l - 1];
// sum of middle subarray
msum = sum[r - 1] - sum[l];
// sum of right subarray
rsum = sum[n - 1] - sum[r];
// print split indices if sum is equal
if (lsum == msum && msum == rsum) {
console.write(l " " r);
return;
}
// move left pointer if lsum < rsum
if (lsum < rsum)
l ;
// move right pointer if rsum > lsum
else if (lsum > rsum)
r--;
// move both pointers if lsum = rsum
// but they are not equal to msum
else {
l ;
r--;
}
}
// if no possible pair exists, print -1
console.write(-1);
}
static void main()
{
// given array
int[] arr = { 2, 5, 12, 7, 19, 4, 3 };
// size of the array
int n = arr.length;
findsplit(arr, n);
}
}
// this code is contributed by rameshtravel07.
java 描述语言
output:
2 4
时间复杂度:o(n) t5辅助空间:** o(n)
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处