martin1990
BAN USER- 0of 0 votes
AnswersGiven a number N and a number K, write a program to find maximum value of N%i where 1<=i<=K.
What is wrong with below solution:
- martin1990 in Indiaimport java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { PrintWriter printwriter = new PrintWriter(System.out); Scanner scanner = new Scanner(System.in); double N = scanner.nextDouble(); int K = scanner.nextInt(); int i = 2, lowerLimit = 0; while (K <= N / i) { i++; } lowerLimit = (int) (N / i); int result = (int) Math.max(N % lowerLimit, N % (lowerLimit + 1)); printwriter.println(result); printwriter.flush(); } }
| Report Duplicate | Flag | PURGE
- 1of 1 vote
Answerspublic class WithoutConditional { static void callme(String s) { System.out.println("string"); } static void callme(Object s) { System.out.println("object"); } public static void main(String[] args) { callme(null); } }
The above code prints "string". Even if we change the order of the methods.
- martin1990 in India
My question is why it doesn't print "object" as String is also instanceof Object. Passing null as argument shouldn't print "object" ???| Report Duplicate | Flag | PURGE
- -3of 3 votes
AnswersHow can I find the shortest distance between the first and the last element in a two dimension Array of 0's and 1's.
- martin1990 in India
Given if the element is 1 we can move left or down, if it is 0 we can only proceed downwar| Report Duplicate | Flag | PURGE
Samsung Software Engineer / Developer Java
Adding one more argument to the match method will work , I guess
private static boolean match(String input1, String input2, int i, int j,char previousChar) {
if(i==0 && j==0)
return true;
else{
if(input1.charAt(0)==input2.charAt(0)||input2.charAt(0)=='.')
return match(input1.substring(1),input2.substring(1),i-1,j-1,input2.charAt(0));
else if(previousChar==input1.charAt(0) && input2.charAt(0)=='*'){
return match(input1.substring(1),input2.substring(1),i-1,j-1,input2.charAt(0))||match(input1.substring(1),input2,i-1,j,previousChar);
}
}
return false;
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CheckPalindrome {
public static void main(String[] args) throws IOException {
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number");
int input1 = Integer.parseInt(obj.readLine());
boolean isPalindrome = checkPalindrome(input1);
if (isPalindrome) {
System.out.println("The input is Palindrome");
} else {
boolean isPalindromePossible = checkifPalindromePossible(input1);
if(!isPalindromePossible)
{
System.out.println("Palindrome number not possible");
}
}
}
private static boolean checkifPalindromePossible(int number) {
int digitsCount[] = new int[10];
int remainder = 0, numberOfDigitsWithOddCount = 0, inputLength = 0,temp=number;
while (number > 0) {
remainder = number % 10;
digitsCount[remainder]++;
number /= 10;
inputLength++;
}
for (int i = 0; i < 10; i++) {
if (digitsCount[i] % 2 == 1) {
numberOfDigitsWithOddCount++;
}
}
if (numberOfDigitsWithOddCount > 1)
return false;
else {
char[] characterArray=new char[inputLength];
int charPlace = 0;
for (int i = 0; i < 10; i++) {
if (digitsCount[i] % 2 == 0 && digitsCount[i] > 0) {
characterArray[charPlace]= (char) ('0' + i);
characterArray[inputLength - charPlace-1]=(char) ('0' + i);
charPlace++;
} else if (digitsCount[i] % 2 == 1) {
characterArray[(int) Math.ceil(inputLength / 2)]=(char) ('0' + i);
}
}
System.out.println(String.valueOf(characterArray));
return true;
}
}
private static boolean checkPalindrome(int input1) {
int temp = input1, rem = 0, temp2 = 0;
while (temp > 0) {
rem = temp % 10;
temp2 = temp2 * 10 + rem;
temp /= 10;
}
if (input1 == temp2)
return true;
return false;
}
}
Here is a basic recursive solution. Few check like the first string should only have alphabets and digits can also be added
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class StringMatch {
public static void main(String[] args) throws IOException {
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the main String");
String input1 = obj.readLine();
System.out.println("Enter the second String ");
String input2 = obj.readLine();
boolean matchSuccessful=match(input1,input2,input1.length(),input2.length());
System.out.println(matchSuccessful);
}
private static boolean match(String input1, String input2, int i, int j) {
if(i==0 && j==0)
return true;
else{
if(input1.charAt(0)==input2.charAt(0)||input2.charAt(0)=='.')
return match(input1.substring(1),input2.substring(1),i-1,j-1);
else if(input2.charAt(0)=='*'){
return match(input1.substring(1),input2.substring(1),i-1,j-1)||match(input1.substring(1),input2,i-1,j);
}
}
return false;
}
}
I think your solution will not work for "aaaa" and "aaa". You should also have length check for both the strings a and b.
- martin1990 February 11, 2014Here is a sample code.But it will print all three rotation for the input "ooo".
package com.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class AllRotation {
public static void main(String[] args) throws IOException {
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the String whose Rotation is to be found");
String input = obj.readLine();
System.out.println("Enter the String to check whether it's rotation of first String");
String input2 = obj.readLine();
boolean isRotated = false;
for (int index = 0; index < input.length(); index++) {
String rotatedString = input.substring(index)+ input.substring(0, index);
System.out.println(rotatedString);
if (rotatedString.equals(input2)) {
isRotated = true;
}
}
System.out.println("The Second input is Rotation of first " + isRotated);
}
}
@Anonymous
It was {-2,2,4} not {-2,-2,4}. Sum of the remaining in first case will be 0 while in second case it is -4. But for both the cases the output should be true.
Could you explain how will it work for {-2,2,4} ?
Here the largest element is 4 and the sum of remaining elements is 0 which means neither 4-0 nor 4+0 equals to 0. But -2-2+4 becomes 0. So, the output should be true.
Take the inputs as string and store them into integer arrays and perform addition something like below:
public class SumOfLargeNumber {
public static void main(String[] args) {
String a = "111111111111";
String b = "22222222222222";
int i = 0;
int firstInputLength = a.length();
int secondInputLength = b.length();
int sizeOfResult = firstInputLength > secondInputLength ? firstInputLength: secondInputLength;
int result[] = new int[sizeOfResult + 1];
int input1[] = new int[sizeOfResult];
int input2[] = new int[sizeOfResult];
while (i < firstInputLength) {
input1[sizeOfResult - i - 1] = Character.getNumericValue((a.charAt(firstInputLength - i - 1)));
i++;
}
i = 0;
while (i < secondInputLength) {
input2[sizeOfResult - i - 1] = Character.getNumericValue(b.charAt(secondInputLength - i - 1));
i++;
}
sumOfInputs(input1,input2,result);
}
private static void sumOfInputs(int[] input1, int[] input2, int[] result) {
int carry=0;
int sizeOfInputs=input1.length;
for(int i=sizeOfInputs;i>0;i--){
int sum = input1[i-1]+input2[i-1]+carry;
result[i]=sum%10;
if(sum/10>10)
carry=1;
else
carry=0;
}
result[0]=carry;
for(int i=0;i<=sizeOfInputs;i++)
System.out.print(result[i]);
}
}
public class SumZeroPossible {
public static void main(String[] args) {
int a[] = { -2, 2,4 };
int sum = a[0];
char sign[] = new char[a.length-1];
boolean ab = isSumZeroPossible(a, 1, sign, sum);
System.out.println(ab);
if(ab){
System.out.print(a[0]);
for (int i = 0; i < sign.length; i++) {
System.out.print(sign[i]);
System.out.print(a[i + 1]);
}
}
}
private static boolean isSumZeroPossible(int[] a, int i, char[] sign,int sum) {
if (sum == 0 && i == a.length)
return true;
else if (i == a.length)
return false;
else {
sign[i - 1] = '-';
if (isSumZeroPossible(a, i + 1, sign, sum - a[i]))
return true;
sign[i - 1] = '+';
if (isSumZeroPossible(a, i + 1, sign, sum + a[i]))
return true;
}
return false;
}
}
I think the probability will become greater than 1 when P=0.5
- martin1990 July 24, 2013yeah Rahul you are correct, the question was if the element was 1 , we can move right or downwards and if it was 0, we can proceed downwards only. Also, we have to find whether the route from the (0,0) to (m-1,n-1) exists.
- martin1990 July 21, 2013
@Balaji: digitsCount[remainder]++ stores the number of times the digit between 0-9 repeats in the given number. For making a number palindrome by altering the position( as per the example mentioned) , only one digit can have odd count and should be placed at the mid position. If there are more than one digits whose count is odd, palindrome number can't be formed from the given number.
- martin1990 March 15, 2014