Olivia
BAN USER
Comments (3)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
public static void bullsAndCows(String p1, String p2) {//duplicate element may lead to different result, thus we do'not consider that condition
if(p1==null||p2==null){
return ;
}
int bulls=0;
int cows=0;
HashMap<Character,Integer> map= new HashMap<>();
for(int i=0;i<p1.length();i++){
map.put(p1.charAt(i), i);
}
for(int j=0;j<p2.length();j++){
char cur=p2.charAt(j);
if(map.containsKey(cur)){
if(map.get(cur)==j){
bulls++;
}else
cows++;
}
}
System.out.println("bulls"+bulls+"cows"+cows);
}
Comment hidden because of low score. Click to expand.
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
public class Solution {
- Olivia February 25, 2015public static boolean isEven(int num){
if(num<=2){
System.out.println("Number less than 3");
}
return num%2==0?true:false;
}
public static boolean isPrime(int num){
for(int i=2;i<=Math.sqrt(num);i++){
if(num%i==0){
return false;
}
}
return true;
}
public static void Goldbach(int num){
if(isEven(num)){
for(int i=2;i<=num/2;i++){
if(isPrime(i)&&isPrime(num-i)){
System.out.println("Pair"+i+","+(num-i));
}
}
}else{
System.out.println("Odd or less than 3");
}
}
public static void main(String[] args){
Goldbach(20);
}
}