prithvish.mukherjee@techolution.com
BAN USER
import java.util.Arrays;
import java.util.List;
public class CirculaarCriminalDeath {
public static void main(String[] args) {
System.out.println(lastTwoCriminalsInCircle(4));
System.out.println(lastTwoCriminalsInCircle(-4));
System.out.println(lastTwoCriminalsInCircle(Long.MAX_VALUE));
}
/**
* N people form a circle, in each cycle they keep killing the one[alive] to
* their immediate left i.e. next element; When n is even, man at end changes by
* power of 2^pow; when n is odd, man at front changes by power of 2^(pow+1);
* With each cycle pow increases by 1.
*
* @param n
* @return
*
*/
/**
* Odd
* 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37
* -> n odd
*
* Cycle 1: 3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37 -> since prev n
* was odd, front = front + 2; n = n/2, we get n = 18, i.e. even, deleted n/2 +
* 1 = 19
*
*
* Cycle2: 3,7,11,15,19,23,27,31,35 -> since prev n was even, back = back - 2; n
* = n/2 we get n = 9, i.e. odd, deleted n/2 = 9
*
*
* Cycle3: 11,19,27,35 -> since prev n was odd, front = front + 8; n = n/2 we
* get n = 4, i.e. even; deleted n/2 + 1 = 5
*
*
* Cycle4: 11,27 -> since prev n was even, back = back - 8; n = n/2 we get n = 2
* even; deleted n/2 = 2
*
* if(n == 2) -> answer is front , back i.e 11,27
*
*
* Even
* 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28 ->
* n even
*
*
* Cycle1: 1,3,5,7,9,11,13,15,17,19,21,23,25,27 -> since prev n was even, back =
* back - 1; n = n/2, we get n = 14, i.e. even, deleted n/2 = 14
*
* Cycle2: 1,5,9,13,17,21,25 -> since prev n was even, back = back - 2; n = n/2,
* we get n = 7, i.e. odd, deleted n/2 = 7
*
*
* Cycle: 9,17,25 -> since prev n was odd, front = front + 8; n = n/2, we get n
* = 3, i.e. odd, deleted n/2 + 1 = 4
*
* if(n == 3) -> answer is front, back i.e 9,25
*
*/
static List<Long> lastTwoCriminalsInCircle(long n) {
if (n <= 1) {
System.out.println("Please enter a positive Intger between 2 and " + Long.MAX_VALUE);
return Arrays.asList(n);
}
long front = 1l;
long back = n;
double power = 0;
double base = 2;
while (n > 3) {
long f = (long) Math.pow(base, (power + 1));
long b = (long) Math.pow(base, power);
if (n % 2l == 0) {
back = back - b;
}
if (n % 2l != 0) {
front = front + f;
}
n = n / 2l;
power++;
}
return Arrays.asList(front, back);
}
}
package com.sample.test;
import java.util.Objects;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class FastestPrimePrint {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner ss = new Scanner(System.in);
System.out.println("Please enter the integer till which you wish to print prime numbers :");
int n = ss.nextInt();
printPrimes(n);
}
static void printPrimes(int seed) {
System.out.println(IntStream.range(2, seed+1).mapToObj(num ->{
int sqRoot = (int)Math.sqrt(num);
boolean isPrime = true;
for(int s=2; s<=sqRoot; s++){
if(num%s==0) {
isPrime = false;
break;
}
}
if(isPrime) {
return String.valueOf(num);
} else {
return null;
}
})
.filter(Objects::nonNull)
.collect(Collectors.joining(", ")));
}
}
package com.sample.test;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class NotificationSystem {
public static void main(String[] args) {
ServiceProvider sp = new ServiceProvider();
Subscriber sub = new Subscriber();
Scanner ss = new Scanner(System.in);
System.out.println("List of available services with their Ids ::");
sp.getSubscriptions().stream().forEach(System.out::println);
System.out.println("Please select Ids that you wish to subscribe to separated by ( , ) ::");
String[] strIds = ss.nextLine().split(",");
int[] ids = Stream.of(strIds).mapToInt(s -> Integer.valueOf(s.trim())).toArray();
sp.registerForService(sub, ids);
sp.getSubscriptionMap().entrySet().stream().map(e -> {
return String.format("Subscriber Subscribed to %s", e.getKey());
}).forEach(System.out::println);
Random r = new Random();
int[] stop = new int[] { -1 };
while (stop[0] != 3) {
sp.getSubscriptions().stream().forEach(subscription -> {
if (stop[0] != 3) {
int upper = 10000;
int lower = 2000;
// so that we have lower >> num >> upper
subscription.setData(r.nextInt(upper - lower) + lower);
try {
Thread.sleep(1000);
} catch (Exception e1) {
System.out.println("Error " + e1);
}
stop[0] = r.nextInt(10);
if(stop[0] == 3) {
System.out.println(".... Stopping");
}
}
});
}
}
}
class Subscriber {
ServiceProvider pub;
void getNotified(Subscription s) {
System.out.println(s);
// System.out.println(s.msg);
}
public Set<Subscription> getSubscList() {
return pub.getSubscriptions();
}
public void subscribe(int... serviceIds) {
pub.registerForService(this, serviceIds);
}
}
class ServiceProvider {
private Map<Subscription, List<Subscriber>> subscriptionMap = new LinkedHashMap<Subscription, List<Subscriber>>();
private Set<Subscription> subscriptions = new LinkedHashSet<Subscription>();
Subscription s1 = new Subscription(1, 1000);
Subscription s2 = new Subscription(2, 2000);
Subscription s3 = new Subscription(3, 3000);
Subscription s4 = new Subscription(4, 4000);
Subscription s5 = new Subscription(5, 5000);
public ServiceProvider() {
addSubscription(s1, s5, s2, s4);
}
public Map<Subscription, List<Subscriber>> getSubscriptionMap() {
return subscriptionMap;
}
public Set<Subscription> getSubscriptions() {
return subscriptions;
}
void addSubscription(Subscription... subs) {
Stream.of(subs).forEach(s -> {
if (!subscriptions.contains(s)) {
s.setPublisher(this);
subscriptions.add(s);
}
});
}
void registerForService(Subscriber subscriber, int... serviceIds) {
IntStream.of(serviceIds).forEach(serviceId -> {
Set<Subscription> sFound = subscriptions.stream().filter(s -> s.id == serviceId)
.collect(Collectors.toCollection(LinkedHashSet<Subscription>::new));
if (null != sFound && sFound.size() > 0) {
Subscription s = sFound.iterator().next();
if (subscriptionMap.containsKey(s)) {
subscriptionMap.get(s).add(subscriber);
} else {
subscriptionMap.put(s, Arrays.asList(subscriber));
}
} else {
System.err.println("No service available with serviceId :: " + serviceId);
}
});
}
void notifyConsumers(Subscription... subs) {
Stream.of(subs).forEach(s -> {
if (subscriptionMap.containsKey(s)) {
List<Subscriber> consumers = subscriptionMap.get(s);
if (null != consumers) {
consumers.stream().forEach(con -> con.getNotified(s));
}
}
});
}
}
class Subscription {
final int id;
int data;
String msg = "";
ServiceProvider serviceProvider;
public Subscription(int id, int data) {
this.id = id;
this.data = data;
}
public Subscription(int id) {
this.id = id;
}
public void setPublisher(ServiceProvider serviceProvider) {
this.serviceProvider = serviceProvider;
}
public void setData(int new_data) {
if (new_data != data) {
msg = String.format("Subscription data changed from %d to %d", data, new_data);
data = new_data;
serviceProvider.notifyConsumers(this);
}
}
@Override
public boolean equals(Object obj) {
if (null == obj) {
return false;
}
if (obj instanceof Subscription) {
Subscription o2 = (Subscription) obj;
return o2.id == this.id;
}
return false;
}
@Override
public String toString() {
return "Subscription [id=" + id + ", data=" + data + ", msg=" + msg + "]";
}
}
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
public class OptimalFlightDistant {
public static void main(String[] args) {
// sample input
System.out.println(getIdPairsForOptimal(
Arrays.asList(Arrays.asList(1, 3000), Arrays.asList(2, 5000), Arrays.asList(3, 7000),
Arrays.asList(4, 10000)),
Arrays.asList(Arrays.asList(1, 2000), Arrays.asList(2, 9000), Arrays.asList(3, 5000)), 10000));
}
public static List<List<Integer>> getIdPairsForOptimal(List<List<Integer>> forwardList,
List<List<Integer>> backwardList, int maxDistance) {
List<List<Integer>> result = new LinkedList<List<Integer>>();
forwardList = forwardList.stream().sorted((x1, x2) -> Integer.compare(x2.get(1), x1.get(1)))
.collect(Collectors.toList());
backwardList = backwardList.stream().sorted((x1, x2) -> Integer.compare(x1.get(1), x2.get(1)))
.collect(Collectors.toList());
int maxDist = maxDistance;
while (true) {
for (List<Integer> l : forwardList) {
for (List<Integer> b : backwardList) {
int forward = l.get(1);
int backward = b.get(1);
int tot = (forward + backward);
if (tot > maxDist) {
break;
}
if (tot == maxDist) {
// print the pair of Id and optimum distance
result.add(Arrays.asList(l.get(0), b.get(0), maxDist));
break;
}
}
}
if (result.size() > 0) {
break;
}
maxDist--;
}
return result;
}
}
Repherminanmuller87, Animator at 247quickbookshelp
Hello,I am a literacy teacher. I completed my degree from Chicago and now am a literacy teacher in a ...
RepEddieAtkins, Network Engineer at Big Fish
Creative Screenwriter with a passion for storytelling and extraordinary experience of working with world-famous actors and entertainment companies, such as ...
Repgaryllamasg, Backend Developer at Adjetter Media Network Pvt Ltd.
Filling in as an office agent at Mode O'Day here I learned various things here , or Office Administrator, is ...
RepPrishaDavin, abc at 8x8
I promote and provide extraordinary internal external guest services, answer questions, offer information, and provide assistance in a courteous manner ...
Repbenaryhell3454, Blockchain Developer at A9
Working as a carpet installer at Simple Solutions is almost 10 years . Here I daily meet new people and learn ...
Repgenoimthomas666, Android test engineer at 8x8
Hey, I'm a Training and Development Manager. And I love my work. Apart from this, I am doing some ...
Repdelenestanf0, HR Executive at Agilent Technologies
Hi, I am a Clinical social worker. methods of prevention and treatment in providing mental-health/healthcare services, I also have ...
- prithvish.mukherjee@techolution.com January 04, 2019