Ravi Kumar
BAN USERpublic class PrintNotCommonInBothArrays {
List<Integer> list=new ArrayList<Integer>();
public void print(int A[],int B[])
{
int i=0,j=0;
boolean flag=false;
for( i=0;i<A.length;i++)
{
flag=false;
for( j=0;j<B.length;j++)
{
if(A[i]==B[j])
{
flag=true;
break;
}
}
if(j==B.length && flag==false)
{
//System.out.println(A[i]);
list.add(A[i]);
}
}
}
public static void main(String[] args) {
PrintNotCommonInBothArrays pib=new PrintNotCommonInBothArrays();
int A[] = {1, 3, 4, 6,8,10, 17, 34};
int B[] = {2, 8, 17, 33, 44, 66, 89, 100, 123};
pib.print(A,B);
pib.print(B, A);
Collections.sort(pib.list);
System.out.println(pib.list);
}
}
class Country
{
String countryName;
State slink_country;
Country clink_country;
Country(String countryName)
{
this.countryName=countryName;
}
}
class State
{
String stateName;
City citlink_state;
State slink_state;
State(String stateName)
{
this.stateName=stateName;
}
}
class City
{
String cityName;
City citlink_city;
City(String cityName)
{
this.cityName=cityName;
}
}
public class CombinationsOfDeniminations {
static int a[]={2,3,4,5};
static void print(int k,int tot)
{
if(k==a.length)
return;
int sum=0;
String newString="";
for(int i=k;i<a.length;i++)
{
sum+=a[i];
newString+=a[i]+",";
if(sum==tot)
{
System.out.println(newString);
}
}
print(k+1,tot);
}
public static void main(String[] args) {
print(0,9);
}
}
public class FindMissingCharactersFromaRangeOfString {
static void findMissing(String str) {
char ch[] = str.toCharArray();
Set<Character> ch1 = new TreeSet<Character>();
for (Character a : ch)
ch1.add(Character.toLowerCase(a));
ArrayList<Character> a1 = new ArrayList<Character>(ch1);
//System.out.println(a1);
int p = a1.get(0);
int m = 0;
for (int a : a1) {
if ((p + m) == a) {
// System.out.println((char)a);
}
else
System.out.println("Missed="+(char) (p + m));
m++;
}
// a1.add(a);
// System.out.println(a1);
}
public static void main(String[] args) {
String str = "baADfc";
findMissing(str);
}
}
public class FindSubStringinAnotherStringNotInThirdString {
String str1 = null;
String str2 = null;
String str3 = null;
public FindSubStringinAnotherStringNotInThirdString() {
str1 = "spqrstruvpqw";
str2 = "sprt";
str3 = "q";
}
String findSubString() {
String newString = "";
String strArray[] = str1.split(str3);
for (String str : strArray) {
if (str.length() >= str2.length())
newString = str;
// System.out.println(str);
}
return findpositions(newString);
}
public String findpositions(String newString) {
int pos1 = 0, pos2 = 0;
char ch[] = str2.toCharArray();
int a1 = 0, b = 0, c = 1;
for (char a2 : ch) {
a1 = newString.indexOf(a2);
if (a1 > b)
b = a1;
if (a1 < c)
c = a1;
}
// System.out.println(c + " "+b+""+newString.substring(c+1,b+1));
return newString.substring(c + 1, b + 1);
}
public static void main(String[] args) {
FindSubStringinAnotherStringNotInThirdString fs = new FindSubStringinAnotherStringNotInThirdString();
System.out.println("subString is=" + fs.findSubString());
}
}
public class RemoveZero {
public static void main(String[] args) {
remove("120jdvj00ncdnv000ndnv0nvd0nvd0" );
}
static void remove(String str)
{
int k=0;
for(int i=0;i<str.length()-1;i++)
{
k++;
if(str.charAt(i)=='0' && str.charAt(i+1)=='0' )
{
i++;
k++;
if(str.charAt(i)=='0' && str.charAt(i+1)=='0' )
{
i++;
k++;
}
}
else
System.out.print(str.charAt(i));
}
//System.out.print(str.charAt(k));
System.out.println();
System.out.println(str);
}
}
public class ReversefirstNuNUmbers {
public static void main(String[] args) {
int a[] = { 2, 12, 8, 6, 5, 1, 2, 10, 3, 2 };
reverseArray(a,3);
display(a);
}
public static void display(int a[]) {
for (int a1 : a) {
System.out.print(a1 + ",");
}
}
public static void reverseArray(int a[],int count)
{
for (int i=0;i<count;i++) {
int temp=a[i];
a[i]=a[a.length-i-1];
a[a.length-i-1]=temp;;
}
}
}
Output: -
12,12,10,10,10,10,10,10,3,2,
public class ReplaceNextGreaterElement {
public static void main(String[] args) {
int a[] = { 2, 12, 8, 6, 5, 1, 2, 10, 3, 2 };
greaterElement(a);
display(a);
}
public static void display(int a[])
{
for(int a1:a)
{
System.out.print(a1+",");
}
}
public static void greaterElement(int a[]) {
int len = a.length;
for (int i = 0; i < len-1; i++) {
for (int j = 0; j < len-1; j++) {
if(a[j]<a[j+1])
{
a[j]=a[j+1];
}
}
}
}
}
Output-
Range is={ 1,....., 11 }
public class FindRangeEachArrayContainingAtleats1Integer {
public static void main(String[] args) {
int a[] = { 1, 2, 3, 5 };
int b[] = { 3, 5, 6, 7 };
int c[] = { 2, 8, 9, 11 };
Set<Integer> se = new HashSet<>();
for (int i = 0; i < a.length; i++) {
se.add(a[i]);
}
for (int i = 0; i < b.length; i++) {
se.add(b[i]);
}
for (int i = 0; i < c.length; i++) {
se.add(c[i]);
}
int length = se.size();
int i = 0,min=0,max=0;
for (int a1 : se) {
if (i == 0) {
min=a1;
}
i++;
if (i == length) {
max=a1;
}
}
System.out.println("Range is={ "+min+ ",.....,"+ max + " } ");
}
}
Output-
a1
b1
a2
b2
a3
b3
class Link {
public String data;
public Link next;
Link() {
}
public Link(String data) {
this.data = data;
}
}
class InsertLinkList {
private Link first;
InsertLinkList() {
first = null;
}
public void intializeLink(String data) {
Link a = new Link(data);
if (first == null) {
first = a;
a.next = null;
} else {
a.next = first;
first = a;
}
}
public void display(InsertLinkList lin) {
while (first != null) {
System.out.println(first.data);
first = first.next;
}
}
public void display1(InsertLinkList lin) {
Link first1 = first;
Link first3 = first;
while (first1.next != null) {
System.out.println(first3.data);
first1 = first3.next.next.next;
System.out.println(first1.data);
first3 = first3.next;
}
}
}
public class LinkedLista1b1a2b2 {
public static void main(String[] args) {
InsertLinkList lin = new InsertLinkList();
lin.intializeLink("b3");
lin.intializeLink("b2");
lin.intializeLink("b1");
lin.intializeLink("a3");
lin.intializeLink("a2");
lin.intializeLink("a1");
lin.display1(lin);
}
}
Output:-
Pythagoreans sets= { 3,4,5 }
Pythagoreans sets= { 6,8,10 }
Pythagoreans sets= { 9,12,15 }
Pythagoreans sets= { 8,6,10 }
Pythagoreans sets= { 12,9,15 }
{
public class FindPythagoreanTripltes {
public static void main(String[] args) {
findPythagoreanTriplets();
}
public static void findPythagoreanTriplets()
{
int arr[]= { 3,6,9,4,8,5,10,12,15 };
for(int i=0;i<arr.length;i++)
{
for(int j=1;j<arr.length;j++)
{
for(int k=2;k<arr.length;k++)
{
if((Math.pow(arr[i],2)+Math.pow(arr[j],2))==Math.pow(arr[k],2))
{
System.out.println("Pythagoreans sets= { "+arr[i]+","+arr[j]+","+arr[k]+" }");
}
}
}
}
}
}
public class FindMissingNumber {
public static void main(String[] args) {
findDuplicateNumber(10);
}
public static void findDuplicateNumber(int num)
{
int sum=10*(11)/2;
int sum1=1+2+3+5+6+7+8+9+10;
int num1=sum-sum1;
System.out.println(num1);
}
}
output-
Number of zero=24
public class CalculateNumberOfZeros {
public static void main(String[] args) {
findMaxPowerof5(100);
}
public static void findMaxPowerof5(int num) {
LinkedList<Integer> link = new LinkedList<Integer>();
int totPower = 0;
for (int i = 1; i <= num; i++) {
if (Math.pow(5, i) <= num) {
totPower += num / Math.pow(5, i);
}
}
System.out.println("Number of zero=" + totPower);
}
}
public class FindPatternOccurrences {
public static void main(String[] args) {
String str = "aabababcfdc";
String pattern = "ab";
matchPattern(str, pattern);
}
public static void matchPattern(String str, String pattern) {
int count = 0;
for (int i = 0; i < str.length() - 1; i++) {
String str1 = str.substring(i, i + 2);
if (str1.equals(pattern))
count++;
}
System.out.println("NUmber of occurrences= " + count);
}
}
// Find number of occurrences of 0 to 9 upto 1 to 100
public class FindNumOfOccurrences {
public static void main(String[] args) {
findCount(9);
}
public static void findCount(int num)
{
int count=0;
for(int i=0;i<100;i++)
{
int a=i/10;
int b=i%10;
if(a==num)
count++;
if(b==num)
count++;
}
System.out.println("Number of "+ num +"'s= "+count);
}
}
Output-
bacde
bcade
bcdae
bcdea
public class SwapLikeAnagram {
public static void swapAnagram(String str) {
for (int i = 0; i < str.length(); i++) {
str = charValues(str, i);
}
}
public static String charValues(String str, int i) {
String newString = "";
char[] ch = str.toCharArray();
if (i < str.length() - 1) {
char ch1 = ch[i];
ch[i] = ch[i + 1];
ch[i + 1] = ch1;
for (Character ch5 : ch) {
System.out.print(ch5);
newString += ch5;
}
System.out.println();
}
return newString;
}
public static void main(String[] args) {
String str = "abcde";
swapAnagram(str);
}
}
import java.util.HashMap;
import java.util.Set;
public class ReArrangeToPalindrome {
HashMap<Character, Integer> hmp = new HashMap<Character, Integer>();
public void isPalindromePossible(String str) {
int i = 0;
char[] ch = str.toCharArray();
if (ch.length == 0) {
System.out.println("Empty String");
return;
} else if (ch.length == 1) {
System.out.println("---PalinDrome---");
return;
}
while (i != ch.length) {
if (hmp.containsKey(ch[i])) {
hmp.put(ch[i], hmp.get(ch[i]) + 1);
} else {
hmp.put(ch[i], 1);
}
i++;
}
findPalinDrome();
}
public void findPalinDrome() {
Set<Character> ch1 = hmp.keySet();
int k = 0;
for (Character ch : ch1) {
if (hmp.get(ch) % 2 == 0) {
} else {
k++;
if (k % 2 == 0) {
System.out.println("---No Palindrome---");
return;
}
}
}
System.out.println("---PalinDrome---");
}
public static void main(String[] args) {
ReArrangeToPalindrome rp = new ReArrangeToPalindrome();
String str = "axcdbcacba";
rp.isPalindromePossible(str);
}
}
import java.util.LinkedList;
//shopkeeper wants to sell only in weights of 20,9 and 6
public class SetOfWeights {
public void findWeights(int totWeight)
{
LinkedList<Integer> maxWeight=new LinkedList<Integer>();
LinkedList<Integer> midWeight=new LinkedList<Integer>();
LinkedList<Integer> minWeight=new LinkedList<Integer>();
int weight1=20,weight2=9,weight3=6;
int multiple=0;
int i1=0;
while(totWeight>multiple)
{
maxWeight.add(multiple);
i1++;
multiple=weight1*i1;
}
multiple=0;i1=0;
while(totWeight>multiple)
{
midWeight.add(multiple);
i1++;
multiple=weight2*i1;
}
multiple=0;i1=0;
while(totWeight>multiple)
{
minWeight.add(multiple);
i1++;
multiple=weight3*i1;
}
//display();
for(int i=0;i<maxWeight.size();i++)
{
for(int j=0;j<midWeight.size();j++)
{
for(int k=0;k<minWeight.size();k++)
{
int calc= maxWeight.get(i)+midWeight.get(j)+minWeight.get(k);
if(calc==totWeight)
{
System.out.println("set equal to "+totWeight+"= {"+ weight1+"*"+maxWeight.get(i)/weight1+" , "+ weight2+"*"+ midWeight.get(j)/weight2+" , "+ weight3+"*"+ minWeight.get(k)/weight3 + "}");
}
}
}
}
}
public static void main(String[] args) {
SetOfWeights sw=new SetOfWeights();
sw.findWeights(47);
}
}
- Ravi Kumar June 28, 2014