📌  相关文章
📜  K 应添加到任何元素以对给定数组进行排序的最少次数

📅  最后修改于: 2022-05-13 01:56:10.229000             🧑  作者: Mango

K 应添加到任何元素以对给定数组进行排序的最少次数

给定一个由N个整数和整数K组成的未排序数组A[] ,任务是计算应将K添加到数组元素以使数组排序的最小次数。

例子:

方法:可以通过以下思路解决问题:

请按照以下步骤解决问题:

  • 创建两个指针iji从 0 开始, j从 1 开始。
  • 然后运行 while 循环直到i < Nj < N
    • 检查arr[i] <= arr[j]是否是,然后简单地将两个指针都增加1
    • 如果arr[i] > arr[j]然后将整数K添加到arr[j]直到它变得大于或等于arr[i]并且对于每次添加K,将计数增加 1,然后将两个指针都增加1.
  • 最后,返回计数。

下面是上述方法的实现:

C++
// C++ implementation of above approach
 
#include 
using namespace std;
 
// Function to count min no of operations
int minOperation(vector arr,
                 int N, int K)
{
    int i = 0, j = 1, count = 0;
 
    while (i < N && j < N) {
 
        // If current elements are sorted
        // Increment i and j by 1
        if (arr[i] <= arr[j]) {
            i++;
            j++;
        }
 
        // If current elements are not
        // Sorted then add K to arr[j] till
        // It become greater than equal to
        // arr[i] and then increment
        // i and j by 1
        else {
            while (arr[i] > arr[j]) {
                arr[j] += K;
 
                // Increment count in
                // Each operation
                count++;
            }
            i++;
            j++;
        }
    }
 
    return count;
}
 
// Driver Code
int main()
{
    vector arr = { 3, 6, 8, 5, 3 };
    int N = 5, K = 4;
 
    // Function call
    cout << minOperation(arr, N, K);
    return 0;
}


Java
// Java program for above approach
import java.util.*;
class GFG
{
 
  // Function to count min no of operations
  static int minOperation(int[] arr, int N, int K)
  {
    int i = 0, j = 1, count = 0;
 
    while (i < N && j < N) {
 
      // If current elements are sorted
      // Increment i and j by 1
      if (arr[i] <= arr[j]) {
        i++;
        j++;
      }
 
      // If current elements are not
      // Sorted then add K to arr[j] till
      // It become greater than equal to
      // arr[i] and then increment
      // i and j by 1
      else {
        while (arr[i] > arr[j]) {
          arr[j] += K;
 
          // Increment count in
          // Each operation
          count++;
        }
        i++;
        j++;
      }
    }
 
    return count;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int[] arr = { 3, 6, 8, 5, 3 };
    int N = 5, K = 4;
 
    // Function call
    System.out.print(minOperation(arr, N, K));
  }
}
 
// This code is contributed by sanjoy_62.


Python3
# python3 implementation of above approach
 
# Function to count min no of operations
def minOperation(arr, N, K):
 
    i, j, count = 0, 1, 0
 
    while (i < N and j < N):
 
        # If current elements are sorted
        # Increment i and j by 1
        if (arr[i] <= arr[j]):
            i += 1
            j += 1
 
        # If current elements are not
        # Sorted then add K to arr[j] till
        # It become greater than equal to
        # arr[i] and then increment
        # i and j by 1
        else:
            while (arr[i] > arr[j]):
                arr[j] += K
 
                # Increment count in
                # Each operation
                count += 1
 
            i += 1
            j += 1
 
    return count
 
# Driver Code
if __name__ == "__main__":
 
    arr = [3, 6, 8, 5, 3]
    N, K = 5, 4
 
    # Function call
    print(minOperation(arr, N, K))
 
# This code is contributed by rakeshsahni


C#
// C# implementation of above approach
using System;
class GFG {
 
  // Function to count min no of operations
  static int minOperation(int[] arr, int N, int K)
  {
    int i = 0, j = 1, count = 0;
 
    while (i < N && j < N) {
 
      // If current elements are sorted
      // Increment i and j by 1
      if (arr[i] <= arr[j]) {
        i++;
        j++;
      }
 
      // If current elements are not
      // Sorted then add K to arr[j] till
      // It become greater than equal to
      // arr[i] and then increment
      // i and j by 1
      else {
        while (arr[i] > arr[j]) {
          arr[j] += K;
 
          // Increment count in
          // Each operation
          count++;
        }
        i++;
        j++;
      }
    }
 
    return count;
  }
 
  // Driver Code
  public static void Main()
  {
    int[] arr = { 3, 6, 8, 5, 3 };
    int N = 5, K = 4;
 
    // Function call
    Console.Write(minOperation(arr, N, K));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
3

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