// Minimum distance that can be maintained between the rooms
def canplace(a,n,k,mid):
count=1
prev=a[0]
for i in range(1,n):
if(a[i]-prev>=mid):
count=count+1
prev=a[i]
return count>=k
def largestMinDistance(a,n,k):
a.sort()
low=1
high=a[n-1]-a[0]
while(low<=high):
mid=low+(high-low)/2
if(canplace(a,n,k,mid)):
low=mid+1
else:
high=mid-1
return high
n=int(input())
k=int(input())
a=list(map(int,input().split()))
result=largestMinDistance(a,n,k)
print(int(result))
//Minimum distance between room JAVA CODE
import java.util.*;
public class Main {
public static boolean canPlace(int[] A, int N, int K, int mid) {
int count = 1;
int prev = A[0];
for (int i = 1; i < N; i++) {
if (A[i] - prev >= mid) {
count++;
prev = A[i];
}
}
return count >= K;
}
public static int largestMinDistance(int[] A, int N, int K) {
Arrays.sort(A);
int low = 1;
int high = A[N - 1] - A[0];
while (low <= high) {
int mid = low + (high - low) / 2;
if (canPlace(A, N, K, mid)) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return high;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int K = scanner.nextInt();
int[] A = new int[N];
for (int i = 0; i < N; i++) {
A[i] = scanner.nextInt();
}
int result = largestMinDistance(A, N, K);
System.out.println(result);
}
}
// Roman to Integer Python
class Solution:
def romanToInt(self, s: str) -> int:
numbers = {
'I' : 1,
'V' : 5,
'X' : 10,
'L' : 50,
'C' : 100,
'D' : 500,
'M' : 1000
}
splitted = list(s)
splitted.reverse()
saved = 1
sum = 0
for e in splitted:
if numbers.get(e) < saved:
sum -= numbers.get(e)
else:
sum += numbers.get(e)
saved = numbers.get(e)
return sum
//C++ Code for Roman to Integer
// Program to convert Roman
// Numerals to Numbers
#include <bits/stdc++.h>
using namespace std;
// This function returns value
// of a Roman symbol
int value(char r)
if (r == 'I')
return 1;
if (r == 'V')
return 5;
if (r == 'X')
return 10;
if (r == 'L')
return 50;
if (r == 'C')
return 100;
if (r == 'D')
return 500;
if (r == 'M')
return 1000;
return -1;
// Returns decimal value of
// roman numaral
int romanToDecimal(string& str)
// Initialize result
int res = 0;
// Traverse given input
for (int i = 0; i < str.length(); i++) {
// Getting value of symbol s[i]
int s1 = value(str[i]);
if (i + 1 < str.length()) {
// Getting value of symbol s[i+1]
int s2 = value(str[i + 1]);
// Comparing both values
if (s1 >= s2) {
// Value of current symbol
// is greater or equal to
// the next symbol
res = res + s1;
else {
// Value of current symbol is
// less than the next symbol
res = res + s2 - s1;
i++;
}
else {
res = res + s1;
return res;
// Driver Code
int main()
// Considering inputs given are valid
string str = "MCMIV";
cout << "Integer form of Roman Numeral is "
<< romanToDecimal(str) << endl;
return 0;
}
// JAVA CODE for Roman To Integer
// Program to convert Roman
// Numerals to Numbers
import java.util.*;
public class RomanToNumber {
// This function returns
// value of a Roman symbol
int value(char r)
if (r == 'I')
return 1;
if (r == 'V')
return 5;
if (r == 'X')
return 10;
if (r == 'L')
return 50;
if (r == 'C')
return 100;
if (r == 'D')
return 500;
if (r == 'M')
return 1000;
return -1;
// Finds decimal value of a
// given roman numeral
int romanToDecimal(String str)
// Initialize result
int res = 0;
for (int i = 0; i < str.length(); i++) {
// Getting value of symbol s[i]
int s1 = value(str.charAt(i));
// Getting value of symbol s[i+1]
if (i + 1 < str.length()) {
int s2 = value(str.charAt(i + 1));
// Comparing both values
if (s1 >= s2) {
// Value of current symbol
// is greater or equalto
// the next symbol
res = res + s1;
else {
// Value of current symbol is
// less than the next symbol
res = res + s2 - s1;
i++;
else {
res = res + s1;
}
return res;
// Driver Code
public static void main(String args[])
RomanToNumber ob = new RomanToNumber();
// Considering inputs given are valid
String str = "MCMIV";
System.out.println("Integer form of Roman Numeral"
+ " is "
+ ob.romanToDecimal(str));
}
// Python Code of isValidPhoneNumber
import re
def isValid(s):
# 1) Begins with 0 or 91
# 2) Then contains 6,7 or 8 or 9.
# 3) Then contains 9 digits
Pattern = re.compile("^(\\+91|0)?[1-9][0-9]{9}$")
return Pattern.match(s)
# Driver Code
s = "814-732-6014"
if (isValid(s)):
print ("True")
else :
print ("False")
//IsValidPhoneNumber using JAVA
import java.util.Scanner;
import java.util.regex.Pattern;
public class Main
{
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int N=scanner.nextInt();
for(int i=0;i<N;i++){
String phonenumber=scanner.nextLine();
boolean isvalid=isvalidphonenum(phonenumber);
System.out.println(isvalid ? "True" : "False");
}
scanner.close();
}
public static boolean isvalidphonenum(String phonenumber){
String regex="^(\\+91|0)?[1-9][0-9]{9}$";
return Pattern.matches(regex,phonenumber);
}
}
//Python Code for PivotElement
def findElement(arr, n):
for i in range(1, n):
leftSum = sum(arr[0:i])
rightSum = sum(arr[i+1:])
if(leftSum == rightSum):
return arr[i]
return -1
# Driver Code
if __name__ == "__main__":
# Case 1
arr = [1, 4, 2, 5]
n = len(arr)
print(findElement(arr, n))
# Case 2
arr = [2, 3, 4, 1, 4, 5]
n = len(arr)
print(findElement(arr, n))
//C++ Code for Pivot Element
// Java Code for Pivot Element
class Solution {
public int pivotIndex(int[] arr) {
int n=arr.length;
int[] prefix=new int[n];
prefix[0]=arr[0];
int[] suffix=new int[n];
suffix[n-1]=arr[n-1];
for(int i=1;i<n;i++){
prefix[i]=prefix[i-1]+arr[i];
}
for(int i=n-2;i>=0;i--){
suffix[i]=suffix[i+1]+arr[i];
}
// if(prefix[n-1]==0) return n-1;
// if(suffix[0]==0) return 0;
for(int i=0;i<n;i++){
if(prefix[i]==suffix[i]) return i;
}
return -1;
}
}