Minimum K for which odd operations remove at least half of N
Last Updated :
23 Dec, 2023
Given integer N (1 <= N <= 1018). Perform the following operation until N is not zero, In odd operation remove K from N (if N is less than K remove all N). In an even operation remove 10% of N from the remaining N. The task for this problem is to find the minimum integer K for which the total N removed in odd operations is at least half of N.
Examples:
Input: N = 68
Output: 3
Explanation: Initially we have N = 68 if we choose K = 3
- In the first operation, we subtract K from N, and it becomes 65
- In the second operation we subtract 10% of N which is 6 (it should be 6.5 but we are only considering the integer part) after subtracting 6 from N, it becomes 59
- In the third operation, we subtract K from N, and it becomes 56
- In the fourth operation we subtract 10% of N which is 5(it should be 5.6 but we are only considering the integer part) after subtracting 5 from N, it becomes 51
- In the fifth operation, we subtract K from N, and it becomes 48
- In the sixth operation, we subtract 10% of N which is 4(it should be 4.8 but we are only considering the integer part) after subtracting 4 from N, it becomes 44
- In the seventh operation, we subtract K from N, and it becomes 41
- In the eighth operation we subtract 10% of N which is 4(it should be 4.1 but we are only considering the integer part) after subtracting 4 from N, it becomes 37
- In the ninth operation, we subtract K from N, and it becomes 34
- In the tenth operation we subtract 10% of N which is 3(it should be 3.4 but we are only considering the integer part) after subtracting 3 from N, it becomes 31
- In the 11th operation we subtract K from N, it becomes 28
- In 12th operation, we subtract 10% of N which is 2(it should be 2.8 but we are only considering integer part) after subtracting 2 from N, it becomes 26
- In the 13th operation we subtract K from N, it becomes 23
- In 14th operation we subtract 10% of N which is 2 (it should be 2.3 but we are only considering integer part) after subtracting 2 from N, it becomes 21
- In 15th operation we subtract K from N, it becomes 18
- In 16th operation we subtract 10% of N which is 1(it should be 1.8 but we are only considering integer part) after subtracting 1 from N, it becomes 17
- In 17th operation we subtract K from N, it becomes 14
- In 18th operation we subtract 10% of N which is 1(it should be 1.4 but we are only considering integer part) after subtracting 1 from N, it becomes 13
- In 19th operation we subtract K from N, it becomes 10
- In 20th operation we subtract 10% of N which is 1 after subtracting 1 from N, it becomes 9
- In 21st operation we subtract K from N, it becomes 6
- In 22nd operation we subtract 10% of N which is 0(it should be 0.6 but we are only considering integer part) after subtracting 0 from N, it remains 6
- In 23rd operation we subtract K from N, it becomes 3
- In 24th operation we subtract 10% of N which is 0(it should be 0.3 but we are only considering integer part) after subtracting 0 from N, it remains 3
- In 25th operation we subtract K from N, it becomes 0.
The total N removed in odd operations is 39 which is at least half of N
Input: N = 9
Output: 1
Efficient Approach: To solve the problem follow the below idea:
Binary Search can be used to solve this problem and the range of binary search will be 1 to N / 2 is monotonic function represents whether at least half of N removed in odd operations with K. it is of the form FFFFFFFTTTTTT. we have to find when the first time function is true using Binary Search.
Below are the steps for the above approach:
- Set low and high range of binary serach.
- isKpos(K, N) function used to check whether if K used for subtracting in odd operations removes at least half of N or not.
- Run while loop till high and low are not adjacent.
- In while loop find middle element and store it in mid variable.
- Check if that mid can be used as K for subtracting in odd operations to remove at least half of N using isKpos() function. If it is true then set high = mid else low = mid + 1.
- After loop ends if isKpos() true for low then return low else return high.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isKpos( int K, int N)
{
int oddTotalRemoved = 0;
int curN = N;
while (curN > 0) {
oddTotalRemoved += min(K, curN);
curN -= min(curN, K);
curN = curN - curN / 10;
}
return oddTotalRemoved * 2 >= N;
}
int findMinimumK( int N)
{
int low = 1, high = (N / 2) + 1;
while (high - low > 1) {
int mid = (low + high) / 2;
if (isKpos(mid, N)) {
high = mid;
}
else {
low = mid + 1;
}
}
if (isKpos(low, N))
return low;
else
return high;
}
int32_t main()
{
int N = 68;
cout << findMinimumK(N) << endl;
int N1 = 9;
cout << findMinimumK(N1) << endl;
return 0;
}
|
Java
import java.util.*;
public class MinimumK {
static boolean isKpos( int K, int N) {
int oddTotalRemoved = 0 ;
int curN = N;
while (curN > 0 ) {
oddTotalRemoved += Math.min(K, curN);
curN -= Math.min(curN, K);
curN = curN - curN / 10 ;
}
return oddTotalRemoved * 2 >= N;
}
static int findMinimumK( int N) {
int low = 1 , high = (N / 2 ) + 1 ;
while (high - low > 1 ) {
int mid = (low + high) / 2 ;
if (isKpos(mid, N)) {
high = mid;
} else {
low = mid + 1 ;
}
}
if (isKpos(low, N))
return low;
else
return high;
}
public static void main(String[] args) {
int N = 68 ;
System.out.println(findMinimumK(N));
int N1 = 9 ;
System.out.println(findMinimumK(N1));
}
}
|
Python
def isKpos(K, N):
oddTotalRemoved = 0
curN = N
while curN > 0 :
oddTotalRemoved + = min (K, curN)
curN - = min (curN, K)
curN = curN - curN / / 10
return oddTotalRemoved * 2 > = N
def findMinimumK(N):
low, high = 1 , (N / / 2 ) + 1
while high - low > 1 :
mid = (low + high) / / 2
if isKpos(mid, N):
high = mid
else :
low = mid + 1
if isKpos(low, N):
return low
else :
return high
N = 68
print (findMinimumK(N))
N1 = 9
print (findMinimumK(N1))
|
C#
using System;
class GFG
{
static bool isKpos( int K, int N)
{
int oddTotalRemoved = 0;
int curN = N;
while (curN > 0)
{
oddTotalRemoved += Math.Min(K, curN);
curN -= Math.Min(curN, K);
curN = curN - curN / 10;
}
return oddTotalRemoved * 2 >= N;
}
static int findMinimumK( int N)
{
int low = 1, high = (N / 2) + 1;
while (high - low > 1)
{
int mid = (low + high) / 2;
if (isKpos(mid, N))
{
high = mid;
}
else
{
low = mid + 1;
}
}
if (isKpos(low, N))
return low;
else
return high;
}
public static void Main()
{
int N = 68;
Console.WriteLine(findMinimumK(N));
int N1 = 9;
Console.WriteLine(findMinimumK(N1));
}
}
|
Javascript
function isKpos(K, N) {
let oddTotalRemoved = 0;
let curN = N;
while (curN > 0) {
oddTotalRemoved += Math.min(K, curN);
curN -= Math.min(curN, K);
curN = curN - Math.floor(curN / 10);
}
return oddTotalRemoved * 2 >= N;
}
function findMinimumK(N) {
let low = 1, high = Math.floor(N / 2) + 1;
while (high - low > 1) {
let mid = Math.floor((low + high) / 2);
if (isKpos(mid, N)) {
high = mid;
} else {
low = mid + 1;
}
}
if (isKpos(low, N)) {
return low;
} else {
return high;
}
}
let N = 68;
console.log(findMinimumK(N));
let N1 = 9;
console.log(findMinimumK(N1));
|
Time Complexity: O(N*logN)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...