JAVA-MATHS-101 Flashcards
1
Q
Define Permutation & Combination in one Line with formula
A
Perm : When no repitetion ; n!
Comb: When repetion is allowed ;
2
Q
Write a Program to find Permutation & Combination?
A
import java.util.Date;
import java.util.*;
public class HelloWorld { public static void main(String[] args) {
char elements[] = {'R','O','O','T'}; int noOfUniqueElements = getUniqueElements(elements); //case of PErutation int noOfElements = elements.length; // case Of Combination int noOFSlots = 3; System.out.println(doPerm(noOfElements, noOFSlots)); System.out.println(doComb(noOfElements, noOFSlots)); } private static int getUniqueElements(char elements[]){ // to do return elements.length; } private static int doPerm(int noOFElements, int noOFSlots){ int perms = noOFElements; while(noOFSlots > 1){
noOFElements = noOFElements - 1; perms = perms * noOFElements; // mind that the perms should not bemultiplied to perms // and also the noOfElements-1 wont work
noOFSlots--; } return perms; } private static int doComb(int noOFElements, int noOFSlots){ int perms = 1; while(noOFSlots > 0){ perms = perms * noOFElements; noOFSlots--; } return perms; }
}
3
Q
What strategy will you apply to find the non repeated unique number be formed inbetween 100 & 1000. Where the numbers used are 0-6 and no duplicates must come in number generations?
A
This is a problem of permutation
import java.util.Date;
public class HelloWorld { public static void main(String[] args) { int noOfElements = 5; // as the 0-6 , but the hundreath place wil be taken int noOSlots = 3;
int perms = 5; // as the very first or the hundreath place wont have 0 while(noOSlots > 1){ perms = perms * noOfElements; noOfElements--; noOSlots--; } System.out.println(perms); }
}