Given an integer n. find an integer k for which n % k is the largest where 1 ≤ k < n.
Examples :
Input: n = 3
Output: 2
Explanation:
3 % 1 = 0
3 % 2 = 1
So, the modulo is highest for 2.
Input: n = 4
Output: 3
Explanation:
4 % 1 = 0
4 % 2 = 0
4 % 3 = 1
So, the modulo is highest for 3.
Table of Content
[Naive Approach] Check Every Possible k - O(n) Time O(1) Space
The idea is to iterate through all values of
kfrom1ton-1and computen % kfor each value. Keep track of the maximum remainder obtained and return the corresponding value ofk.
#include <iostream>
using namespace std;
int modTask(int n)
{
// Stores the maximum remainder found so far
int maxRem = -1;
// Stores the value of k that gives the maximum remainder
int res = 1;
// Try all possible values of k
for (int k = 1; k <= n; k++)
{
// Calculate remainder when n is divided by k
int rem = n % k;
// Update answer if a larger remainder is found
if (rem > maxRem)
{
maxRem = rem;
res = k;
}
// If remainder is same, choose the larger k
else if (rem == maxRem)
{
res = max(res, k);
}
}
return res;
}
// Driver code
int main()
{
int n = 4;
cout << modTask(n);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int modTask(int n)
{
// Stores the maximum remainder found so far
int maxRem = -1;
// Stores the value of k that gives the maximum remainder
int res = 1;
// Try all possible values of k
for (int k = 1; k <= n; k++)
{
// Calculate remainder when n is divided by k
int rem = n % k;
// Update answer if a larger remainder is found
if (rem > maxRem)
{
maxRem = rem;
res = k;
}
// If remainder is same, choose the larger k
else if (rem == maxRem)
{
res = (res > k)? res : k;
}
}
return res;
}
int main()
{
int n = 4;
printf("%d", modTask(n));
return 0;
}
public class GfG {
static int modTask(int n)
{
// Stores the maximum remainder found so far
int maxRem = -1;
// Stores the value of k that gives the maximum
// remainder
int res = 1;
// Try all possible values of k
for (int k = 1; k <= n; k++) {
// Calculate remainder when n is divided by k
int rem = n % k;
// Update answer if a larger remainder is found
if (rem > maxRem) {
maxRem = rem;
res = k;
}
// If remainder is same, choose the larger k
else if (rem == maxRem) {
res = Math.max(res, k);
}
}
return res;
}
public static void main(String[] args)
{
int n = 4;
System.out.println(modTask(n));
}
}
def modTask(n):
# Stores the maximum remainder found so far
maxRem = -1
# Stores the value of k that gives the maximum remainder
res = 1
# Try all possible values of k
for k in range(1, n + 1):
# Calculate remainder when n is divided by k
rem = n % k
# Update answer if a larger remainder is found
if rem > maxRem:
maxRem = rem
res = k
# If remainder is same, choose the larger k
elif rem == maxRem:
res = max(res, k)
return res
# Driver code
if __name__ == "__main__":
n = 4
print(modTask(n))
using System;
class GfG {
static int modTask(int n)
{
// Stores the maximum remainder found so far
int maxRem = -1;
// Stores the value of k that gives the maximum
// remainder
int res = 1;
// Try all possible values of k
for (int k = 1; k <= n; k++) {
// Calculate remainder when n is divided by k
int rem = n % k;
// Update answer if a larger remainder is found
if (rem > maxRem) {
maxRem = rem;
res = k;
}
// If remainder is same, choose the larger k
else if (rem == maxRem) {
res = Math.Max(res, k);
}
}
return res;
}
static void Main()
{
int n = 4;
Console.WriteLine(modTask(n));
}
}
function modTask(n)
{
// Stores the maximum remainder found so far
let maxRem = -1;
// Stores the value of k that gives the maximum
// remainder
let res = 1;
// Try all possible values of k
for (let k = 1; k <= n; k++) {
// Calculate remainder when n is divided by k
let rem = n % k;
// Update answer if a larger remainder is found
if (rem > maxRem) {
maxRem = rem;
res = k;
}
// If remainder is same, choose the larger k
else if (rem == maxRem) {
res = Math.max(res, k);
}
}
return res;
}
// Driver code
let n = 4;
console.log(modTask(n));
Output
3
Time Complexity: O(n)
Auxiliary Space: O(1)
[Expected Approach] Mathematical Observation - O(1) Time O(1) Space
The idea is to observe that for any
k > n / 2, the quotient ofn / kis1, so the remainder becomesn - k. Since this remainder decreases askincreases, the maximum remainder is obtained for the smallest value ofkgreater thann/2. Therefore, the required value ofkisn/2 + 1.
Let us understand with example:
For n = 4, the function computes res = n / 2 + 1.
4 / 2 = 2res = 2 + 1 = 3- The function returns
3.
Thus, the output is 3, which is the value of k for which n % k is maximum.
#include <iostream>
using namespace std;
int modTask(int n)
{
// Calculating the answer by dividing N by 2 and adding 1.
int res = n / 2 + 1;
// Returning the answer.
return res;
}
// Driver code
int main()
{
int n = 4;
cout << modTask(n);
return 0;
}
#include <stdio.h>
int modTask(int n)
{
// Calculating the answer by dividing N by 2 and adding 1.
int res = n / 2 + 1;
// Returning the answer.
return res;
}
// Driver code
int main()
{
int n = 4;
printf("%d", modTask(n));
return 0;
}
public class GfG {
public static int modTask(int n)
{
// Calculating the answer by dividing N by 2 and
// adding 1.
int res = n / 2 + 1;
// Returning the answer.
return res;
}
public static void main(String[] args)
{
int n = 4;
System.out.println(modTask(n));
}
}
def modTask(n):
# Calculating the answer by dividing N by 2 and adding 1.
res = n // 2 + 1
# Returning the answer.
return res
# Driver code
if __name__ == "__main__":
n = 4
print(modTask(n))
using System;
class GfG {
static int modTask(int n)
{
// Calculating the answer by dividing N by 2 and
// adding 1.
int res = n / 2 + 1;
// Returning the answer.
return res;
}
static void Main()
{
int n = 4;
Console.WriteLine(modTask(n));
}
}
function modTask(n) {
// Calculating the answer by dividing N by 2 and adding 1.
let res = Math.floor(n / 2) + 1;
// Returning the answer.
return res;
}
// Driver code
let n = 4;
console.log(modTask(n));
Output
3
Time Complexity: O(1)
Auxiliary Space: O(1)