Showing posts with label Group Problem. Show all posts
Showing posts with label Group Problem. Show all posts

Wednesday, October 4, 2017

Problem Array Sort Shuffle

Code:

public class Akhil {

    public static void main(String [] nt){

        int[] a={1,3,4,2};

        if(!sorted(a)){
            for (int i=0;i<a.length-2;i++){
                if(!sorteda(a[i],a[i+1],a[i+2])){
                    if(sorteda(a[i+1],a[i+2],a[i])){
                        int temp=a[i];
                        a[i]=a[i+1];
                        a[i+1]=a[i+2];
                        a[i+2]=temp;
                        continue;
                    }else {
                        if(sorteda(a[i+2],a[i],a[i+1])){
                            int temp=a[i];
                            a[i]=a[i+2];
                            a[i+2]=a[i+1];
                            a[i+1]=temp;
                            continue;
                        }else{
                            break;
                        }
                    }
                }
            }
        }

        if(sorted(a)){
            System.out.println("Yes");
        }else{
            System.out.println("No");
        }

    }

    static boolean sorted(int[] a){

        for(int i=0;i<a.length-1;i++){
            if(a[i]>a[i+1]){
                return false;
            }
        }

        return true;
    }

    static boolean sorteda(int a,int b,int c){
        if(a<b && b<c){
            return true;
        }
        return false;
    }

}

Primorial Prime Numbers

Code:

import java.util.ArrayList;

public class PP {

    public static void main(String[] nt){

        // To avoid repetion of Primorial primes        ArrayList<Integer>integers=new ArrayList<>();

        //This is end Value        int x=11;


        for(int J=2;J<=x;J++) {
            int pn = 1;
//Calculating Primorial            for (int i = 2; i <= J; i++) {
                if (isprime(i)) {
                    pn = pn * i;
                }
            }
//Checking For Prime            if(isprime(pn-1)){
                if(!integers.contains(pn-1)){
                    integers.add(pn-1);
                }

            }if(isprime(pn+1)){
                if(!integers.contains(pn+1)){
                    integers.add(pn+1);
                }
            }
        }
//Printing all Primorial Primes        for (int a:integers){
            System.out.println(a);
        }


    }

    static boolean isprime(int n){

        for (int i=2;i<=n/2;i++){
            if(n%i==0){
                return false;
            }
        }
        return true;
    }

}

Output:


Sunday, September 3, 2017

Number To Equivalent InWords String

Code:
package com.nearur;

import java.util.Scanner;

public class English {

String[] ones= {"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","ninteen"};
String[] tens= {"","","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninty"};
StringBuffer buffer=new StringBuffer();

String c(int n) {
if(n<0) {
return "Minus "+c(-n);
}else if(n<20) {
return ones[n];
}else if(n<100) {
return tens[n/10]+" "+ones[n%10];
}else if(n<1000) {
return c(n/100)+" hundred "+c(n%100);
}else if(n<100000) {
return c(n/1000)+" thousand "+c(n%1000);
}else if(n<10000000) {
return c(n/100000)+" lakh "+c(n%100000);
}else if(n<1000000000) {
return c(n/10000000)+" crore "+c(n%10000000);
}

return "";
}

public static void main(String[] args) {
English a=new English();
Scanner in =new Scanner(System.in);
System.out.print("Enter Any Number : ");
int n=in.nextInt();
System.out.println(a.c(n).toUpperCase());

}

}

Output:


Friday, September 1, 2017

Name In Binary

Code:

package com.nearur;

import java.util.Scanner;

public class NTB {


static StringBuffer buffer=new StringBuffer();
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter Your Name:");
String a=in.nextLine();
String[] name=a.split(" ");

for(String x:name) {
convert(x);
}

System.out.println("Binary Code is : "+buffer.toString());
}

static void convert(String name) {

char[] a=name.toCharArray();
for(char x: a) {
binary((int)x);
buffer.append(" ");
}

}
static void binary(int n) {
while(n>0) {
if(n%2==0) {
buffer.append("0");
}else {
buffer.append("1");
}
n=n/2;
}
}


}


Output:


Tuesday, August 22, 2017

HangMan

Following Code is Hangman Like game without graphics inspired by Duta http:duta.in:


