MrWayne
BAN USER
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class Sumis10 {
public static void main(String[] args) {
// TODO Auto-generated method stu
int[] arr = new int[11];
for (int j = 1; j <= 10; j++) {
arr[j] = j;
}
int sum = 10;
HashMap<Integer, Integer> hm = new HashMap<>(arr.length);
for (int j = 0; j < arr.length; j++) {
hm.put(arr[j], sum - arr[j]);
}
Scanner sc = new Scanner(System.in);
String a = sc.next();
sc.close();
// System.out.println(a);
String[] numberarrstream = a.split(",");
ArrayList<Integer> differeneLeft = new ArrayList<>();
int streamNumber = 0;
boolean is_found = false;
for (String s : numberarrstream) {
streamNumber = Integer.parseInt(s);
for (Integer number : differeneLeft) {
if (streamNumber == number) {
is_found = true;
break;
}
}
if (is_found)
break;
if (hm.containsKey(streamNumber)) {
int differenceleft = hm.get(streamNumber);
differeneLeft.add(differenceleft);
}
}
System.out.println("First Combination of number 10 " + ">>>" + streamNumber);
}
}
package com.test.staticthread;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
public class CountAmazonInTwoDArray {
public static void main(String[] args) {
// TODO Auto-generated method stub
char[][] arr = {{'B', 'B', 'A', 'B', 'B', 'N'},
{'B', 'B', 'M', 'B', 'B', 'O'}, {'B', 'B', 'A', 'B', 'B', 'Z'},
{'N', 'O', 'Z', 'B', 'B', 'A'}, {'B', 'B', 'B', 'B', 'B', 'M'},
{'B', 'B', 'B', 'B', 'B', 'A'}};
HashMap<Character, Integer> hm = new HashMap<>();
ArrayList<Character> arrayList = new ArrayList<>();
String s = "AMAZON";
for (char ch : s.toCharArray())
arrayList.add(ch);
int rows = arr.length;
int colums = arr[0].length;
for (int j = 0; j < rows; j++) {
for (int k = 0; k < colums; k++) {
char ch = arr[j][k];
if (arrayList.contains(ch)) {
if (hm.containsKey(ch)) {
hm.put(ch, hm.get(ch) + 1);
} else {
hm.put(ch, 1);
}
}
}
}
//what we are doing is trying to sort the list entry object by frequency of given charcter in 2 d array.
//limiting factor in creating string will be one with lowest number of charcters/
List<Entry<Character, Integer>> list = new ArrayList<>(
hm.entrySet());
Collections.sort(list, new Comparator<Entry<Character, Integer>>() {
@Override
public int compare(Entry<Character, Integer> o1,
Entry<Character, Integer> o2) {
// TODO Auto-generated method stub
return o1.getValue() - o2.getValue();
}
});
Entry<Character, Integer> entry1 = list.get(0);
if (entry1.getKey() == 'A') {
if (entry1.getValue() % 2 != 0) {
System.out.println(entry1.getValue() - 1);
}
else {
System.out.println(entry1.getValue() / 2);
}
} else {
System.out.println(entry1.getValue());
}
}
}
package com.test.staticthread;
import java.awt.color.CMMException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
public class MaximumNumberOfusersLoggedin {
public static void main(String[] args) {
// TODO Auto-generated method stub
String users = "user1, user4, user2, user1, user3, user1, user2, user3";
int k = 3;
HashMap<String, Integer> hm = new HashMap<>();
for (String user : users.split(",")) {
String temp = user.trim();
if (hm.containsKey(temp))
hm.put(temp, hm.get(temp) + 1);
else
hm.put(temp, 1);
}
List<Entry<String, Integer>> list=new ArrayList<>(hm.entrySet());
Collections.sort(list, new Comparator<Entry<String, Integer>>() {
@Override
public int compare(Entry<String, Integer> o1,
Entry<String, Integer> o2) {
// TODO Auto-generated method stub
return o2.getValue()-o1.getValue();
}
});
for(int i=0;i<k;i++)
{
Entry<String, Integer> entry=list.get(i);
System.out.println(entry.getKey()+"("+entry.getValue()+")");
}
}
}
will incoming stream be a structured so as to fit in binary search tree.that is if incoming stream is 1,2,3,4,5.?how will we build bst without looking all the elements.we can build BST out of sorted int array(but again this violates your not storing all the stuff in the element).
I think the best would be to do arraylist/set to store the stuff.that is keep on adding in arraylist,untill you hit same element again.(if(set.contains(incoming element) do a break and come out of the loop in this way you won;t have stored all the elements and and search is also linear,unless specified way the data is coming on stream,I think only viable solution is to use arraylist or set to do a contains/add method and break if it return true.
import java.util.ArrayList;
import java.util.Random;
public class FindDuplicates {
public static void main(String[] args) {
// TODO Auto-generated method stub
Random random = new Random();
ArrayList<Integer> arrayList = new ArrayList<>();
int randomnumber = 0;
for (int i = 0; i < 1000; i++) {
randomnumber = random.nextInt(10);
if (arrayList.contains(randomnumber)) {
break;
}
}
System.out.println(randomnumber +">>>>>>>>>>>>" +"has already been processed ");
}
}
package com.interview.multithreaded;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
public class SearchASubStringInword {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "banana";
Set<String> set = new HashSet<>();
ArrayList<String> arrlist = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
for (int j = i + 1; j < s.length() + 1; j++) {
String temp = s.substring(i, j);
if (!set.add(temp)) {
arrlist.add(temp);
// System.out.println(temp);
}
}
}
Collections.sort(arrlist, new Comparator<String>() {
public int compare(String o1, String o2) {
// TODO Auto-generated method stub
return o2.length() - o1.length();
}
});
System.out.println(arrlist.get(0));
}
}
just put the stuff in list and while putting the stuff in list check it element is already there.if yes.then remove it and add same element and when you are done with your inputstream,you are left with least recent as first entry(oth index) in list.list.getIndex(0)
import java.util.ArrayList;
public class LeastRecentOccuredMessages {
public static void main(String[] args) {
// TODO Auto-generated method stub
String message = "m";
ArrayList<String> arrayList = new ArrayList<>();
java.util.Random random = new java.util.Random();
for (int i = 1; i < 20; i++) {
int k = random.nextInt(5);
String temp = message + k;
if (arrayList.contains(temp)) {
arrayList.remove(temp);
arrayList.add(temp);
}
else
{
arrayList.add(temp);
}
}
System.out.println("least recent message is"+">>>"+arrayList.get(0));
}
}
import java.util.HashMap;
import java.util.Map.Entry;
public class NeeedtoMakePalindrome {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "aabbbccddee";
// if string is of even length then frequenecy of all chars should be
// even.
// if string is odd length than atmost one char can have odd frequency
// aabcc(correct, a palidrome can be made out of this pattern)
// aaabbef(not a palindrome)
HashMap<Character, Integer> hm = new HashMap<>();
int len = s.length();
for (char ch : s.toCharArray()) {
if (hm.containsKey(ch)) {
hm.put(ch, hm.get(ch) + 1);
} else {
hm.put(ch, 1);
}
}
HashMap<Character, Integer> paddingmap = new HashMap<>();
int totalpaddingneeded = 0;
if (len % 2 == 0) {
for (Entry<Character, Integer> entry : hm.entrySet()) {
Integer value = entry.getValue();
totalpaddingneeded = 0;
if (value % 2 == 1) {
totalpaddingneeded += 1;
paddingmap.put(entry.getKey(), totalpaddingneeded);
}
}
if (paddingmap.size() > 0) {
for (Entry<Character, Integer> entry : paddingmap.entrySet()) {
System.out.println(entry.getKey() + ">>>needed to apppened"
+ ">>>" + entry.getValue() + ">>>" + "times");
}
} else {
System.out.println("Already palindrome can be construcuted");
}
}
else {
System.out.println("when length is odd");
int count = 0;
for (Entry<Character, Integer> entry : hm.entrySet()) {
Integer value = entry.getValue();
totalpaddingneeded = 0;
if (value % 2 == 1) {
count++;
if (count >= 1) {
totalpaddingneeded += 1;
paddingmap.put(entry.getKey(), totalpaddingneeded);
}
}
}
if (paddingmap.size() > 0) {
for (Entry<Character, Integer> entry : paddingmap.entrySet()) {
System.out.println(entry.getKey() + ">>>needed to apppened"
+ ">>>" + entry.getValue() + ">>>" + "times");
}
}
}
}
}
it is brute force,there should be better way to do the same.
public class SubArrayWithsum {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[]arr={1,3,5,18};
boolean is_found=false;
int sumdesired=8;
int currentsum=0;
for(int j=0;j<arr.length;j++)
{
currentsum=0;
for(int k=j;k<arr.length;k++)
{
currentsum+=arr[k];
if(currentsum==sumdesired)
{
is_found=true;
System.out.println("yes there exists contigous array with that sum");
break;
}
}
if(is_found)
break;
}
}
}
How about brute force.??
public class BleakNumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
int number = 10;
int counter = 0;
boolean is_bleak = true;
for (int i = 1; i < number - 1; i++) {
counter = 0;
String s = Integer.toBinaryString(i);
for (int j = 0; j < s.length(); j++) {
if (s.charAt(j) == '1')
counter++;
}
if (i + counter == number) {
System.out.println("Not bleak");
is_bleak = false;
break;
}
}
if (is_bleak) {
System.out.println("Bleak");
}}}
package com.interview.array;
import java.util.ArrayList;
import java.util.Collections;
public class FindSubString {
public static void main(String[] args) {
// TODO Auto-generated method stub
completeFunction("aab");
}
private static void completeFunction(String a) {
ArrayList<String> responseList = new ArrayList<>();
ArrayList<Character> arrlist = new ArrayList<>();
arrlist.add('a');
arrlist.add('e');
arrlist.add('i');
arrlist.add('o');
arrlist.add('u');
for (int i = 0; i < a.length(); i++) {
for (int j = i + 1; j < a.length() + 1; j++) {
String temp = a.substring(i, j);
if (arrlist.contains(temp.charAt(0))
&& !arrlist.contains(temp.charAt(temp.length()-1)))
responseList.add(temp);
//System.out.println(a.substring(i, j));
}}
Collections.sort(responseList);
String responesString = responseList.get(0) + ","
+ responseList.get(responseList.size()-1);
System.out.println(responesString);}}
package com.interview.array;
public class MinimumLengthWord {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = {10, -4, 6, 2, 8, 9, 4};
int max = 0;
int count = 0;
for (int i = 0; i < arr.length - 1; i++) {
if (arr.length - i < max) {
break;
}
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] > arr[i])
count++;
}
if (count > max) {
max = count;
}
count = 0;
}
System.out.println(max);}}
Possible Approach I can think of is as follow.s
1>Let us say set of chars is of length 7{d,a,r,l,i,n,g}.
2.Start from point/index where dictinary starts with atleast 7 chars.
3.for all 7 digits in dictionary,we need to find sum of ascii values.and compare it with ascii sum of charcter string.and see if there is match.conitnue for rest of dictionary where length>7.
we can eliminate 19 chars check by using starts with()
Any one has better approach,please suggest.
import java.util.ArrayList;
public class SetOfpointsOODesignStrcuture {
class firstQuardantPoints extends Coordinates {
public firstQuardantPoints(int x, int y) {
// TODO Auto-generated constructor stub
if (x < 0 || y < 0)
throw new IllegalArgumentException(
"First Quardant points cannot be negative");
this.setX(x);
this.setY(y);
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
firstQuardantPoints points = (firstQuardantPoints) obj;
if (points.getX() == this.getX() && points.getY() == this.getY()) {
return true;
}
return false;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return 31 * this.getX() * this.getY();
}
}
class AnotherSetOfpoints extends Coordinates {
public AnotherSetOfpoints(int x, int y) {
// TODO Auto-generated constructor stub
this.setX(x);
this.setY(y);
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (obj == this)
return true;
if (obj == null)
return false;
Coordinates points = (Coordinates) obj;
if (points.getX() == this.getX() && points.getY() == this.getY()) {
return true;
}
return false;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return 31 * this.getX() * this.getY();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// create firstQuardant sets
SetOfpointsOODesignStrcuture designStrcuture = new SetOfpointsOODesignStrcuture();
// SetOfpointsOODesignStrcuture.firstQuardantPoints
// firstQuardantPoints1= designStrcuture.new firstQuardantPoints(1, 2);
ArrayList<firstQuardantPoints> arrayList = new ArrayList<>();
for (int i = 0; i < 7; i++) {
arrayList.add(designStrcuture.new firstQuardantPoints(i, i + 1));
}
ArrayList<AnotherSetOfpoints> anotherSetOfpoints = new ArrayList<>();
for (int i = 2; i < 8; i++) {
anotherSetOfpoints
.add(designStrcuture.new AnotherSetOfpoints(i, i + 1));
}
for (AnotherSetOfpoints coordinates : anotherSetOfpoints) {
if (arrayList.contains(coordinates)) {
System.out.println(coordinates.getX() + "," + coordinates.getY()
+ ">>>" + "is in list");
} else {
System.out.println(coordinates.getX() + "," + coordinates.getY()
+ ">>>" + "is not in list");
}}}}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class SetOFStrings {
public static void main(String[] args) {
// TODO Auto-generated method stub
// A-b objcts that belong to A and not to b;
ArrayList<String> arr1 = new ArrayList<>();
ArrayList<String> arr2 = new ArrayList<>();
arr1.add("munny");
arr1.add("sas");
arr1.add("sunnynnnn");
arr1.add("ciaia");
arr2.add("munny");
arr2.add("sas");
arr2.add("nana");
arr2.add("mama");
arr2.add("sharma");
arr2.add("wayoming");
Iterator<String> itr = arr1.iterator();
while (itr.hasNext()) {
String temp = itr.next();
if (arr2.contains(temp)) {
arr2.remove(temp);
itr.remove();
}}
Collections.sort(arr1);
Collections.sort(arr2);
arr1.addAll(arr2);
for (String a : arr1)
System.out.println(a)}}
import java.util.Arrays;
import java.util.Random;
public class MaximumProduct {
public static void main(String[] args) {
// TODO Auto-generated method stub
Random random = new Random();
int[] array = new int[200];
for (int i = 0; i < 200; i++) {
array[i] = random.nextInt(10);
}
Arrays.sort(array);
int maxproduct = 1;
for (int j = array.length - 1; j >= array.length - 3; j--) {
maxproduct *= array[j];
}
System.out.println(maxproduct);
}
}
/**
- MrWayne January 21, 2017*
*/
package com.learn.hazlecast.DistributedRun;
public class IndexValueFind {
public static void main(String[] args) {
int[]arr={2,3,4,5,6,7};
int[]newarr=new int[arr.length];
int product=1;
for(int j=0;j<arr.length;j++){
product*=arr[j];
}
for(int m=0;m<arr.length;m++){
newarr[m]=product/arr[m];
}
for(int j:newarr)
{
System.out.println(j);
}
}
}