Magemello
BAN USERpublic static void main(String[] args) {
int array[][] = { { 20, 40, 80 }, { 5, 60, 90 }, { 45, 50, 55 } };
int sortIndex[] = new int[array.length];
for (int i = 0; i < array.length; i++) {
sortIndex[i] = 0;
}
for (int i = 0; i < array.length * array[0].length; i++) {
int min = 0;
for (int j = 0; j < sortIndex.length; j++) {
if (sortIndex[j] != -1) {
min = array[j][sortIndex[j]];
break;
}
}
int indexMin = 0;
for (int j = 1; j < sortIndex.length; j++) {
if (sortIndex[j] != -1 && array[j][sortIndex[j]] < min) {
min = array[j][sortIndex[j]];
indexMin = j;
}
}
System.out.print(min + " ");
sortIndex[indexMin] = sortIndex[indexMin] + 1;
if (sortIndex[indexMin] >= array[0].length) {
sortIndex[indexMin] = -1;
}
}
}
O(n)
private static void mySolution(String palindrome){
char word[] = palindrome.toCharArray();
boolean flag = false;
for (int i = 0, j = word.length - 1; j > word.length/2; i++) {
if(word[i] == word[j]){
j--;
}else{
if(flag){
System.out.println("NO");
return;
}
if(word[i] == word[j-1]){
j--;
}
flag = true;
}
}
System.out.println("YES");
}
public class LookAndSaySequence {
public static void main(String args[]){
System.out.println(patternUtil(4));
}
public static String patternUtil(int input)
{
String result = "1";
while(input> 0){
input--;
char currentCharacter = result.charAt(0);
String newResult = "";
int countRipetitionChar = 0;
for(int i = 0; i< result.length(); i++) {
char nextCharacter = result.charAt(i);
if(nextCharacter != currentCharacter) {
newResult = newResult + countRipetitionChar + currentCharacter;
countRipetitionChar = 0;
currentCharacter = nextCharacter;
}
countRipetitionChar ++;
}
result = newResult + countRipetitionChar + currentCharacter;
}
return result;
}
}
public class findABEqualCDPoints {
public static void main(String args[]) {
int a[] = { 3, 4, 7, 1, 2, 9, 8 };
printSumPairsABCD(a);
}
public static void printSumPairsABCD(int[] a) {
HashMap<Integer, List<Index>> map = new HashMap<Integer, List<Index>>();
List<Index> list = new ArrayList<>();
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
Integer key = a[i] + a[j];
list = map.get(key);
if (list == null) {
list = new ArrayList<Index>();
list.add(new Index(i, j));
} else {
for (Index index : list) {
System.out.println(a[index.indexA] + " " + a[index.indexB] + " -- " + a[j] + " " + a[i]);
if (index.indexA != i && index.indexB != j && index.indexB != i && index.indexA != j) {
System.out.println(index.indexA + " " +
index.indexB + " -- " + j + " " + i);
}
}
list.add(new Index(i, j));
}
map.put(key, list);
}
}
}
static class Index {
int indexA;
int indexB;
Index(int indexA, int indexB) {
this.indexA = indexA;
this.indexB = indexB;
}
}
}
public class MaxDifference {
public static void main(String args[]) {
int array[] = { 1, 2, 1, 4, 5, 6, 7 };
// int array[] = { 3, 2, 1, 4, 5, 6, 7 };
int maxDiff = Integer.MIN_VALUE;
int mineleMent = array[0];
for (int i = 1; i < array.length; i++) {
int diff = array[i] - mineleMent;
if (diff > maxDiff) {
maxDiff = diff;
}
if (array[i] < mineleMent) {
mineleMent = array[i];
}
}
System.out.println(maxDiff);
}
/*
* Time Complexity: O(n) Auxiliary Space: O(1)
*/
}
public class ReverseSentence {
public static void main(String[] args) {
char input[] = "I wish you a merry Christmas".toCharArray();
recursive(input, input.length, input.length);
}
private static int recursive(char input[], int index, int indexMax) {
--index;
if (index < 0) {
return -1;
}
int minIndex = -1;
if (input[index] == ' ') {
return index;
}
minIndex = recursive(input, index, indexMax);
System.out.print(input[index]);
if (index + 1 == indexMax) {
System.out.print(' ');
recursive(input, minIndex, minIndex);
}
return minIndex;
}
}
public class ReverseSentence {
public static void main(String[] args) {
char input[] = "I wish you a merry Christmas".toCharArray();
recursive(input, input.length, input.length);
}
private static int recursive(char input[], int index, int indexMax) {
--index;
if (index < 0) {
return -1;
}
int minIndex = -1;
if (input[index] == ' ') {
return index;
}
minIndex = recursive(input, index, indexMax);
System.out.print(input[index]);
if (index + 1 == indexMax) {
System.out.print(' ');
recursive(input, minIndex, minIndex);
}
return minIndex;
}
}
public class ReverseSentence {
public static void main(String[] args) {
char input[] = "I wish you a merry Christmas".toCharArray();
recursive(input, input.length, input.length);
}
private static int recursive(char input[], int index, int indexMax) {
--index;
if (index < 0) {
return -1;
}
int minIndex = -1;
if (input[index] == ' ') {
return index;
}
minIndex = recursive(input, index, indexMax);
System.out.print(input[index]);
if (index + 1 == indexMax) {
System.out.print(' ');
recursive(input, minIndex, minIndex);
}
return minIndex;
}
}
- Magemello March 15, 2015