Created
June 29, 2021 16:31
-
-
Save bijay-shrestha/e1345a5a8a1aec21d36fedb6b1e67b35 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* * Write a function named minDistance that returns the smallest distance between two factors of a number. | |
* * | |
* * For example, consider 13013 = 1*7*11*13. Its factors are 1, 7, 11, 13 and 13013. | |
* * minDistance(13013) would return 2 because the smallest distance between any two factors is 2 (13 - 11 = 2). | |
* * | |
* * As another example, minDistance (8) would return 1 because the factors of 8 are 1, 2, 4, 8 | |
* * and the smallest distance between any two factors is 1 (2 – 1 = 1). | |
* * | |
* * The function signature is | |
* * int minDistance(int n) | |
*/ | |
package com.basic.practice; | |
import lombok.extern.slf4j.Slf4j; | |
@Slf4j | |
public class MinDistance { | |
public static void main(String[] args) { | |
// int number = 13013; | |
int number = 8; | |
log.info("Finding the min-distance of the number: {}, we get: {}", number, minDistance(number)); | |
} | |
static int minDistance(int n) { | |
int[] a = getFactorsOfNumber(n); | |
int len = a.length; | |
int min = a[len - 1]; | |
int sub = 0; | |
for (int i = len - 2; i > 0; i--) { | |
sub = a[i] - a[i - 1]; | |
if (sub <= min) { | |
min = sub; | |
} | |
} | |
return min; | |
} | |
static int[] getFactorsOfNumber(int n) { | |
int[] arr = new int[countNumberOfFactors(n)]; | |
int idx = 0; | |
for (int i = 1; i <= n; i++) { | |
if (n % i == 0) { | |
arr[idx] = i; | |
idx++; | |
} | |
} | |
return arr; | |
} | |
static int countNumberOfFactors(int num) { | |
int count = 0; | |
for (int i = 1; i <= num; i++) { | |
if (num % i == 0) { | |
count++; | |
} | |
} | |
return count; | |
} | |
} | |
; |
sanjmgr
commented
Aug 16, 2022
static int minDistance(int n) {
int smallestDistance = n/2, previousFactor = 1;
for (int i = 2; i < n/2; i++) {
if(n%i == 0) {
int diff = i - previousFactor;
if(diff < smallestDistance) smallestDistance = diff;
previousFactor = i;
}
}
return smallestDistance;
}
public static int minDistance(int n){
int minDistance = n, count=0,first=0;
for (int i=2;i<n;i++) {
if (n % i == 0) {
if (count == 0) {
first = i;
}
count++;
if (count > 1) {
if (minDistance > i - first) {
minDistance = i - first;
}
first = i;
}
}
if(count==1 && i==n-1){
minDistance=i-i;
}
}
if(count==0){
minDistance=-1;
}
return minDistance;
}
static int minDistance(int n) { int factor1 = 1; int factor2 = 0; int minDistance = n; for (int i = 2; i < n; i++) { if (n % i == 0) { factor2 = i; int distance = factor2 - factor1; if (distance <= minDistance) { minDistance = distance; factor1 = factor2; } } } return minDistance; }
test with n=25 or 7 .. your code fails.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment