📌  相关文章
📜  找到要删除的最小元素的索引,以使数组的和可被K整除

📅  最后修改于: 2021-04-17 14:15:07             🧑  作者: Mango

给定大小为N的数组arr []和正整数K ,任务是找到需要删除的最小数组元素的索引,以使剩余数组的总和可被K整除。如果存在多个解决方案,则打印最小的索引。否则,打印-1

例子:

天真的方法:解决此问题的最简单方法是遍历数组并通过从数组中删除当前元素来计算总和。如果获得的总和可被K整除,则打印当前索引。否则,将删除的元素插入数组。

时间复杂度: O(N 2 )
辅助空间: O(1)

方法:可以通过预先计算数组的总和来优化上述方法。最后,遍历数组并检查(sum – arr [i])是否可被K整除。如果发现为真,则打印当前索引。请按照以下步骤解决问题:

  • 计算数组的总和并将其存储在变量中,例如sum。
  • 初始化两个变量,例如resmini,以存储最小元素和元素的索引,以便删除元素使总和可被K整除。
  • 遍历数组,并检查(sum – arr [i])是否可被K整除。如果发现为真,则检查arr [i]是否小于mini 。如果发现为真,则更新mini = arr [i]res = i
  • 最后,打印res

下面是上述方法的实现:

C++14
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to find index of the smallest array element
// required to be removed to make sum divisible by K
int findIndex(int arr[], int n, int K)
{
 
    // Stores sum of array elements
    int sum = 0;
 
    // Stores index of the smallest element
    // removed from the array to make sum
    // divisible by K
    int res = -1;
 
    // Stores the smallest element removed
    // from the array to make sum divisible by K
    int mini = 1e9;
 
    // Traverse the array, arr[]
    for (int i = 0; i < n; i++) {
 
        // Update sum
        sum += arr[i];
    }
 
    // Traverse the array arr[]
    for (int i = 0; i < n; i++) {
 
        // Calculate remaining sum
        int temp = sum - arr[i];
 
        // If sum is divisible by K
        if (temp % K == 0) {
 
            // If res ==-1 or mini is greater
            // than arr[i]
            if (res == -1 || mini > arr[i]) {
 
                // Update res and mini
                res = i + 1;
                mini = arr[i];
            }
        }
    }
 
    return res;
}
 
// Driver Code
int main()
{
    int arr[] = { 14, 7, 8, 2, 4 };
    int K = 7;
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << findIndex(arr, N, K);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
 
// Function to find index of the smallest array element
// required to be removed to make sum divisible by K
static int findIndex(int arr[], int n, int K)
{
 
    // Stores sum of array elements
    int sum = 0;
 
    // Stores index of the smallest element
    // removed from the array to make sum
    // divisible by K
    int res = -1;
 
    // Stores the smallest element removed
    // from the array to make sum divisible by K
    int mini = (int) 1e9;
 
    // Traverse the array, arr[]
    for (int i = 0; i < n; i++)
    {
 
        // Update sum
        sum += arr[i];
    }
 
    // Traverse the array arr[]
    for (int i = 0; i < n; i++)
    {
 
        // Calculate remaining sum
        int temp = sum - arr[i];
 
        // If sum is divisible by K
        if (temp % K == 0)
        {
 
            // If res ==-1 or mini is greater
            // than arr[i]
            if (res == -1 || mini > arr[i])
            {
 
                // Update res and mini
                res = i + 1;
                mini = arr[i];
            }
        }
    }
    return res;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 14, 7, 8, 2, 4 };
    int K = 7;
    int N = arr.length;
    System.out.print(findIndex(arr, N, K));
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program to implement
# the above approach
 
# Function to find index of the smallest array element
# required to be removed to make sum divisible by K
def findIndex(arr, n, K) :
 
    # Stores sum of array elements
    sum = 0
 
    # Stores index of the smallest element
    # removed from the array to make sum
    # divisible by K
    res = -1
 
    # Stores the smallest element removed
    # from the array to make sum divisible by K
    mini = 1e9
 
    # Traverse the array, arr[]
    for i in range(n):
 
        # Update sum
        sum += arr[i]
 
    # Traverse the array arr[]
    for i in range(n):
 
        # Calculate remaining sum
        temp = sum - arr[i]
 
        # If sum is divisible by K
        if (temp % K == 0) :
 
            # If res ==-1 or mini is greater
            # than arr[i]
            if (res == -1 or mini > arr[i]) :
 
                # Update res and mini
                res = i + 1
                mini = arr[i]
    return res;
 
# Driver Code
arr = [ 14, 7, 8, 2, 4 ]
K = 7
N = len(arr)
print(findIndex(arr, N, K))
 
# This code is contributed by sanjoy_62.


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
     
// Function to find index of the smallest
// array element required to be removed
// to make sum divisible by K
static int findIndex(int[] arr, int n, int K)
{
     
    // Stores sum of array elements
    int sum = 0;
   
    // Stores index of the smallest
    // element removed from the array
    // to make sum divisible by K
    int res = -1;
   
    // Stores the smallest element
    // removed from the array to
    // make sum divisible by K
    int mini = (int)1e9;
   
    // Traverse the array, arr[]
    for(int i = 0; i < n; i++)
    {
         
        // Update sum
        sum += arr[i];
    }
   
    // Traverse the array arr[]
    for(int i = 0; i < n; i++)
    {
         
        // Calculate remaining sum
        int temp = sum - arr[i];
   
        // If sum is divisible by K
        if (temp % K == 0)
        {
             
            // If res ==-1 or mini is greater
            // than arr[i]
            if (res == -1 || mini > arr[i])
            {
                 
                // Update res and mini
                res = i + 1;
                mini = arr[i];
            }
        }
    }
    return res;
}
 
// Driver code   
static void Main()
{
    int[] arr = { 14, 7, 8, 2, 4 };
    int K = 7;
    int N = arr.Length;
     
    Console.WriteLine(findIndex(arr, N, K)); 
}
}
 
// This code is contributed by divyeshrabadiya07


输出:
2

时间复杂度: O(N)
辅助空间: O(1)