PeyarTheriyaa
BAN USERThis is how I'd solve it
1 sort the list with the min value of the two
2 add the NY cost if NY is min or sf cost if sf is min only until either of the counts become 50
3 on any one count (NY/SF) reaches 50 add the rest of the list to the other
public class MinSumBt2 {
public static void main(String[] args) {
ArrayList<CostPair> list = new ArrayList<>();
try (Scanner in = new Scanner(new FileReader("inputMinSum"))) {
while (in.hasNext()) {
list.add(new CostPair(in.nextInt(), in.nextInt()));
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
list.sort(CostPair::compareTo);
System.out.println(list);
int sum = 0;
int countNY = 0;
int countSF = 0;
for (CostPair tmp : list) {
if (countNY < 50 && countSF < 50) {
if (tmp.isNYMin) {
sum += tmp.toNY;
countNY++;
}
else {
sum += tmp.toSF;
countSF++;
}
}
else if (countNY < 50) {
sum += tmp.toNY;
countNY++;
}
else {
sum += tmp.toSF;
countSF++;
}
}
System.out.println(sum);
}
static class CostPair implements Comparable<CostPair> {
int toNY;
int toSF;
boolean isNYMin;
CostPair(int toNY, int toSF) {
this.toNY = toNY;
this.toSF = toSF;
isNYMin = toNY < toSF;
}
private int min() {
return (toNY < toSF) ? toNY : toSF;
}
private int max() {
return (toNY < toSF) ? toSF : toNY;
}
@Override
public int compareTo(CostPair c) {
if (min() == c.min())
return c.max()  max();
return min()  c.min();
}
@Override
public String toString() {
return "[" + toNY + "," + toSF + "]";
}
}
}

PeyarTheriyaa
September 22, 2018 This is how I would do it
1 list.add() adds only if duplicate not found, else increment existing
2 comparable interface on Entry class implementing compareTo()
3 sort list using the sortt command pass the compareTo of Entry to sort
4 after sorting printTopTen()
public class VideoTopTen {
public static void main(String[] args) {
// populate list using List list.add() eg.
List.add(new Entry("Mine", 1234));
List.add(new Entry("Yours", 5555));
List.add(new Entry("Mine", 1234));
List.list.sort(Entry::compareTo);
List.printTop10();
}
static class List {
static ArrayList<Entry> list;
static {
list = new ArrayList<>();
}
static void add(Entry tmp) {
boolean flag = true;
for (Entry e : list)
if (e.name.equals(tmp.name)) {
e.count += tmp.count;
flag = false;
break;
}
if (flag)
list.add(tmp);
}
static void printTop10() {
int i = 0;
for (Entry e : list) {
System.out.println(e.name + e.count);
if (i == 9) break;
i++;
}
}
}
static class Entry implements Comparable<Entry> {
String name;
int count;
Entry(String name, int count) {
this.name = name;
this.count = count;
}
@Override
public int compareTo(Entry tmp) {
return tmp.count  count;
}
}
}

PeyarTheriyaa
September 04, 2018 using a program to solve this,
Note: it also prints out the combination
public class Com1ToN
{
static int count;
public static void main(String[] args)
{
count = 0;
System.out.print("Enter N : ");
int n = (new Scanner(System.in)).nextInt();
for (int i = 0; i < n; i++)
print(1, n, i, "");
System.out.println("The count is : " + count);
}
private static void print(int s, int n, int lvl, String pre)
{
if (lvl == 0)
for (int i = s; i <= n; i++) {
System.out.println(pre + i);
count++;
}
else
for (int i = s; i <= n  lvl; i++)
print(i + 1, n, lvl  1, pre + i + " ");
}
}

PeyarTheriyaa
August 22, 2018 This is a bit too mathematical...
choosing 1 from n is nC1
choosing 2 from n is nC2
choosing 3 from n is nC3... till
choosing n from n is nCn
each combination should be added to get the total no of possible formations
nC1+nC2+...+nCn
(without going into the derivation its known that) nC0+nC1+...+nCn=2^n
nC0=1
therefore
nC1+nC2+...+nCn=(2^n)nC0
so, nC1+nC2+...+nCn=(2^n)1
This is how I'd do it,
1 get the points, populate in an ArrayList
2 call Collections.sort on the list with user defined Comparator
3 print the first n points
Note: If we don't use an ArrayList store the points in an array, manually sort the array using the Euclidean distance
simplifications done:
1 Distance from origin sqrt(x^2+y^2)
2 removal of sqrt from the equation cause squares, square roots and n considered here are always proportional under the given problem circumstances
public class NClosePoints
{
static List<Point> list;
public static void main(String[] args)
{
list = new ArrayList<>();
Scanner in = new Scanner(System.in);
System.out.print("Enter no of points : ");
int size = in.nextInt();int i;
for (i = 0; i < size; i++) {
Point tmp = new Point(in.nextInt(), in.nextInt());
list.add(tmp);
}
System.out.print("Enter N of N closest points needed : ");
int n = in.nextInt();
Collections.sort(list, new Comparator<Point>(){
@Override
public int compare(Point p1,Point p2){
return (int) ((Math.pow(p1.x, 2)+Math.pow(p1.y, 2))(Math.pow(p2.x, 2)+Math.pow(p2.y, 2)));
}
});
i=0;
for (Point tmp : list) {
if(i<n)
System.out.println("Point "+(i+1)+" : "+tmp);
else
break;
i++;
}
}
static class Point
{
int x, y;
Point(int i, int j)
{
x = i;
y = j;
}
@Override
public String toString(){
return "( "+x+" , "+y+" )";
}
}
}

PeyarTheriyaa
August 21, 2018 This is my take on the question
case num ==0 output Zero
case num != 0
split it in 1000s (0999) append thousand, million or billion appropriately
split it in 100s (099)
case 019 append the word from n0to19
case 2099 append tens place from n10s and ones place from n0to19
split hundreds place append word from n0to19 and hundred if not empty
Note : need to append "and" if number is less than 1000 and has tens and ones place
public class Num2W
{
static String[] n0to19 = {"", " One", " Two", " Three",
" Four", " Five", " Six", " Seven",
" Eight", " Nine", " Ten", " Eleven",
" Twelve", " Thirteen", " Fourteen", " Fifteen",
" Sixteen", " Seventeen", " Eighteen", " Nineteen"};
static String[] n10s = {" Twenty", " Thirty", " Fourty", " Fifty",
" Sixty", " Seventy", " Eighty", " Ninety"};
static String[] nmulk = {"", " Thousand,", " Million,", " Billion,"};
private static String convert(int n, int sfix)
{
String inW = "";
if (n != 0) {
int tmp = n % 100;
if (tmp != 0) {
if (tmp < 20)
inW = n0to19[tmp];
else
inW = n10s[(tmp / 10)  2] + n0to19[tmp % 10];
if (sfix == 0)
inW = " and" + inW;
}
tmp = n / 100;
if (tmp != 0)
inW = n0to19[tmp] + " Hundred" + inW;
inW += nmulk[sfix];
}
return inW;
}
public static void main(String[] args)
{
int n = (new Scanner(System.in)).nextInt();
String out = "";
if (n == 0)
out = " Zero";
else
for (int i = 0; n != 0; i++) {
out = convert(n % 1000, i) + out;
n /= 1000;
}
System.out.println("In Words :" + out);
}
}

PeyarTheriyaa
August 21, 2018 Here's what I'd do
find repetition and remove if more than 3, repeat
public static void main(String[] args)
{
String input = (new Scanner(System.in)).nextLine();
for (int i = 0; i < input.length(); i++) {
int j = i;
while (j < input.length() && input.charAt(j) == input.charAt(i))
j++;
if (j  i > 3) {
input = input.substring(0, i) + input.substring(j);
System.out.println(input);
i = 0;
}
}
System.out.println("Final string : " + input);
}

PeyarTheriyaa
August 17, 2018 not sure if the lunch time is counted as working hours
1 if lunch is counted too
total of 8 am to 5 pm 9 hours
total mappings 350 + 150 = 500
avg 500 / 9 = 55.56
2 if lunch isn't counted
total of 812 + 25 = 7 hours
total mappings 500
avg 500 / 7 = 71.43

PeyarTheriyaa
August 13, 2018 convert it into a matrix and apply some shortest path algorithm, ex Dijkstra algorithm for directed weighted graph.. If you need a program reply to this comment and do tell me the language you want this solved
 PeyarTheriyaa August 06, 2018something like this might work better
basically prime numbers are numbers that are only divisible by themselves and 1
but cheating at it we can check for n
1 divisibility by numbers less than n/2
2 and only by other prime numbers less than n/2
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the limit : ");
int n = in.nextInt();
boolean flag;
List<Integer> arr = new ArrayList<>();
for (int cntr = 2; arr.size() < n; cntr++) {
flag = true;
if (!arr.isEmpty()) {
int index = 0;
for (int slct = arr.get(index++); slct <= cntr / 2; slct = arr.get(index++))
if (cntr % slct == 0) {
flag = false;
break;
}
}
if (flag) {
arr.add(cntr);
System.out.print("" + cntr + "\t");
}
}
}

PeyarTheriyaa
July 22, 2018 I would suggest doing something like this...
1 send the ans, arr, index and current ans recursively
2 the good thing about using recursion is it reduces complexity of the idea
3 using OR '' operator ensures that the program does not compute
through all combinations since it greedily returns true if it encounters a true
midway without processing the rest of the code
private static boolean isMgic(int ans, int[] arr)
{
return isMgic(ans, arr, 0, 0);
}
private static boolean isMgic(int ans, int[] arr, int index, int tmp)
{
if (arr.length > index)
return (isMgic(ans, arr, index + 1, tmp + arr[index])
 isMgic(ans, arr, index + 1, tmp  arr[index]));
if (ans == tmp)
return true;
return false;
}

PeyarTheriyaa
July 11, 2018 My answer in Java would be like this
private static String addNumbers(String no1, String no2)
{
String sum = "";
int bal = 0;
int max = no1.length() > no2.length() ? no1.length() : no2.length();
for (int count = 1; count <= max  bal > 0; count++) {
if (count <= max){
if (no1.length()  count >= 0)
bal += (no1.charAt(no1.length()  count)  '0');
if (no2.length()  count >= 0)
bal += (no2.charAt(no2.length()  count)  '0');
}
sum = "" + (bal % 10) + sum;
bal /= 10;
}
return sum;
}

PeyarTheriyaa
July 08, 2018 Something like this should be good
would return the index if the word is found
will return 1 if no word found matching the given string with wild card
do not forget to populate the index
class Lookup
{
static List<String> words;
static int isPresent(String lookup)
{
int index = lookup.indexOf('*');
String l1 = lookup.substring(0, index);
String l2 = lookup.substring(index + 1);
for (int i = 0; i < words.size(); i++)
if (words.get(i).substring(0, index).compareTo(l1) == 0
&& words.get(i).substring(index + 1).compareTo(l2) == 0)
return i;
return 1;
}
}

PeyarTheriyaa
July 06, 2018 there are a few ways to go about it..
1 split it in half (equal parts) and weigh both against each other,
which half has lower weight has the defective coin,
repeat until you have only one left
2 split it in 4 or 3 (equal) parts compare 2,
if one has lower the defective coin is in that part,
if both weigh the same the defective part is in
the 2 or 1 part that was not compared
usually explain the first part, and if they say this can be optimised then explain the second method(pause enough so that your answer seems legit ðŸ˜Š)
its actually n complexity... since we parse only once through the given string.. I'd say that 's about as low as one can go... since we need to parse at least once to look through the string
 PeyarTheriyaa June 01, 2018hope this helps...
public class CharCount
{
public static void main(String[] args)
{
System.out.print("Enter String to find count of individual char : ");
Scanner in = new Scanner(System.in);
String string = in.next();
char[] c = new char[string.length()];
int[] count = new int[string.length()];
int end = 0;
boolean flag;
for (int i = 0; i < string.length(); i++)
{
flag = true;
for (int j = 0; j < end; j++)
{
if (string.charAt(i) == c[j])
{
flag = false;
count[j]++;
}
}
if (flag)
{
c[end] = string.charAt(i);
count[end] = 1;
end++;
}
}
for (int i = 0; i < end; i++)
{
// System.out.println("char : " + c[i] + " count : " + count[i]);
System.out.print(""+count[i]+c[i]);
}
}
}

PeyarTheriyaa
May 26, 2018 Using java, this code should work,
int max = 1;
for (ArrayList<Integer> i : mulList)
max = (max < i.size()) ? i.size() : max;
tmp = new ArrayList<>();
for (int j = 0; j < max; j++)
for (ArrayList<Integer> i : mulList)
try {
tmp.add(i.get(j));
}
catch (IndexOutOfBoundsException e) {}
System.out.println(tmp.toString());

PeyarTheriyaa
April 19, 2018 I did something similar to the scala one..
1. loop through the entire string
1.a. if ( is found push it in stack
1.b.i if ) is found pop it from stack
1.b.ii if ) is found and there is no matching ( <empty stack> then remove )
2 at the end stack has all the ( with no matching ) and so remove all ( in the stack
import java.io.IOException;
import java.util.EmptyStackException;
import java.util.Scanner;
import java.util.Stack;
public class Solution1a
{
private static final Scanner in = new Scanner(System.in);
private static String input;
public static void main(String[] args) throws IOException
{
input = in.nextLine();
balPara();
System.out.println(input);
}
private static void balPara()
{
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < input.length(); i++)
if (input.charAt(i) == '(')
stack.push(i);
else if (input.charAt(i) == ')')
try {
stack.pop();
}
catch (EmptyStackException e) {
input = input.substring(0, i) + input.substring(i + 1);
i;
}
while (stack.size() > 0) {
input = input.substring(0, stack.peek()) + input.substring(stack.peek() + 1);
stack.pop();
}
}
}

