Scorpion King
BAN USERclass MyOwnException extends Exception {
public MyOwnException(String str) {
super(str);
}
}
class NonRepeatavieChar {
public static char getNonRepeatativeChar(String str1) throws MyOwnException {
if (str1.length() == 0 || str1 == null) {
throw new MyOwnException("Given String is not valid");
}
int[] chArr = new int[26];
String str = str1.toLowerCase();
for (int i = 0; i < str.length(); i++) {
int temp = str.charAt(i) - 'a';
if (temp >= 0 && temp < 26) {
chArr[temp]++;
}
}
for (int i = 0; i < str.length(); i++) {
int temp = str.charAt(i) - 'a';
if (temp >= 0 && temp < 26) {
if (chArr[temp] == 1) {
return str.charAt(i);
}
}
}
return '\0';
}
}
public class FirstNonRepeatativeChar {
public static void main(String[] args) throws MyOwnException {
System.out.println(NonRepeatavieChar
.getNonRepeatativeChar("Salesforce is the best company to work for"));
}
}
class FrequencyCalculator {
public static void calculator(String str) {
int[] ch = new int[26];
for (int i = 0; i < str.length(); i++) {
ch[str.charAt(i) - 'a']++;
}
for (int i = 0; i < ch.length; i++) {
if (ch[i] > 0) {
System.out.print("(" + (char) (i + 'a') + ", " + ch[i] + "),");
}
}
System.out.println("");
}
}
public class CalculateFrequency {
public static void main(String[] args) {
FrequencyCalculator.calculator("abbcdc");
}
}
public static char firstNonRepeatedChar(String str) {
// A good question to ask is what Space is the first non repeated char. Do we consider
// it or not.
if (str.length() == 0 || str == null) {
return ' '; // Depending what interviewer want in this case.
}
// Depending on whether interviewer want take in account UniCode. I am not considering it
// and I am just using 256 ASCII char
int[] charArr = new int[256];
// Question for Interviewer whether we are treating lower and upper case same or not. I am
// treating it same.
String str1 = str.toLowerCase();
for (int i = 0; i < str1.length(); i++) {
int val = (int) str1.charAt(i);
charArr[val]++;
}
for (int i = 0; i < str1.length(); i++) {
int val = (int) str1.charAt(i);
if (charArr[val] == 1) {
return str1.charAt(i);
}
}
return ' ';
}
class HighestPossibleProductSolution {
public static Object highestProductOfThree(int[] arr) {
if (arr.length < 3) {
return null;
} else if (arr.length == 3) {
return (arr[0] * arr[1] * arr[2]);
}
int x;
int y;
int z;
if (arr[0] >= arr[1] && arr[0] >= arr[2]) {
x = arr[0];
if (arr[1] > arr[2]) {
y = arr[1];
z = arr[2];
} else {
y = arr[2];
z = arr[1];
}
} else if (arr[1] >= arr[0] && arr[1] >= arr[2]) {
x = arr[1];
if (arr[0] > arr[2]) {
y = arr[0];
z = arr[2];
} else {
y = arr[2];
z = arr[0];
}
} else {
x = arr[2];
if (arr[0] > arr[1]) {
y = arr[0];
z = arr[1];
} else {
y = arr[1];
z = arr[0];
}
}
for (int i = 3; i < arr.length; i++) {
if (arr[i] >= x) {
z = y;
y = x;
x = arr[i];
} else {
if (arr[i] >= y) {
z = y;
y = arr[i];
} else if (arr[i] > z) {
z = arr[i];
}
}
}
int min1;
int min2;
if (arr[0] > arr[1]) {
min1 = arr[1];
min2 = arr[0];
} else {
min2 = arr[1];
min1 = arr[0];
}
for (int i = 2; i < arr.length; i++) {
if (arr[i] <= min1) {
min2 = min1;
min1 = arr[i];
} else if (arr[i] < min2) {
min2 = arr[i];
}
}
System.out.println("MAX1: " + x + "\t MAX2: " + y + "\t MAX3: " + z + "\t MIN1: "
+ min1 + "\t MIN2: " + min2);
if ((x * min1 * min2) > (x * y * z)) {
return (x * min1 * min2);
}
return (x * y * z);
}
}
import java.util.ArrayList;
class NodeForGetSecondLastAndHalfNode {
public int data;
public NodeForGetSecondLastAndHalfNode next;
public NodeForGetSecondLastAndHalfNode(int iData) {
data = iData;
}
public void display() {
System.out.print(data + " ==> ");
}
}
class ListForGetSecondLastAndHalfNode {
private NodeForGetSecondLastAndHalfNode first;
public ListForGetSecondLastAndHalfNode() {
first = null;
}
public boolean isEmpty() {
if (first == null) {
return true;
}
return false;
}
public void insertData(int data) {
System.out.println("INSERT: " + data);
NodeForGetSecondLastAndHalfNode newElm = new NodeForGetSecondLastAndHalfNode(data);
newElm.next = first;
first = newElm;
}
public void display() {
System.out.println("DISPLAY");
if (isEmpty()) {
System.out.println("EMPTY");
return;
}
NodeForGetSecondLastAndHalfNode curr = first;
while (curr != null) {
curr.display();
curr = curr.next;
}
System.out.println();
}
public NodeForGetSecondLastAndHalfNode getFirst() {
if (isEmpty()) {
return null;
}
return first;
}
}
class GetSecondLastAndHalfNodeSolution {
public Object getSecondLastNode(ListForGetSecondLastAndHalfNode list) {
list.display();
System.out.print("\t SECOND LAST NODE: ");
NodeForGetSecondLastAndHalfNode slow = list.getFirst();
if (list.isEmpty()) {
return null;
} else if (slow.next == null) {
return null;
}
NodeForGetSecondLastAndHalfNode fast = slow.next;
while (fast.next != null) {
slow = slow.next;
fast = fast.next;
}
System.out.println(slow.data);
return slow;
}
public ArrayList<Integer> getHalfNode(ListForGetSecondLastAndHalfNode list) {
list.display();
NodeForGetSecondLastAndHalfNode slow = list.getFirst();
System.out.print("\t MID NODES: ");
ArrayList<Integer> out = new ArrayList<Integer>();
if (list.isEmpty()) {
return out;
} else if (slow.next == null || slow.next.next == null) {
out.add(slow.data);
} else {
NodeForGetSecondLastAndHalfNode fast = slow.next;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
if (fast != null) {
out.add(slow.data);
out.add(slow.next.data);
} else {
out.add(slow.data);
}
}
return out;
}
public void display(ArrayList<Integer> out) {
for (int i = 0; i < out.size(); i++) {
System.out.print(out.get(i) + " , ");
}
System.out.println();
}
}
public static String uppercaseAndReverseAlternately(String str) {
if (str.length() == 0 || str == null) {
return null;
}
String out = "";
String temp = "";
int count = 0;
int i = 0;
while (i < str.length()) {
if (str.charAt(i) == ' ') {
out = out + str.charAt(i);
i++;
} else {
while (i < str.length() && str.charAt(i) != ' ') {
if (count % 2 == 0) {
temp = temp + (str.charAt(i) + "").toUpperCase();
} else {
temp = str.charAt(i) + temp;
}
i++;
}
out = out + temp;
count++;
temp = "";
}
}
return out;
}
class GetKeyIndexFromArraySolution {
/** Will only return one index where given key is present else -1
* @return index where key is present otherwise -1 */
public static int getAnyIndex(int[] arr, int key) {
if (arr.length == 0) {
return -1;
}
int low = 0;
int high = arr.length;
while (low < high) {
int mid = (low + high) / 2;
if (arr[mid] == key) {
return mid;
} else if (key < arr[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
/** This method will return all the index where given key is present
* @param arr
* @param key
* @return */
public static ArrayList<Integer> getAllIndex(int[] arr, int key) {
if (arr.length == 0) {
return null;
}
int start = 0;
int end = arr.length - 1;
boolean fStart = false;
boolean fEnd = false;
while (start <= end) {
if (!fStart && arr[start] == key) {
fStart = true;
}if (!fStart && arr[start] != key) {
start++;
}
if (!fEnd && arr[end] == key) {
fEnd = true;
} else if (!fEnd && arr[end] != key) {
end--;
}
if (fStart && fEnd) {
break;
}
}
ArrayList<Integer> out = new ArrayList<Integer>();
for (int i = start; i <= end; i++) {
System.out.println("IND: " + i);
out.add(i);
}
return out;
}
}
class GetNDigitOfRationalNumber {
public double solution(int numerator, int denominator, int num) throws ArithmeticException{
double out = 0;
double temp = (float) numerator/denominator;
out = (int)(temp * Math.pow(10, num));
return (float) (out/Math.pow(10, num));
}
private String getOutput(int numerator, int denominator, int num) throws ArithmeticException{
String str = "";
int pre = (numerator/denominator + "").length();
double temp = (float) numerator/denominator;
str = str+temp;
System.out.println(str.substring(0,num+pre+1));
return str;
}
public double solution2(int numerator, int denominator, int num) throws ArithmeticException{
return Double.parseDouble(getOutput(numerator, denominator, num));
}
}
import java.util.HashMap;
class ConvertMobileNumberIntoNumber {
public String solution(String str) {
HashMap<String, String> numToStrMap = new HashMap<String, String>();
numToStrMap.put("2", "ABC");
numToStrMap.put("3", "DEF");
numToStrMap.put("4", "GHI");
numToStrMap.put("5", "JKL");
numToStrMap.put("6", "MNO");
numToStrMap.put("7", "PQRS");
numToStrMap.put("8", "TUV");
numToStrMap.put("9", "WXYZ");
String[] arr = str.split("#");
String out = "";
for (int i = 0; i < arr.length - 1; i++) {
out = out + numToStrMap.get("" + arr[i].charAt(0)).charAt(arr[i].length() - 1) + " ";
}
out = out + numToStrMap.get("" + arr[arr.length - 1].charAt(0)).charAt(
arr[arr.length - 1].length() - 1);
return out;
}
}
public class EpicConvertMobileNumberIntoNumber {
public static void main(String[] args) {
ConvertMobileNumberIntoNumber mSol = new ConvertMobileNumberIntoNumber();
System.out.println(mSol.solution("22#22"));
}
}
This pattern is count and say. How it works is starting from 1, next number in series will
"1" -> "One 1" i.e "11"
"11" -> "Two 1" i.e "21"
"21" -> "One 2 One 1" i.e "1211"
"1211" -> "One 1 One 2 Two 1" i.e 111221
.....
class GetCountAndSay {
public String solution(int n) {
if(n == 0) {
return "";
}
String out = "1";
int count = 1;
while (count != n) {
int c = 0;
String in = out;
out = "";
for(int i = 0; i < in.length()-1; i++) {
if(in.charAt(i) == in.charAt(i+1)) {
c++;
} else {
c++;
out = out+("" + c) + in.charAt(i);
c=0;
}
}
c++;
out = out+("" + c) + in.charAt(in.length()-1);
count++;
}
return out;
}
}
public class GoogleGetCountAndSay {
public static void main(String[] args) {
GetCountAndSay mSol = new GetCountAndSay();
System.out.println(mSol.solution(1));
System.out.println(mSol.solution(2));
System.out.println(mSol.solution(3));
System.out.println(mSol.solution(4));
System.out.println(mSol.solution(5));
System.out.println(mSol.solution(6));
System.out.println(mSol.solution(7));
System.out.println(mSol.solution(8));
System.out.println(mSol.solution(9));
System.out.println(mSol.solution(10));
System.out.println(mSol.solution(11));
System.out.println(mSol.solution(12));
}
}
Question was little unclear as what it should return if group size is greater than array size. Considering that as invalid entry below is code:
import java.util.Arrays;
class ReverseArrayByGroupSize {
public void solution(int[] arr, int num) {
if (num > arr.length || num == 0) {
System.out.println("Invalid Entry");
return;
}
int start = 0;
int last = 0;
int end = arr.length / num;
for (int i = 0; i < end; i++) {
start = i * num;
last = (i + 1) * num - 1;
int x = ((last - start) / 2) + start;
int k = last;
for (int j = start; j <= x; j++) {
int temp = arr[j];
arr[j] = arr[k];
arr[k] = temp;
k--;
}
}
if (last != arr.length - 1) {
start = last + 1;
last = arr.length - 1;
while (start <= last) {
int temp = arr[start];
arr[start++] = arr[last];
arr[last--] = temp;
}
}
System.out.println(Arrays.toString(arr));
}
}
public class SalesforceReverseArrayByGroupSize {
public static void main(String[] args) {
ReverseArrayByGroupSize mSol = new ReverseArrayByGroupSize();
mSol.solution(new int[] {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
}, 3);
mSol.solution(new int[] {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13
}, 4);
mSol.solution(new int[] {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
}, 4);
mSol.solution(new int[] {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
}, 6);
mSol.solution(new int[] {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
}, 15);
mSol.solution(new int[] {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
}, 16);
mSol.solution(new int[] {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
}, 17);
}
}
But the requirement is: You can only ask one question to one of them in order to find the way to heaven.
- Scorpion King April 30, 2015NOTE: This solution is for if all the elements in the array are positive. It can be solved in O(n)
class SolutionFindTopNMaximum {
private int getMaximum(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
public void solution(int[] arr, int n) {
int posSize = getMaximum(arr);
boolean[] posArr = new boolean[posSize];
for (int i = 0; i < arr.length; i++) {
posArr[arr[i] - 1] = true;
}
int counter = n;
while (counter > 0 && posSize >= 0) {
if (posArr[posSize - 1]) {
System.out.println(posSize);
counter--;
}
posSize--;
}
}
}
public class FindTopNMaximum {
public static void main(String[] args) {
SolutionFindTopNMaximum mSol = new SolutionFindTopNMaximum();
mSol.solution(new int[] {
2, 1, 20, 3, 6, 5, 4, 8, 11, 12
}, 3);
}
}
class StackFromArray {
private int[] arr;
private int[] pointer;
private int stackSize;
public StackFromArray(int stacksSize) {
arr = new int[stacksSize*2];
this.stackSize = stacksSize;
pointer = new int[]{-1, stacksSize-1};
}
public boolean isEmpty(int stackNum) {
if(pointer[stackNum - 1] == (stackNum - 1) * stackSize -1) {
return true;
}
return false;
}
public boolean isFull(int stackNum) {
if(pointer[stackNum-1] == ((stackNum - 1) * stackSize + stackSize -1)) {
return true;
}
return false;
}
public void push(int stackNum, int data) {
if(isFull(stackNum)) {
System.out.println("Stack: " + stackNum + " is full. Cannot push " + data);
return;
}
arr[++pointer[stackNum - 1]] = data;
}
public int pop(int stackNum) {
if(isEmpty(stackNum)) {
System.out.println("Stack: " + stackNum + " is Empty.");
return -1;
}
return arr[pointer[stackNum - 1]--];
}
public int peek(int stackNum) {
if(isEmpty(stackNum)) {
System.out.println("Stack: " + stackNum + " is Empty.");
return -1;
}
return arr[pointer[stackNum-1]];
}
public void display(int stackNum) {
for(int i = ((stackNum - 1) * stackSize); i <= ((stackNum - 1) * stackSize + stackSize -1); i++) {
System.out.println("-------");
System.out.println("| " + arr[i] + " |");
}
System.out.println("-------");
}
}
public class ImplementTwoStackInSingleArray {
public static void main(String[] args) {
StackFromArray mStack = new StackFromArray(5);
mStack.pop(1);
mStack.push(1, 10);
System.out.println(mStack.pop(1));
mStack.pop(1);
mStack.push(1, 10);
mStack.push(1, 20);
mStack.push(1, 30);
mStack.push(1, 40);
mStack.push(1, 50);
mStack.push(1, 60);
mStack.display(1);
mStack.pop(2);
mStack.push(2, 10);
System.out.println(mStack.pop(2));
mStack.pop(2);
mStack.push(2, 110);
mStack.push(2, 220);
mStack.push(2, 330);
mStack.push(2, 440);
mStack.push(2, 550);
mStack.push(2, 660);
System.out.println(mStack.pop(2));
mStack.push(2, 660);
mStack.display(2);
}
}
class SolutionMaximumNumberFormed {
public long solution(long num) {
if (num == 0) {
return 0;
}
long temp = num;
if(num < 0) {
temp = temp * -1;
}
int[] arr = new int[10];
while (temp != 0) {
System.out.print("TEMP: " + (int) (temp % 10) + "\t BEFORE: " + arr[(int) (temp % 10)]);
int x = (int) (temp % 10);
arr[x] = arr[x] * 10 + x;
System.out.print("\t AFTER: " + arr[x]);
System.out.println();
temp = temp / 10;
}
int start = arr.length - 1;
int end = 0;
if(num < 0) {
start = 0;
end = arr.length;
}
String out = "";
while(start != end) {
if (arr[start] != 0) {
out = out + arr[start];
}
if(num < 0) {
start++;
} else {
start--;
}
}
if(num < 0) {
return Long.parseLong(out) * -1;
}
return Long.parseLong(out);
}
}
public class MaximumNumberFromedFromDigits {
public static void main(String[] args) {
SolutionMaximumNumberFormed mSol = new SolutionMaximumNumberFormed();
System.out.println(mSol.solution(87543655));
System.out.println(mSol.solution(0));
System.out.println(mSol.solution(123456789));
System.out.println(mSol.solution(999999999));
System.out.println(mSol.solution(889922443));
System.out.println(mSol.solution(-889922443));
}
}
class SolutionSumClosestToZero {
public void solution(int[] arr) {
if(arr.length <=1) {
System.out.println("NOT ENOUGH ELEMENTS IN THE GIVEN ARRAYS");
return;
}
Arrays.sort(arr);
int first = arr[0];
int secnd = arr[1];
int min = Math.abs(first) + Math.abs(secnd);
for(int i = 2; i < arr.length; i++) {
if(min > Math.abs(arr[i-1]) + Math.abs(arr[i])) {
min = Math.abs(arr[i-1]) + Math.abs(arr[i]);
first = arr[i-1];
secnd = arr[i];
}
}
System.out.println("FIRST: " + first + "\t SECOND: " + secnd);
}
}
public class GoogleSumClosestToZero {
public static void main(String[] args) {
SolutionSumClosestToZero mSol = new SolutionSumClosestToZero();
mSol.solution(new int[] {-1,-4,-9,-8,-10,4,2,5,8,9,3,0});
mSol.solution(new int[] {9,9,9});
mSol.solution(new int[] {-1,-5,-2,-9,-6,-4});
mSol.solution(new int[] {-9,-9,-9,-9,-9});
mSol.solution(new int[] {9,8,7,6,5,4,3,2,1});
}
}
class SolutionForSumOfTwoBinaryGivenInStringFormat {
public String solution(String str1, String str2) {
String out = "";
String first = str1;
String secnd = str2;
if (str2.length() > str1.length()) {
first = str2;
secnd = str1;
}
int j = secnd.length() - 1;
int carry = 0;
for (int i = first.length() - 1; i >= 0; i--) {
int temp1 = first.charAt(i) - '0';
int temp2 = 0;
if (j >= 0) {
temp2 = secnd.charAt(j--) - '0';
}
int sum = temp1 + temp2 + carry;
// System.out.println("TEMP1: " + temp1 + "\t TEMP2: " + temp2 + "\t CARRY: " + carry +
// "\t SUM: " + sum);
if (sum == 0) {
out = '0' + out;
carry = 0;
} else if (sum == 1) {
out = '1' + out;
carry = 0;
} else if (sum == 2) {
out = '0' + out;
carry = 1;
} else if (sum == 3) {
out = '1' + out;
carry = 1;
} else {
System.out.println("Invalid Entry");
return null;
}
}
if(carry != 0) {
return carry + out;
}
return out;
}
}
public class SumOfTwoBinaryGivenInStringFormat {
public static void main(String[] args) {
SolutionForSumOfTwoBinaryGivenInStringFormat mSol = new SolutionForSumOfTwoBinaryGivenInStringFormat();
System.out.println(mSol.solution("0111101", "1101"));
System.out.println(mSol.solution("", ""));
System.out.println(mSol.solution("1", ""));
System.out.println(mSol.solution("10101", ""));
System.out.println(mSol.solution("1111", "11"));
System.out.println(mSol.solution("1111", "1111"));
}
}
class SolutionForSumOfTwoConsecutiveNUmberIsTen {
public boolean sumOfTwoConsecutiveNUmberIsTen(int[] arr) {
for(int i = 1; i < arr.length; i++) {
if(arr[i-1] + arr[i] == 10) {
return true;
}
}
return false;
}
}
I think this can be solved in O(N)
class SolutionForExchangeTwoElementsForSortedArray {
public boolean solution(int[] arr) {
if(arr.length <= 1) {
System.out.println("Not enough element to swap");
return true;
}
boolean flag = true;
int first = -1;
int second = -1;
for (int i = 1; i < arr.length; i++) {
if (flag) {
if (arr[i - 1] > arr[i]) {
first = i - 1;
flag = false;
}
} else {
if (arr[first] <= arr[i]) {
second = i - 1;
}
}
if (second != -1) {
break;
}
}
if(second == -1) {
second = arr.length -1;
}
if (first == -1) {
return false;
}
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
for (int i = 1; i < arr.length - 1; i++) {
if (arr[i - 1] > arr[i]) {
return false;
}
}
return true;
}
}
public class ExchangeTwoElementsForSortedArray {
public static void main(String[] args) {
SolutionForExchangeTwoElementsForSortedArray mSol = new SolutionForExchangeTwoElementsForSortedArray();
System.out.println(mSol.solution(new int[] {4,2,2,2,5}));
System.out.println(mSol.solution(new int[] {4,2,2,2,3}));
System.out.println(mSol.solution(new int[] {3,2,2,2,3,3,4,4,4}));
System.out.println(mSol.solution(new int[] {3,2}));
System.out.println(mSol.solution(new int[] {3,2,2}));
System.out.println(mSol.solution(new int[] {3,2,4}));
System.out.println(mSol.solution(new int[] {8,2,2,4}));
System.out.println(mSol.solution(new int[] {8}));
}
}
class LinkForLinkedListSwapItemInPair {
public char data;
public LinkForLinkedListSwapItemInPair next;
public LinkForLinkedListSwapItemInPair(char data) {
this.data = data;
}
public void display() {
System.out.print(data + " ==> ");
}
}
class LinkedList {
private LinkForLinkedListSwapItemInPair first;
public LinkedList() {
first = null;
}
public boolean isEmpty() {
if(first == null) {
return true;
}
return false;
}
public void insertFirst(char data) {
LinkForLinkedListSwapItemInPair newElm = new LinkForLinkedListSwapItemInPair(data);
newElm.next = first;
first = newElm;
}
public void deleteFirst() {
if(isEmpty()) {
System.out.println("List is empty");
return;
}
LinkForLinkedListSwapItemInPair temp = first;
first = temp.next;
temp.next = null;
}
public void display() {
LinkForLinkedListSwapItemInPair current = first;
while(current != null) {
current.display();
current = current.next;
}
System.out.println("\n");
}
public LinkForLinkedListSwapItemInPair getFirst() {
return first;
}
}
class SwapItemInPair {
private LinkedList list;
public SwapItemInPair() {
list = new LinkedList();
}
public void insert(char data) {
list.insertFirst(data);
}
public void delete() {
list.deleteFirst();
}
public void display() {
list.display();
}
public boolean isEmpty() {
return list.isEmpty();
}
public void swapItemInPair() {
if(isEmpty()) {
System.out.println("List is Empty");
return;
}
if(list.getFirst().next == null) {
System.out.println("Single Item and cannot swap anymore.");
return;
}
LinkForLinkedListSwapItemInPair fItem = list.getFirst();
LinkForLinkedListSwapItemInPair sItem = list.getFirst().next;
char temp;
while(fItem.next.next != null && sItem.next.next != null) {
temp = fItem.data;
fItem.data = sItem.data;
sItem.data = temp;
fItem = fItem.next.next;
sItem = sItem.next.next;
}
temp = fItem.data;
fItem.data = sItem.data;
sItem.data = temp;
}
}
public class LinkedListSwapItemInPair {
public static void main(String[] args) {
SwapItemInPair mSwap = new SwapItemInPair();
mSwap.insert('E');
mSwap.insert('D');
mSwap.insert('C');
mSwap.insert('B');
mSwap.insert('A');
mSwap.display();
mSwap.swapItemInPair();
mSwap.display();
}
}
If you do so then it will mess up the sequence and solution will be different
- Scorpion King April 22, 2015class SolutionCharacterAppearingTheMostInAString {
public void getCharacterAppearingTheMostInAString(String str) {
int[] charMap = new int[256];
int max = 0;
for (int i = 0; i < str.length(); i++) {
charMap[str.charAt(i)]++;
if (max < charMap[str.charAt(i)]) {
max = charMap[str.charAt(i)];
}
}
for (int i = 0; i < str.length(); i++) {
if (max == charMap[str.charAt(i)]) {
System.out.println("Character: " + str.charAt(i)
+ " appears maximum number of time i.e: "
+ charMap[str.charAt(i)]);
charMap[str.charAt(i)] = -1;
}
}
}
}
class SolutionForSumOfTwoInteger {
public int sumOfTwoInteger(int a, int b) {
int negBothFlag = 1;
int max;
int sum;
if(a < 0 && b < 0) {
negBothFlag = -1;
if(a*-1 > b*-1) {
max = a*-1;
sum = b;
} else {
max = b*-1;
sum = a;
}
} else if( a < 0 && b >= 0) {
max = b;
sum = a;
} else if( a >= 0 && b < 0) {
max = a;
sum = b;
} else {
if(a > b) {
max = b;
sum = a;
} else {
max = a;
sum = b;
}
}
for(int i = 0 ; i < max; i++) {
if(negBothFlag == -1) {
sum--;
} else {
sum++;
}
}
return sum;
}
}
public class SumOfTwoInteger {
public static void main(String[] args) {
SolutionForSumOfTwoInteger mSol = new SolutionForSumOfTwoInteger();
System.out.println(mSol.sumOfTwoInteger(3, 5));
System.out.println(mSol.sumOfTwoInteger(-3, -5));
System.out.println(mSol.sumOfTwoInteger(3, -5));
System.out.println(mSol.sumOfTwoInteger(-3, 5));
System.out.println(mSol.sumOfTwoInteger(0, -5));
System.out.println(mSol.sumOfTwoInteger(-5, 0));
System.out.println(mSol.sumOfTwoInteger(0, 0));
}
}
import java.util.ArrayList;
class SolutionFindGivenNumberIsPerfectSquareOrNot {
public boolean findWhetherPerfectSquareOrNotMethod1(int num) {
if (num == 1 || num == 0) {
System.out.println("Perfect Square: " + num);
return true;
} else if (num < 0) {
System.out.println("Not Perfect Square");
return false;
}
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(0);
list.add(1);
int m = 1;
while (m * m <= num) {
m = m * 10;
}
for (int i = 2; i <= m; i++) {
list.add(i * i);
if (i * i >= num) {
break;
}
}
if (list.indexOf(num) == -1) {
System.out.println("Not Perfect Square");
return false;
}
System.out.println("Perfect Square: " + list.indexOf(num));
return true;
}
public boolean findWhetherPerfectSquareOrNotMethod2(int num) {
if (num == 1 || num == 0) {
System.out.println("Perfect Square: " + num);
return true;
} else if (num < 0) {
System.out.println("Not Perfect Square");
return false;
}
int m = 1;
int out = 0;
while (m * m <= num) {
m = m * 10;
}
for (int i = 2; i <= m; i++) {
if (i * i >= num) {
out = i;
break;
}
}
if (out == 0 || (out * out) != num) {
System.out.println("Not Perfect Square");
return false;
}
System.out.println("Perfect Square: " + out);
return true;
}
}
public class FindGivenNumberIsPerfectSquareOrNot {
public static void main(String[] args) {
SolutionFindGivenNumberIsPerfectSquareOrNot mSol = new SolutionFindGivenNumberIsPerfectSquareOrNot();
mSol.findWhetherPerfectSquareOrNotMethod1(1024);
mSol.findWhetherPerfectSquareOrNotMethod1(1025);
mSol.findWhetherPerfectSquareOrNotMethod2(1024);
mSol.findWhetherPerfectSquareOrNotMethod2(1025);
}
}
class SolutionConvertStringToInteger {
public void convert(String str) {
int negFlag = 1;
int start = 1;
int out = str.charAt(0) - '0';
if(str.charAt(0) == 45) {
negFlag = -1;
out = str.charAt(1) - '0';
start = 2;
if(out < 0 || out > 9) {
System.out.println("INVALID NUMBER");
return;
}
}
for(int i = start; i < str.length(); i++) {
int temp = str.charAt(i) - '0';
if(temp < 0 || temp > 9) {
System.out.println("INVALID NUMBER");
return;
}
out = (out * 10) + temp;
}
System.out.println(out * negFlag);
}
}
public class ConvertStringToInteger {
public static void main(String[] args) {
SolutionConvertStringToInteger mSol = new SolutionConvertStringToInteger();
mSol.convert("67889");
mSol.convert("-67889");
mSol.convert("-678-89");
mSol.convert("678-89");
}
}
class SolutionNumberOfOnesAppearsInElevenPowerN {
public int getNumberOneInElevenPowerN(int n) {
long num = (long) Math.pow(11, n);
long div = (long) Math.pow(10, n);
int count = 0;
System.out.print("(11 ^ " + n + "): " + num + " \t NUMBER OF 1 : " );
while(num != 0) {
if(num/div == 1) {
count++;
}
num = num % div;
div = div/10;
}
System.out.print(count);
System.out.println();;
return count;
}
}
public class NumberOfOnesAppearsInElevenPowerN {
public static void main(String[] args) {
SolutionNumberOfOnesAppearsInElevenPowerN sol = new SolutionNumberOfOnesAppearsInElevenPowerN();
sol.getNumberOneInElevenPowerN(0);
sol.getNumberOneInElevenPowerN(1);
sol.getNumberOneInElevenPowerN(2);
sol.getNumberOneInElevenPowerN(3);
sol.getNumberOneInElevenPowerN(4);
sol.getNumberOneInElevenPowerN(5);
sol.getNumberOneInElevenPowerN(6);
sol.getNumberOneInElevenPowerN(7);
sol.getNumberOneInElevenPowerN(8);
sol.getNumberOneInElevenPowerN(9);
sol.getNumberOneInElevenPowerN(10);
sol.getNumberOneInElevenPowerN(11);
sol.getNumberOneInElevenPowerN(12);
}
}
import java.util.ArrayList;
class SolutionForFindFirstNonRepeatingCharInSinglePass {
public String firstNonRepeatingChar(String str) {
if(str.length() == 0) {
return "";
}
ArrayList<Character> temp = new ArrayList<Character>();
boolean[] charmap = new boolean[256];
for (int i = 0; i < str.length(); i++) {
int val = str.charAt(i);
if (!charmap[val]) {
temp.add(str.charAt(i));
charmap[val] = true;
} else {
if (temp.size() != 0) {
temp.remove((Character) str.charAt(i));
}
}
}
if (temp.size() == 0) {
return "";
}
return temp.get(0) + "";
}
}
public class FindFirstNonRepeatingCharInSinglePass {
public static void main(String[] args) {
SolutionForFindFirstNonRepeatingCharInSinglePass sol = new SolutionForFindFirstNonRepeatingCharInSinglePass();
System.out.println("1.INPUT: aaaaaaaaaaa ANS: " + sol.firstNonRepeatingChar("aaaaaaaaaaa"));
System.out.println("2.INPUT: \"\" ANS: " + sol.firstNonRepeatingChar(""));
System.out.println("3.INPUT: aabbccd ANS: " + sol.firstNonRepeatingChar("aabbccd"));
System.out.println("4.INPUT: aabcbcba ANS: " + sol.firstNonRepeatingChar("aabcbcba"));
System.out.println("5.INPUT: aabcbcbe ANS: " + sol.firstNonRepeatingChar("aabcbcbe"));
System.out.println("6.INPUT: abcde ANS: " + sol.firstNonRepeatingChar("abcde"));
System.out.println("7.INPUT: abcbcbae ANS: " + sol.firstNonRepeatingChar("abcbcbae"));
System.out.println("8.INPUT: abcbedcbae ANS: " + sol.firstNonRepeatingChar("abcbedcbae"));
System.out.println("9.INPUT: abcbedcbfae ANS: " + sol.firstNonRepeatingChar("abcbedcbfae"));
}
}
small mistake
private static void getLongestRunningPositiveSequenceInAnArray(int[] arr) {
int count = 0;
int max = 0;
int indx = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= 0) {
count++;
} else {
if (count > max) {
max = count;
indx = i - count;
}
count = 0;
}
}
if(count > max) {
max = count;
indx = arr.length - count;
}
System.out.println("Longest Running Positive Sequence is: " + max + " Starting at index: "
+ indx);
}
public class LongestRunningPositiveSequenceInAnArray {
public static void main(String[] args) {
getLongestRunningPositiveSequenceInAnArray(new int[] {
1, 2, -3, 2, 3, 4, -6, 1, 2, 3, 4, 5, -8, 5, 6
});
getLongestRunningPositiveSequenceInAnArray(new int[] {
-1, -2, -3, -2, -3, -4, -6, -1, -2, -3, -4, -5, -8, -5, -6
});
getLongestRunningPositiveSequenceInAnArray(new int[] {
1, 2, 3, 2, 3, -4, -6, 1, 2, 3, 4, 5, -8, 5, 6
});
getLongestRunningPositiveSequenceInAnArray(new int[] {
1, 2, 3, 2, 3, 4, 6, 1, 2, 3, 4, 5, 8, 5, 6
});
getLongestRunningPositiveSequenceInAnArray(new int[] {
1, 2, 3, -2, 3, 4, 6, 1, 2, 3, 4, 5, 8, 5, 6
});
}
private static void getLongestRunningPositiveSequenceInAnArray(int[] arr) {
int count = 0;
int max = 0;
int indx = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= 0) {
count++;
} else {
if (count > max) {
max = count;
indx = i - count;
}
count = 0;
}
}
if(count > max) {
max = count;
indx = arr.length - count;
}
System.out.println("Longest Running Positive Sequence is: " + max + " Starting at index: "
+ indx);
}
}
class MergeArray {
private void heapSort(int[] arr) {
...
}
public int[] mergeIntoSortedArray(int[] arr1, int[] arr2) {
sorting(arr1);
sorting(arr2);
int[] out = new int[arr1.length + arr2.length];
int j = 0;
int k = 0;
int i = 0;
while (j < arr1.length && k < arr2.length) {
if (arr1[j] < arr2[k]) {
out[i++] = arr1[j++];
} else {
out[i++] = arr2[k++];
}
}
while (j < arr1.length) {
out[i++] = arr1[j++];
}
while (k < arr2.length) {
out[i++] = arr2[k++];
}
return out;
}
}
public class MergeTwoArrayIntoSortedArray {
public static void main(String[] args) {
int[] arr1 = new int[] {2,5,8,9};
int[] arr2 = new int[] {6,3,4,7,1};
MergeArray mMergeArray = new MergeArray();
System.out.println("MERGED SORTED ARRAY: "
+ Arrays.toString(mMergeArray.mergeIntoSortedArray(arr1, arr2)));
}
}
- Scorpion King April 04, 2015class Pattern {
private void generatePattern(int rowCounter, int row) {
if (rowCounter > row) {
return;
}
int[] arr = new int[rowCounter + 1];
for (int i = 0; i < rowCounter; i++) {
arr[i] = getFactorial(rowCounter - 1)
/ (getFactorial(i) * getFactorial(rowCounter - i - 1));
}
display(arr, rowCounter);
rowCounter++;
generatePattern(rowCounter, row);
}
private void display(int[] arr, int rowCounter) {
for (int i = 0; i < rowCounter; i++) {
System.out.print(arr[i] + "\t");
}
System.out.println();
}
private int getFactorial(int num) {
if (num <= 1) {
return 1;
}
return num * getFactorial(num - 1);
}
public void getPattern(int row) {
generatePattern(0, row);
}
}
public class DisplayTrainglePattern {
public static void main(String[] args) {
Pattern mPattern = new Pattern();
mPattern.getPattern(7);
mPattern.getPattern(9);
}
}
public void convertHorizontalStringToVertical(String[] strArr) {
int max = 0;
for (int i = 1; i < strArr.length; i++) {
int temp = strArr[i].split(" ").length;
if (max < temp) {
max = temp;
}
}
String[][] output = new String[max][strArr.length];
for (int i = 0; i < strArr.length; i++) {
String[] temp = strArr[i].split(" ");
for (int j = 0; j < max; j++) {
if (j < temp.length) {
output[j][i] = temp[j];
} else {
output[j][i] = " ";
}
}
}
for (int i = 0; i < output.length; i++) {
System.out.println(Arrays.toString(output[i]));
}
}
Output:
[epic, interviewing, print]
[is, for, this]
[a, software, vertically]
[healthcare, developer, sentence]
[company., , by]
[ , , sentence.]
public String reverseEvenStringUppercaseOddString(String str) {
int count = 0;
int wordCount = 0;
String revString = "";
String output = "";
for(int i = 0; i < str.length(); i++) {
if(str.charAt(i) != ' ') {
revString = str.charAt(i) + revString;
count++;
} else {
if(count > 0) {
wordCount++;
if(wordCount % 2 == 0) {
output = output + revString;
} else {
output = output + str.substring(i - count, i).toUpperCase();
}
}
revString = "";
count = 0;
output = output + str.charAt(i);
}
}
if(count > 0) {
wordCount++;
if(wordCount % 2 == 0) {
output = output + revString;
} else {
output = output + str.substring(str.length() - count, str.length()).toUpperCase();
}
}
return output;
}
- Scorpion King April 03, 2015public int getLastIndex(int[] arr) {
int lastIndex = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i - 1] != arr[i]) {
lastIndex++;
arr[lastIndex] = arr[i];
}
}
System.out.println("ARRAY: " + Arrays.toString(arr));
return lastIndex;
}
public class ElementPairingFromSingleArray {
public static void main(String[] args) {
int[] arr = new int[] {1,2,3,4,5,11,22,33,44,55} ;
System.out.println(Arrays.toString(arr));
int size = arr.length/2;
int k = 1;
for(int i = 1; i < arr.length -1; i = i + 2) {
while(size != i) {
arr[size] = arr[size] + arr[size - 1];
arr[size - 1] = arr[size] - arr[size -1];
arr[size] = arr[size] - arr[size - 1];
size--;
System.out.println(Arrays.toString(arr));
}
size = arr.length/ 2 + k;
k++;
System.out.println("-------------------------------------");
}
}
}
O/P
[1, 2, 3, 4, 11, 5, 22, 33, 44, 55]
[1, 2, 3, 11, 4, 5, 22, 33, 44, 55]
[1, 2, 11, 3, 4, 5, 22, 33, 44, 55]
[1, 11, 2, 3, 4, 5, 22, 33, 44, 55]
-------------------------------------
[1, 11, 2, 3, 4, 22, 5, 33, 44, 55]
[1, 11, 2, 3, 22, 4, 5, 33, 44, 55]
[1, 11, 2, 22, 3, 4, 5, 33, 44, 55]
-------------------------------------
[1, 11, 2, 22, 3, 4, 33, 5, 44, 55]
[1, 11, 2, 22, 3, 33, 4, 5, 44, 55]
-------------------------------------
[1, 11, 2, 22, 3, 33, 4, 44, 5, 55]
-------------------------------------
Repsheilaahollins, Blockchain Developer at ASAPInfosystemsPvtLtd
Hello, I am working as a mineralogist studying rocks, gems, and other minerals, including their chemical and crystalline structures. I ...
Repangelafiecke, Blockchain Developer at ASU
I am a Data processor at DGS VolMAX. I will also be a controller for the data I use for ...
Replarrymmapp, Android test engineer at Software AG
Hello, I am name and I live in a city, USA. I am a professional's Podiatric doctor. I am ...
Repamayahoyle, Associate at ABC TECH SUPPORT
Working as a Set Designer with 2+ years of experience in Audio Aid for vehicles and props including turns using ...
Repfloydmsnyder, Theoretical mathematician at STM Auto Parts
As a Theoretical Mathematician,I’m little more interested in the theory involved with mathematics. In my free time , I ...
Reppathfisher, Animator at Rack N Sack
I am Pat working in Rack N Sack where I use sequential images to produce the illusion of movement and ...
Reprubyperezj, Backend Developer at A9
My name is Ruby Perez and I live in Lyndonville and it would be my honor and privilege to be ...
Solved this and seems to be working:
Checked with the above input:
- Scorpion King April 19, 2016output is :
C -> D = 8
C -> B = 5
A -> B = 3
Checked with this input:
A B 8
B C 4
C A 4
Output:
A -> B = 4