K 应添加到任何元素以对给定数组进行排序的最少次数
给定一个由N个整数和整数K组成的未排序数组A[] ,任务是计算应将K添加到数组元素以使数组排序的最小次数。
例子:
Input: A[] = {2, 4, 3, 5, 9}, K = 3
Output: 2
Explanation: In the above array element at index 3 is 3 which is less than its previous element.
So, add K with the element and array becomes {2, 4, 6, 5, 9}.
Now element at index 4 is less than its previous so add K.
Final array {2, 4, 6, 8, 9} which is sorted.
Input: A[] = {3, 6, 8, 5, 3}, K = 4
Output: 3
Explanation: Perform operation at index 4 once and twice at index 5.
方法:可以通过以下思路解决问题:
If the array element at index i is greater than that of the element at (i-1) then no addition is required. Otherwise, add K with the value at ith index until it is greater than the (i-1)th element.
请按照以下步骤解决问题:
- 创建两个指针i和j , i从 0 开始, j从 1 开始。
- 然后运行 while 循环直到i < N和j < 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)