PeyarTheriyaa
April 08, 2018 I think this would be easier... Note: don forget to write code to populate the tree
import java.util.List;
public class RobNTree
{
static int max;
static class TreeNode
{
int val;
List<TreeNode> kids;
}
public static void main(String[] args)
{
max = 0;
TreeNode root = null;
// populate tree
if (root != null)
traverse(root, 0);
System.out.println("The max possible loot is " + max);
}
private static void traverse(TreeNode node, int sum)
{
sum += node.val;
if (node.kids.isEmpty())
max = (sum > max) ? sum : max;
else
for (int i = 0; i < node.kids.size(); i++)
traverse(node.kids.get(i), sum);
}
}

PeyarTheriyaa
April 01, 2018 in java Math.random() generates values between 0 to <1 with almost equal distribution over large no of iterations... since it is so the following code could work
if(Math.random()<0.67)
System.out.print("Heads");
else
System.out.print("Tails");

PeyarTheriyaa
March 08, 2018 have a string alpabet lookup... remove chars from it if its presnt inthe input string...
print remaining letters from the alphabet lookup string
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the string : ");
String input = in.nextLine();
String out = checkPangram(input);
if (out.length() == 0)
System.out.println("Pangram");
else
System.out.println("The letters missing : " + out);
}
private static String checkPangram(String input)
{
input = input.toLowerCase();
String alph = "abcdefghijklmnopqrstuvwxyz";
int max = 26;
for (int i = 0; i < max; i++)
{
int index = input.indexOf(alph.charAt(i));
if (index != 1)
{
alph = alph.substring(0, i) + alph.substring(i + 1);
i;
max;
}
}
return alph;
}

PeyarTheriyaa
March 08, 2018 this would be my answer, with a few holes of course...
private static String addNumbers(String[] no) // assumes all given strings are in decimal no format
{
String sum = "";
int bal = 0;
int max = 0;
for (String no1 : no) // max length of string nos
if (max < no1.length())
max = no1.length();
for (int count = 1; count <= max  bal > 0; count++)
{
for (String no1 : no) // \ find sum of individual digits
if (no1.length()  count >= 0) // > assumes that the sum of individual
bal += (no1.charAt(no1.length()  count)'0'); // / digits (bal) never exceeds int max limit
sum=""+(bal%10)+sum;
bal/=10;
}
return sum;
}

PeyarTheriyaa
March 07, 2018 Open Chat in New Window
Something like this should work
 PeyarTheriyaa September 22, 2018