Suresh Patel
BAN USERpackage com.concurrency.pkg;
import java.util.Arrays;
public class MaxFrequency {
private static int[] a = { 2, 30, 15, 10, 8, 25, 80 };
public static void main(String[] args) {
Arrays.sort(a);
System.out.println((a[a.length - 1] - a[0]) + (a[a.length - 2] - a[1]));
}
}
package com.concurrency.pkg;
public class RowLabel {
private static int ASCII_CONST = 64;
private static StringBuilder sb = new StringBuilder();
private static int getRowLabel(int number) {
if (number == 0) {
System.out.println(sb.reverse());
return 0;
}
int reminder = number % 26;
if(reminder == 0)
{
reminder = 26;
number = number -1;
}
int lastChar = ASCII_CONST + reminder;
String s = (new Character((char) lastChar)).toString();
sb.append(s);
return getRowLabel(number / 26);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
getRowLabel(4076);
}
}
package com.concurrency.pkg;
public class SubstrCount {
private static int cntSubStr(char[] c1, char[] c2) {
int cnt = 0;
int k = 0;
int n = 0;
for (int j = 0; j < c1.length && k < c2.length;) {
n++;
if (c2[k] == c1[j]) {
j++;
k++;
if (k == c2.length) {
cnt += 1;
k = 0;
}
continue;
}
k = 0;
j = n;
}
return cnt;
}
public static void main(String[] args) {
char[] s1 = "abghefghcde".toCharArray();
char[] s2 = "gh".toCharArray();
System.out.println(cntSubStr(s1, s2));
}
}
import java.util.Random;
public class ArrangeArrayElements {
private static int[] a = new int[18];
private static void rearrangeElements(int grpSize) {
if (grpSize == 1)
return;
int j = 0;
for (; j < a.length; j = j + grpSize) {
int k = (j + grpSize);
for (int n = j; n < (j + (grpSize / 2)) && k < a.length; n++) {
int temp = a[n];
a[n] = a[--k];
a[k] = temp;
}
}
if (j > a.length) {
int remainingElements = a.length - (j - grpSize);
int arrLen = a.length;
for (int n = arrLen - remainingElements; n < (n + remainingElements / 2) && n < (a.length - 1); n++) {
int temp = a[n];
a[n] = a[--arrLen];
a[arrLen] = temp;
}
}
}
public static void main(String[] args) {
Random rnd = new Random();
for (int i = 0; i < 18; i++) {
a[i] = rnd.nextInt(100);
;
System.out.print(a[i] + ",");
}
rearrangeElements(3);
System.out.println();
for (int i = 0; i < 18; i++)
System.out.print(a[i] + ",");
}
}
public class PalindromeMinString {
private static boolean isPalindrome(char[] s1, int newCharIndex) {
for (int i = 0; i < newCharIndex; i++) {
if (i == newCharIndex) {
break;
}
if (s1[i] == s1[newCharIndex--]) {
continue;
}
return false;
}
return true;
}
private static char[] shiftChar(char[] c1, char[] c2, int idx) {
int len = c1.length;
for (int i = idx; i >= 0; i--) {
c2[len++] = c1[i];
}
return c2;
}
private static int addCharsToStr(String s) {
char[] c1 = s.toCharArray();
char[] c2 = new char[c1.length * 2];
int len = c1.length;
for (int j = 0; j < s.length(); j++) {
c2[j] = c1[j];
}
if (isPalindrome(s.toCharArray(), s.length() - 1)) {
return -1;
}
int i = 0;
while (i < len) {
char[] c = shiftChar(c1, c2, i);
if (isPalindrome(c, len + (i))) {
return (len + i);
}
i++;
}
return -1;
}
public static void main(String[] args) {
String str = "test";
int i = addCharsToStr(str);
for (int j = i - str.length(); j >= 0; j--) {
System.out.print(str.toCharArray()[j]);
}
}
}
package com.concurrency.pkg;
import java.util.LinkedHashMap;
import java.util.Map;
public class FirstElementWithEvenOccurences {
public static void main(String[] args) {
int[] array1 = { 8, 9, 11, 2, 11, 11, 16, 9, 16 };
System.out.println(getFirstElementWithEvenOccurences(array1)); // Prints
// 9
}
private static Integer getFirstElementWithEvenOccurences(int[] array) {
Map<Integer, Integer> allOcurrence = new LinkedHashMap<>();
for (int i = 0; i < array.length; i++) {
Integer keyValue = new Integer(array[i]);
Integer value = allOcurrence.get(keyValue);
value = null != value ? value + keyValue : keyValue;
allOcurrence.put(keyValue, value);
}
Integer parityElement = null;
for (Integer key : allOcurrence.keySet()) {
int div = (allOcurrence.get(key) / key);
if ((div & 1) != 1) {
parityElement = key;
break;
}
}
return parityElement;
}
}
In place soluton in Java
package com.concurrency.pkg;
import java.util.Arrays;
public class FirstMaxFirstMin {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[] = {1,2,3,4,5,6,7,8,9,10,11};
Arrays.sort(a);
int j = a.length-1;
int i =0;
for(; i<j ;i+=2 )
{
int start = a[i];
int last = a[a.length-1];
int next = a[i+1];
if((i+1) == j){
a[i]= a[j];
a[j]= last;
a[a.length-1] = start;
swapLastTwo(a);
break;
}
a[i]= a[j];
a[i+1]= (i > 0) ? last : start;
a[a.length - 1] = (i > 0) ? start : next;
a[j] = next;
if( a[a.length-2] < a[a.length-1]){
swapLastTwo(a);
}
j--;
}
if((a.length - i) > 2 && (a.length%2 != 0) ){
int temp = a[a.length - 2];
a[a.length - 2] = a[a.length - 1];
a[a.length - 1] = temp;
}
for (i =0 ; i< a.length ; i++)
System.out.print(a[i]+" , ");
}
private static void swapLastTwo(int a[]){
int temp = a[a.length-1];
a[a.length-1] = a[a.length-2];
a[a.length-2] = temp;
}
private static int swap(int i , int j, int[] a){
int next = a[i+1];
int last = a[a.length-1];
int curr = a[i];
a[i+1] =
a[i]= a[--j];
a[i+1]= a[i];
if( i == 0)
a[j]= next;
return j;
}
}
- Suresh Patel January 03, 2016