找到在给定操作后将保持数组总和的索引
给定一个数组arr[] ,任务是在执行给定操作后打印出具有最高概率保持数组总和的索引:
- 选择任意两个元素arr[i]和arr[j] ,将它们的和存储在变量K
- 在max(arr[i], arr[j])的索引处分配K并在min(arr[i], arr[j])的索引处分配0
这样,在选择完所有元素后,剩下的最后一个元素将存储整个数组的总和。任务是按照保持数组总和的最高可能性的顺序返回数组的索引。
例子:
Input: arr[] = {2, 1, 4}
Output: {2}
Explanation: Choosing of elements can be done in the following ways:
Way 1:
- Choose elements at indices {0, 1}, so arr[] = {3, 0, 4}
- Choose elements at indices {0, 2}, so arr[] = {0, 0, 7}
Way 2:
- Choose elements at indices {0, 2}, so arr[] = {0, 1, 6}
- Choose elements at indices {1, 2}, so arr[] = {0, 0, 7}
Way 3:
- Choose elements at indices {1, 2}, so arr[] = {2, 0, 5}
- Choose elements at indices {0, 2}, so arr[] = {0, 0, 7}
In this case, output = {2}, index 0 and index 1 has zero possibility of holding the sum, so it is excluded.
Input: arr[] = {1, 2, 4, 3}
Output: {1, 2, 3}
方法:可以通过对给定数组元素进行排序来解决该任务,并检查直到第 i-1个元素的总和是否大于直到第 i 个元素的总和,如果大于,则该索引有可能是有效索引.
- 取一个向量对,比如v ,将所有元素与相应的索引一起存储成对,还取一个变量说sum ,它将存储整个数组的总和。
- 以升序对向量进行排序并取一个计数器,例如c,用1进行初始化,因为一个索引总是有可能保存数组的总和。
- 从倒数第二个元素迭代向量并检查直到倒数第二个元素的总和是否大于直到最后的总和(即数组的总和),如果大于,则递增计数器,这意味着它具有保持总和的可能性,否则打破循环,因为它不是有效的索引。
- 取一个向量说ans来存储有效索引并通过从末尾迭代到计数器来存储索引,然后对ans向量进行排序并打印它。
下面是上述方法的实现:
C++
// C++ implementation for the above approach
#include
using namespace std;
// Function to predict the indices which
// will hold the sum of the given array
void predictIndices(int arr[], int N)
{
// Variable for sum of the array
int sum = 0;
// Vector to store the elements along
// with the indices in pair
vector > v;
for (int i = 0; i < N; i++) {
v.push_back({ arr[i], i });
sum += arr[i];
}
// Sort the vector
sort(v.begin(), v.end());
// Take a counter c, initialize
// it with 1
int c = 1;
// Iterate over the vector from the end
// excluding the last element and each time
// update sum by decrementing its value
// by the element in the sorted vector
for (int i = N - 2; i >= 0; i--) {
sum -= v[i + 1].first;
// If element is greater than the sum
// break the loop
if (sum < v[i + 1].first) {
break;
}
// Else increment the counter c
else {
c++;
}
}
// Vector to store all the indices
// which can hold the sum
vector ans;
// Iterate the ans vector in reverse order
// till index is greater or equal to N - c
// and store the indices in ans vector
for (int i = N - 1; i >= N - c; i--) {
ans.push_back(v[i].second);
}
// Sort the ans vector
sort(ans.begin(), ans.end());
// Print the desired indices
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 4, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
predictIndices(arr, N);
return 0;
}
Java
// Java implementation for the above approach
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class GFG {
public static class Pair {
int first = 0;
int second = 0;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
}
// Function to predict the indices which
// will hold the sum of the given array
public static void predictIndices(int arr[], int N)
{
// Variable for sum of the array
int sum = 0;
// Vector to store the elements along
// with the indices in pair
ArrayList v = new ArrayList();
for (int i = 0; i < N; i++) {
v.add(new Pair(arr[i], i));
sum += arr[i];
}
// Sort the vector
Collections.sort(v, new Comparator() {
@Override
public int compare(Pair p1, Pair p2){
return p1.first - p2.first;
}
});
// Take a counter c, initialize
// it with 1
int c = 1;
// Iterate over the vector from the end
// excluding the last element and each time
// update sum by decrementing its value
// by the element in the sorted vector
for (int i = N - 2; i >= 0; i--) {
sum -= v.get(i + 1).first;
// If element is greater than the sum
// break the loop
if (sum < v.get(i + 1).first) {
break;
}
// Else increment the counter c
else {
c++;
}
}
// Vector to store all the indices
// which can hold the sum
ArrayList ans = new ArrayList();
// Iterate the ans vector in reverse order
// till index is greater or equal to N - c
// and store the indices in ans vector
for (int i = N - 1; i >= N - c; i--) {
ans.add(v.get(i).second);
}
// Sort the ans vector
Collections.sort(ans);
// Print the desired indices
for (int i = 0; i < ans.size(); i++) {
System.out.print(ans.get(i) + " ");
}
}
// Driver Code
public static void main(String args[]) {
int arr[] = { 1, 2, 4, 3 };
int N = arr.length;
predictIndices(arr, N);
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# python3 implementation for the above approach
# Function to predict the indices which
# will hold the sum of the given array
def predictIndices(arr, N):
# Variable for sum of the array
sum = 0
# Vector to store the elements along
# with the indices in pair
v = []
for i in range(N):
v.append([arr[i], i])
sum += arr[i]
# Sort the vector
v.sort()
# Take a counter c, initialize
# it with 1
c = 1
# Iterate over the vector from the end
# excluding the last element and each time
# update sum by decrementing its value
# by the element in the sorted vector
for i in range(N - 2, -1, -1):
sum -= v[i + 1][0]
# If element is greater than the sum
# break the loop
if (sum < v[i + 1][0]):
break
# Else increment the counter c
else:
c += 1
# Vector to store all the indices
# which can hold the sum
ans = []
# Iterate the ans vector in reverse order
# till index is greater or equal to N - c
# and store the indices in ans vector
for i in range(N - 1, N - c-1, -1):
ans.append(v[i][1])
# Sort the ans vector
ans.sort()
# Print the desired indices
for i in range(len(ans)):
print(ans[i], end=" ")
# Driver Code
if __name__ == "__main__":
arr = [1, 2, 4, 3]
N = len(arr)
predictIndices(arr, N)
# This code is contributed by ukasp.
C#
// C# implementation for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
class Pair
{
public int first = 0;
public int second = 0;
public Pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// Function to predict the indices which
// will hold the sum of the given array
public static void predictIndices(int[] arr, int N)
{
// Variable for sum of the array
int sum = 0;
// Vector to store the elements along
// with the indices in pair
List v = new List();
for (int i = 0; i < N; i++)
{
v.Add(new Pair(arr[i], i));
sum += arr[i];
}
// Sort the vector
v.Sort((Pair x, Pair y) => x.first.CompareTo(y.first));
// Take a counter c, initialize
// it with 1
int c = 1;
// Iterate over the vector from the end
// excluding the last element and each time
// update sum by decrementing its value
// by the element in the sorted vector
for (int i = N - 2; i >= 0; i--)
{
sum -= v[i + 1].first;
// If element is greater than the sum
// break the loop
if (sum < v[i + 1].first)
{
break;
}
// Else increment the counter c
else
{
c++;
}
}
// Vector to store all the indices
// which can hold the sum
List ans = new List();
// Iterate the ans vector in reverse order
// till index is greater or equal to N - c
// and store the indices in ans vector
for (int i = N - 1; i >= N - c; i--)
{
ans.Add(v[i].second);
}
// Sort the ans vector
ans.Sort();
// Print the desired indices
for (int i = 0; i < ans.Count; i++)
{
Console.Write(ans[i] + " ");
}
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 2, 4, 3 };
int N = arr.Length;
predictIndices(arr, N);
}
}
// This code is contributed by saurabh_jaiswal.
Javascript
输出
1 2 3
时间复杂度: O(NlogN)
辅助空间: O(N)