根据给定条件减少数组后查找所有可能的唯一索引
给定一个包含N个整数的数组arr和一个键数组,使得 arr 中每个元素的键最初等于它在 arr 中的索引。假设一个人可以执行某些移动,在一次移动中,选择两个索引(i, j) ,从数组中删除相应的元素,并添加一个等于删除元素总和的新元素( arr [i] + arr [j] ),其键等于 arr[i] 和 arr[j] 中较大元素的键。如果两个元素相等,则可以使用两者中的任何键。当数组只包含一个元素时,任务是在最后打印所有可能的唯一键。
例子:
Input: N = 3, arr[] = {2, 3, 4}
Output: 1, 2
Explanation: Initially all elements have keys equal to their index. (2->0, 3->1, 4->2).
Case 1:
- Remove 2 and 3 and insert 5 with key 1. The new array will be (5->1, 4->2)
- Remove 5 and 4 and insert 9 with key 1. The new array will be (9->1)
- The final index in the array will be 1 for these operations.
Case 2:
- Remove 2 and 4 and insert 6 with key 2. The new array will be (3->1, 6->2)
- Remove 6 and 3 and insert 9 with key 2. The new array will be (9->2)
- The final index in the array will be 2 for these operations.
So there are a total of 2 possible unique keys (1 and 2).
Input: N = 3, arr[] = {1, 1, 4}
Output: 2
Explanation: The final index left will be 2 in all cases. So there is a total of 1 possible index.
方法:维护一个向量对,它将 是 为每个arr[i]存储{value, i}和一个变量 sum ,它将存储左侧部分的剩余总和。我们以非降序对向量进行排序 有价值的 和 从末端开始遍历。每当左边部分的剩余总和 数组小于当前元素的值,这意味着左边的所有元素都不能成为答案,所以我们从这里中断循环,因为左边的那些元素最后会被右侧较大的元素。
按照以下步骤作为上述方法的算法:
- 维护一个pair vector ,以{value, index}的形式存储数组的元素
- 以非递减值的顺序对对向量进行排序。
- 取一个变量sum ,它将最初存储数组中所有元素的总和。
- 在已排序的向量中从右到左迭代。
- 如果当前元素的值大于其左侧所有元素的总和,则中断循环,否则,将当前索引添加为有效答案,并将总和减去当前元素的值并继续剩余元素.
- 最后返回答案中所有可能的索引。
下面是上述方法的实现:
C++
// C++ implementation for the above approach
#include
using namespace std;
// Function to calculate
// total possible different indexes
// after all possible operations mentioned
int totalFinalIndexes(int A[], int N)
{
// Variable to calculate possible indexes
int res = 0;
// Variable to store the total sum
int sum = 0;
// vector to store total possible indexes
vector ans;
// Calculat the sum and push them in a
// pair vector with their indices.
vector > elements;
for (int i = 0; i < N; i++) {
sum += A[i];
elements.push_back(make_pair(A[i], i));
}
sort(elements.begin(), elements.end());
// Iterate from right to left
// and calculate total possible indexes
for (int i = N - 1; i >= 0; i--) {
// increment the current index.
res++;
// Decrease the sum
sum -= elements[i].first;
// Push the current index
// in the ans vector
ans.push_back(elements[i].second);
// All other indexes
// cannot be the possible answers
if (sum < elements[i].first)
break;
}
// Print the indexes of the values
for (auto x : ans) {
cout << x << " ";
}
return 0;
}
// Driver Code
int main()
{
int N = 3;
int A[] = { 2, 3, 4 };
totalFinalIndexes(A, N);
}
Java
// Java implementation for the above approach
import java.util.*;
class GFG{
static class pair
{
int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// Function to calculate
// total possible different indexes
// after all possible operations mentioned
static int totalFinalIndexes(int A[], int N)
{
// Variable to calculate possible indexes
int res = 0;
// Variable to store the total sum
int sum = 0;
// vector to store total possible indexes
Vector ans = new Vector();
// Calculat the sum and push them in a
// pair vector with their indices.
Vector elements = new Vector();
for (int i = 0; i < N; i++) {
sum += A[i];
elements.add(new pair(A[i], i));
}
Collections.sort(elements,(a,b)->a.first-b.first);
// Iterate from right to left
// and calculate total possible indexes
for (int i = N - 1; i >= 0; i--) {
// increment the current index.
res++;
// Decrease the sum
sum -= elements.get(i).first;
// Push the current index
// in the ans vector
ans.add(elements.get(i).second);
// All other indexes
// cannot be the possible answers
if (sum < elements.get(i).first)
break;
}
// Print the indexes of the values
for (int x : ans) {
System.out.print(x+ " ");
}
return 0;
}
// Driver Code
public static void main(String[] args)
{
int N = 3;
int A[] = { 2, 3, 4 };
totalFinalIndexes(A, N);
}
}
// This code is contributed by shikhasingrajput
Python3
# python implementation for the above approach
# Function to calculate
# total possible different indexes
# after all possible operations mentioned
def totalFinalIndexes(A, N):
# Variable to calculate possible indexes
res = 0
# Variable to store the total sum
sum = 0
# vector to store total possible indexes
ans = []
# Calculat the sum and push them in a
# pair vector with their indices.
elements = []
for i in range(0, N):
sum += A[i]
elements.append([A[i], i])
elements.sort()
# Iterate from right to left
# and calculate total possible indexes
for i in range(N-1, -1, -1):
# increment the current index.
res = res + 1
# Decrease the sum
sum -= elements[i][0]
# Push the current index
# in the ans vector
ans.append(elements[i][1])
# All other indexes
# cannot be the possible answers
if (sum < elements[i][0]):
break
# Print the indexes of the values
for x in ans:
print(x, end=" ")
return 0
# Driver Code
if __name__ == "__main__":
N = 3
A = [2, 3, 4]
totalFinalIndexes(A, N)
# This code is contributed by rakeshsahni
C#
// C# implementation for the above approach
using System;
using System.Collections.Generic;
public class GFG{
class pair : IComparable
{
public int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
public int CompareTo(pair p)
{
return this.first-p.second;
}
}
// Function to calculate
// total possible different indexes
// after all possible operations mentioned
static int totalFinalIndexes(int []A, int N)
{
// Variable to calculate possible indexes
int res = 0;
// Variable to store the total sum
int sum = 0;
// vector to store total possible indexes
List ans = new List();
// Calculat the sum and push them in a
// pair vector with their indices.
List elements = new List();
for (int i = 0; i < N; i++) {
sum += A[i];
elements.Add(new pair(A[i], i));
}
elements.Sort();
elements.Reverse();
// Iterate from right to left
// and calculate total possible indexes
for (int i = N - 1; i >= 0; i--)
{
// increment the current index.
res++;
// Decrease the sum
sum -= elements[i].first;
// Push the current index
// in the ans vector
ans.Add(elements[i].second);
// All other indexes
// cannot be the possible answers
if (sum < elements[i].first)
break;
}
// Print the indexes of the values
foreach (int x in ans) {
Console.Write(x+ " ");
}
return 0;
}
// Driver Code
public static void Main(String[] args)
{
int N = 3;
int []A = { 2, 3, 4 };
totalFinalIndexes(A, N);
}
}
// This code is contributed by shikhasingrajput
Javascript
2 1
时间复杂度: O(N*log(N))
辅助空间: O(N)