PeyarTheriyaa
BAN USERThis is my take on the question
case num ==0 output Zero
case num != 0
split it in 1000s (0-999) append thousand, million or billion appropriately
split it in 100s (0-99)
case 0-19 append the word from n0to19
case 20-99 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);
}
}
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);
}
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 8-12 + 2-5 = 7 hours
total mappings 500
avg 500 / 7 = 71.43
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");
}
}
}
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;
}
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;
}
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;
}
}
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]);
}
}
}
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());
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();
}
}
}
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);
}
}
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");
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;
}
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;
}
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
- PeyarTheriyaa August 21, 2018