Minimum times K should be added to any element to sort given Array
Given an unsorted array A[] of N integers and integer K, the task is to count the minimum number of times K should be added to the array elements to make the array sorted.
Examples:
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.
Approach: The problem can be solved by using the following idea:
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.
Follow the steps to solve the problem:
- Create two pointers i and j , i starts from 0 and j from 1.
- Then run a while loop till i < N and j < N
- Check if arr[i] <= arr[j] or not if it is then simply increment both the pointers by 1.
- If arr[i] > arr[j] then add integer K to arr[j] until it become greater than or equal to arr[i] and for each addition of K, increment a count by 1 after that increment both the pointers by 1.
- Lastly, return the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOperation(vector< int > arr,
int N, int K)
{
int i = 0, j = 1, count = 0;
while (i < N && j < N) {
if (arr[i] <= arr[j]) {
i++;
j++;
}
else {
while (arr[i] > arr[j]) {
arr[j] += K;
count++;
}
i++;
j++;
}
}
return count;
}
int main()
{
vector< int > arr = { 3, 6, 8, 5, 3 };
int N = 5, K = 4;
cout << minOperation(arr, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int minOperation( int [] arr, int N, int K)
{
int i = 0 , j = 1 , count = 0 ;
while (i < N && j < N) {
if (arr[i] <= arr[j]) {
i++;
j++;
}
else {
while (arr[i] > arr[j]) {
arr[j] += K;
count++;
}
i++;
j++;
}
}
return count;
}
public static void main(String[] args)
{
int [] arr = { 3 , 6 , 8 , 5 , 3 };
int N = 5 , K = 4 ;
System.out.print(minOperation(arr, N, K));
}
}
|
Python3
def minOperation(arr, N, K):
i, j, count = 0 , 1 , 0
while (i < N and j < N):
if (arr[i] < = arr[j]):
i + = 1
j + = 1
else :
while (arr[i] > arr[j]):
arr[j] + = K
count + = 1
i + = 1
j + = 1
return count
if __name__ = = "__main__" :
arr = [ 3 , 6 , 8 , 5 , 3 ]
N, K = 5 , 4
print (minOperation(arr, N, K))
|
C#
using System;
class GFG {
static int minOperation( int [] arr, int N, int K)
{
int i = 0, j = 1, count = 0;
while (i < N && j < N) {
if (arr[i] <= arr[j]) {
i++;
j++;
}
else {
while (arr[i] > arr[j]) {
arr[j] += K;
count++;
}
i++;
j++;
}
}
return count;
}
public static void Main()
{
int [] arr = { 3, 6, 8, 5, 3 };
int N = 5, K = 4;
Console.Write(minOperation(arr, N, K));
}
}
|
Javascript
<script>
function minOperation( arr,
N, K)
{
let i = 0, j = 1, count = 0;
while (i < N && j < N) {
if (arr[i] <= arr[j]) {
i++;
j++;
}
else {
while (arr[i] > arr[j]) {
arr[j] += K;
count++;
}
i++;
j++;
}
}
return count;
}
let arr = [3, 6, 8, 5, 3];
let N = 5, K = 4;
document.write(minOperation(arr, N, K));
</script>
|
Time complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
24 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...