📌  相关文章
📜  查找给定数组中的所有良好索引

📅  最后修改于: 2021-04-26 05:43:13             🧑  作者: Mango

给定整数数组A []。任务是打印所有索引i该数组的数组,以便在从数组中删除第i元素后,该数组成为一个好的array

注意事项

  • 如果数组中有一个元素等于所有其他元素的总和,则数组是个好方法
  • 基于1的索引被认为是该数组。

例子

方法:

  1. 创建一个数组A []的散列,该散列存储每个元素的频率以及具有A的每个元素之和的变量和。
  2. 迭代数组,删除每个元素在索引i处的元素0 \leq i \leq n-1
  3. 删除元素后,剩余数组的总和为K,其中K =总和– A [i]。
  4. 我们必须在剩余的数组中找到一个元素K / 2以使其良好。现在让K = K / 2。
  5. 现在,当且仅当满足以下条件时,其余数组才是好的。
    • 如果A [i] == K且hash(K)> 1如果A [i]!= K且hash(K)> 0。
  6. 打印所有此类索引。

下面是上述方法的实现:

C++
// C++ program to find all good indices
// in the given array
  
#include 
using namespace std;
  
// Function to find all good indices
// in the given array
void niceIndices(int A[], int n)
{
    int sum = 0;
  
    // hash to store frequency
    // of each element
    map m;
  
    // Storing frequency of each element
    // and calculating sum simultaneously
    for (int i = 0; i < n; ++i) {
        m[A[i]]++;
        sum += A[i];
    }
  
    for (int i = 0; i < n; ++i) {
        int k = sum - A[i];
        if (k % 2 == 0) {
            k = k >> 1;
  
            // check if array is good after
            // removing i-th index element
            if (m.find(k) != m.end()) {
  
                if ((A[i] == k && m[k] > 1) || (A[i] != k))
                    // print good indices
                    cout << (i + 1) << " ";
            }
        }
    }
}
  
// Driver Code
int main()
{
    int A[] = { 8, 3, 5, 2 };
    int n = sizeof(A) / sizeof(A[0]);
  
    niceIndices(A, n);
  
    return 0;
}


Java
// Java program to find all good indices 
// in the given array 
import java.util.*;
class Solution
{
    
// Function to find all good indices 
// in the given array 
static void niceIndices(int A[], int n) 
{ 
    int sum = 0; 
    
    // hash to store frequency 
    // of each element 
    Map m=new HashMap(); 
      
    // Storing frequency of each element 
    // and calculating sum simultaneously 
    for (int i = 0; i < n; ++i) { 
          
        m.put(A[i],(m.get(A[i])==null)?0:m.get(A[i])+1); 
        sum += A[i]; 
    } 
    
    for (int i = 0; i < n; ++i) { 
        int k = sum - A[i]; 
        if (k % 2 == 0) { 
            k = k >> 1; 
    
            // check if array is good after 
            // removing i-th index element 
            if (m.containsKey(k)) { 
    
                if ((A[i] == k && m.get(k) > 1) || (A[i] != k)) 
                    // print good indices 
                    System.out.print( (i + 1) +" "); 
            } 
        } 
    } 
} 
    
// Driver Code 
public static void main(String args[])
{ 
    int A[] = { 8, 3, 5, 2 }; 
    int n = A.length; 
    
    niceIndices(A, n); 
    
} 
}
//contributed by Arnab Kundu


Python3
# Python3 program to find all good 
# indices in the given array 
from collections import defaultdict
  
# Function to find all good indices 
# in the given array 
def niceIndices(A, n): 
  
    Sum = 0
  
    # hash to store frequency 
    # of each element 
    m = defaultdict(lambda:0)
  
    # Storing frequency of each element 
    # and calculating sum simultaneously 
    for i in range(n): 
        m[A[i]] += 1
        Sum += A[i] 
  
    for i in range(n): 
        k = Sum - A[i] 
          
        if k % 2 == 0:
              
            k = k >> 1
  
            # check if array is good after 
            # removing i-th index element 
            if k in m: 
  
                if ((A[i] == k and m[k] > 1) or 
                    (A[i] != k)): 
                      
                    # print good indices 
                    print((i + 1), end = " ")
  
# Driver Code    
if __name__ == "__main__":
  
    A = [8, 3, 5, 2] 
    n = len(A)
  
    niceIndices(A, n) 
  
# This code is contributed by Rituraj Jain


C#
// C# program to find all good indices 
// in the given array 
using System;
using System.Collections.Generic;
      
class GFG
{
      
// Function to find all good indices 
// in the given array 
static void niceIndices(int []A, int n) 
{ 
    int sum = 0; 
      
    // hash to store frequency 
    // of each element 
    Dictionary mp = new Dictionary();
      
    // Storing frequency of each element 
    // and calculating sum simultaneously 
      
    for (int i = 0 ; i < n; i++)
    {
        if(mp.ContainsKey(A[i]))
        {
            var val = mp[A[i]];
            mp.Remove(A[i]);
            mp.Add(A[i], val + 1);
            sum += A[i];
        }
        else
        {
            mp.Add(A[i], 0);
            sum += A[i];
        }
    }
      
    for (int i = 0; i < n; ++i)
    { 
        int k = sum - A[i]; 
        if (k % 2 == 0) 
        { 
            k = k >> 1; 
      
            // check if array is good after 
            // removing i-th index element 
            if (mp.ContainsKey(k)) 
            { 
      
                if ((A[i] == k && mp[k] > 1) || (A[i] != k)) 
                    // print good indices 
                    Console.Write( (i + 1) +" "); 
            } 
        } 
    } 
} 
      
// Driver Code 
public static void Main(String []args)
{ 
    int []A = { 8, 3, 5, 2 }; 
    int n = A.Length; 
      
    niceIndices(A, n); 
      
} 
}
  
/* This code is contributed by PrinciRaj1992 */


输出:
1 4

时间复杂度: O(N * log(N))