Minimum Distances
Minimum Distances
+ 0 comments Here is my c++ solution, you can watch the explanation here : https://youtu.be/H8aBskFE2XI
int minimumDistances(vector<int> a) { map<int, int>mp; int result = 1000; for(int i = 0; i < a.size(); i++){ if(mp[a[i]]){ result = min(result, i + 1 - mp[a[i]]); } mp[a[i]] = i+1; } return (result == 1000) ? -1:result; }
+ 0 comments Here is my c++ solution, you can watch the explanation here : https://youtu.be/H8aBskFE2XI
int minimumDistances(vector<int> a) { map<int, int>mp; int result = 1000; for(int i = 0; i < a.size(); i++){ if(mp[a[i]]){ result = min(result, i + 1 - mp[a[i]]); } mp[a[i]] = i+1; } return (result == 1000) ? -1:result; }
+ 0 comments ALL CASES RUNNING import java.io.; import java.util.; import java.math.; import java.util.regex.; import java.text.*; public class solution{ public static void main(String [] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a[] = new int[n]; for(int i = 0; i
} int min = Integer.MAX_VALUE; for(int i = 0; i < n ; i++){ for (int j = i+1; j < n; j++) { if (a[i] == a[j]){ int temp = j - i; if(temp < min){ min = temp; } } } } if(min == Integer.MAX_VALUE) min = -1;System.out.println(min); }
}
+ 0 comments ALL CASES RUNNING
import java.io.; import java.util.; import java.math.; import java.util.regex.; import java.text.*; public class solution{ public static void main(String [] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a[] = new int[n]; for(int i = 0; i
} int min = Integer.MAX_VALUE; for(int i = 0; i < n ; i++){ for (int j = i+1; j < n; j++) { if (a[i] == a[j]){ int temp = j - i; if(temp < min){ min = temp; } } } } if(min == Integer.MAX_VALUE) min = -1;System.out.println(min); }
}
+ 0 comments def minimumDistances(a): # Write your code here pairs = set() for i in a: if a.count(i) == 2: pairs.add(i) if not pairs: return -1 distances = [] for val in pairs: indexes = [] for i, v in enumerate(a): if v == val: indexes.append(i) distances.append(indexes[1] - indexes[0]) return min(distances)
Sort 1229 Discussions, By:
Please Login in order to post a comment