Code:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
static String check="AEIOU";
public static void main(String[] args) {
ArrayList<String> questions=new ArrayList<>();
questions.add("ABRUPTLY");
questions.add("BOOKWORM");
questions.add("GALAXY");
questions.add("PNEUMONIA");
questions.add("STRONGHOLD");
questions.add("WHIZZING");
questions.add("PUZZLING");
questions.add("WRISTWATCH");
questions.add("ZODIAC");
questions.add("THUMBSCREW");
questions.add("YACHTSMAN");
questions.add("MNEMONIC");
questions.add("TRANSPLANT");
questions.add("RHYTHM");
questions.add("MICROWAVE");
Collections.shuffle(questions);
String ques=questions.get(0);
   char[] given=new char[ques.length()];
   int count=0;
   for(int i=0;i<given.length;i++) {
    if(varcheck(ques.charAt(i))) {
    given[i]=ques.charAt(i);
    }else {
    given[i]='_';
    }
   }
   boolean win=false;
   Scanner in=new Scanner(System.in);
   StringBuffer buffer=new StringBuffer();
outer:
   while(count<7 && !win) {
    for(int i=0;i<given.length;i++) {
    System.out.print(given[i]+" ");
    }
    System.out.println();
    System.out.println("Guess a Letter");
    char c=in.nextLine().toUpperCase().charAt(0);
    int i=0;
    while(i<given.length) {
    if(given[i]==c) {
    System.out.println("Already Present in Word!!");
    System.out.println("Guessed Letters : "+buffer);
    System.out.println("Chances left : "+(7-count));
    System.out.println("------------------------------");
    continue outer;
    }i++;
    }
    if(buffer.toString().contains(c+"")) {
    System.out.println("Already Guessesed!!");
    System.out.println("Guessed Letters : "+buffer);
    System.out.println("Chances left : "+(7-count));
    System.out.println("------------------------------");
    continue outer;
    }
    if(ques.contains(c+"")) {
    int idx=ques.indexOf(c);
    while(idx>=0) {
    given[idx]=c;
    idx=ques.indexOf(c,idx+1);
    win=true;
    }
    System.out.println("Good Guess!!");
    }else {
    count++;
    System.out.println("Bad Guess!!");
    }
    buffer.append(c+",");
    System.out.println("Guessed Letters : "+buffer.toString().substring(0,buffer.length()-1));
    System.out.println("Chances left : "+(7-count));
    for(char x: given) {
    if(x=='_') {
    win=false;
    }
    }
    System.out.println("--------------------------------\n");
   }
    System.out.println("Word is : "+ques);
   if(win) {
    System.out.println("Congratulations,You win!!");
   }else {
    System.out.println("You Loose!!,Don't Worry Try Again");
   }
   in.close();
}
static boolean varcheck(char c) {
if(check.contains(c+"")) return true;
return false;
}

}

Output:






Sunday, August 20, 2017

Desired Number of Quark Pairs

Following Code will give you desired number of quark pairs:


Code:

import java.util.Scanner;

public class MainQuark {
public static void main(String[] nt) {
System.out.print("Enter Value of N :");
int n;
Scanner in=new Scanner(System.in);
n=in.nextInt();
        int count=0,i=1,num;
        StringBuffer buffer=new StringBuffer();
while(count!=n) {
num=i;
int q=quark(num);
if(!prime(q)) {
for(int j=num+1;j<q;j++) {
if(q==quark(j)) {
buffer.append(i+"-"+j+"\n");
count++;
}
}
}
i++;
}
System.out.println("Pairs are\n"+buffer.toString());
}
static int quark(int n) {
int one=0,i=n;
while(n>0) {
if(n%2==1) {
one++;
}n=n/2;
}return one*(i);
}

static boolean prime(int n) {
for(int i=2;i<n/2;i++) {
if(n%i==0) return false;
}return true;
}

}


Output:




Quark Pairs Upto N

Quark Pairs: 



Code:

import java.util.Scanner;

public class MainQuark {
public static void main(String[] nt) {
System.out.print("Enter Value of N :");
int n;
Scanner in=new Scanner(System.in);
n=in.nextInt();
int[] quarks=new int[n];
int num=0;
for(int i=0;i<n;i++) {
num=i+1;
int one=0;
while(num>0) {
if(num%2==1) {
one++;
}num=num/2;
}
quarks[i]=one*(i+1);
}
StringBuffer buffer=new StringBuffer();
for(int i=0;i<n;i++) {
for(int j=i+1;j<n;j++) {
if(quarks[i]==quarks[j]) {
buffer.append((i+1)+"-"+(j+1)+"\n");
}
}
}
System.out.println("All Pairs upto "+n+" : ");
System.out.println(buffer.toString());
}

}


Output: