📌  相关文章
📜  给定数组中满足给定条件的最小索引

📅  最后修改于: 2021-06-27 01:24:13             🧑  作者: Mango

给定大小为N的数组arr []和整数K ,任务是在数组中找到最小的索引,以便:

如果找不到这样的索引,则打印-1

例子:

方法:对于从0开始的每个索引i ,找到给定序列的总和,并且给定总和大于或等于K的第一个索引就是我们所需的索引。如果没有这样的索引,则打印-1

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the required index
int getIndex(int arr[], int n, int K)
{
 
    // Check all the indices, the first index
    // satisfying the conidtion is
    // the required index
    for (int i = 0; i < n; i++) {
 
        // To store the sum of the series
        int sum = 0;
 
        // Denominator for the sum series
        int den = 1;
 
        // Find the sum of the series
        for (int j = i; j < n; j++) {
            sum += (arr[j] / den);
 
            // Increment the denominator
            den++;
 
            // If current sum is greater than K
            // no need to execute loop further
            if (sum > K)
                break;
        }
 
        // Found the first valid index
        if (sum <= K)
            return i;
    }
 
    return -1;
}
 
// Driver code
int main()
{
    int arr[] = { 6, 5, 4, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int K = 8;
    cout << getIndex(arr, n, K);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG {
 
    // Function to return the required index
    static int getIndex(int arr[], int n, int K)
    {
 
        // Check all the indices, the first index
        // satisfying the conidtion is
        // the required index
        for (int i = 0; i < n; i++) {
 
            // To store the sum of the series
            int sum = 0;
 
            // Denominator for the sum series
            int den = 1;
 
            // Find the sum of the series
            for (int j = i; j < n; j++) {
                sum += (arr[j] / den);
 
                // Increment the denominator
                den++;
 
                // If current sum is greater than K
                // no need to execute loop further
                if (sum > K)
                    break;
            }
 
            // Found the first valid index
            if (sum <= K)
                return i;
        }
 
        return -1;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 6, 5, 4, 2 };
        int n = arr.length;
        int K = 8;
        System.out.print(getIndex(arr, n, K));
    }
}


Python
# Python3 implementation of the approach
def getIndex(array, k):
  n = len(array)
  for ind in range(n):
    sum = 0
    div = 1
    for item in array:
      sum += item//div
      div += 1
      if sum > k:
        break
    if sum <= k:
      return ind
   
  return -1
 
# Driver code
arr = [6, 5, 4, 2]
K = 8
print(getIndex(arr, K))
 
arr = [5, 4, 2, 3, 9, 1, 8, 7]
K = 7
print(getIndex(arr, K))


C#
// C# implementation of the approach
using System;
 
class GFG
{
    // Function to return the required index
    static int getIndex(int []arr, int n, int K)
    {
 
        // Check all the indices, the first index
        // satisfying the conidtion is
        // the required index
        for (int i = 0; i < n; i++)
        {
 
            // To store the sum of the series
            int sum = 0;
 
            // Denominator for the sum series
            int den = 1;
 
            // Find the sum of the series
            for (int j = i; j < n; j++)
            {
                sum += (arr[j] / den);
 
                // Increment the denominator
                den++;
 
                // If current sum is greater than K
                // no need to execute loop further
                if (sum > K)
                    break;
            }
 
            // Found the first valid index
            if (sum <= K)
                return i;
        }
 
        return -1;
    }
 
    // Driver code
    static public void Main ()
    {
         
        int []arr = { 6, 5, 4, 2 };
        int n = arr.Length;
        int K = 8;
        Console.WriteLine(getIndex(arr, n, K));
    }
}
 
// This Code is contributed by ajit.


PHP
 $K)
                break;
        }
 
        // Found the first valid index
        if ($sum <= $K)
            return $i;
    }
 
    return -1;
}
 
// Driver code
$arr = array( 6, 5, 4, 2 );
$n = sizeof($arr);
$K = 8;
 
echo getIndex($arr, $n, $K);
 
// This code is contributed by Ryuga
?>


Javascript


输出:
1

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

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。