vishal naik
BAN USERpackage com.javafries.thread;
public class EvenOddNumberPrinter {
private static class NumberPrinter {
// To check if even number is printed or not.
private boolean isEvenPrinted = true;
public void printOdd(int number) throws InterruptedException {
// Get a lock on NumberPrinter
synchronized (this) {
// Wait until even is not printed.
if (!isEvenPrinted)
wait();
System.out.println(number);
isEvenPrinted = false;
// Notify the other waiting thread which is waiting on
// NumberPrinter
// Other thread will get out of waiting state
notify();
}
}
public void printEven(int number) throws InterruptedException {
synchronized (this) {
if (isEvenPrinted)
wait();
System.out.println(number);
isEvenPrinted = true;
notify();
}
}
}
private static class OddNumberGenerator implements Runnable {
private NumberPrinter q;
private int max;
public OddNumberGenerator(NumberPrinter q, int max) {
this.q = q;
this.max = max;
}
@Override
public void run() {
for (int i = 1; i < max; i = i + 2) {
try {
q.printOdd(i);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
private static class EvenNumberGenerator implements Runnable {
private NumberPrinter printer;
private int max;
public EvenNumberGenerator(NumberPrinter printer, int max) {
this.printer = printer;
this.max = max;
}
@Override
public void run() {
for (int i = 2; i <= max; i = i + 2) {
try {
printer.printEven(i);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
public static void main(String[] args) {
int maxNumber = 10;
NumberPrinter printer = new NumberPrinter();
new Thread(new EvenNumberGenerator(printer, maxNumber)).start();
new Thread(new OddNumberGenerator(printer, maxNumber)).start();
}
}
package com.javafries.number;
/**
* @author vishal.naik
*/
public class MaximumNumberProblem {
private static int findMaxNumberFromDigits(int num) {
int[] digits = getDigitsInSortedOrder(num);
int maxNum = 0;
for (int index = digits.length - 1; index >= 0; index--) {
int frequency = digits[index];
while (frequency > 0) {
maxNum = maxNum * 10 + index;
frequency--;
}
}
return maxNum;
}
private static int[] getDigitsInSortedOrder(int num) {
int[] digits = new int[10];
while (num > 0) {
// Get remainder
int index = num % 10;
digits[index]++;
num /= 10;
}
return digits;
}
public static void main(String[] args) {
int num = 38293367;
int maxNum = findMaxNumberFromDigits(num);
System.out.println("Maximum number : " + maxNum);
}
}
I have implemented simple soultion as mentioned by - tejaswi.yvs
import java.util.Arrays;
/**
* @author vishal.naik
*
*/
public class WaveSort {
public static void main(String[] args) {
int[] arr = { 7, 3, 6, 9, 2, 5, 8, 4 };
waveSort(arr);
printArray(arr);
}
/**
* Sorts the input array such that a1 >= a2 <= a3 >= a4 <= a5...
*
* @param arr , not null
*/
private static void waveSort(int[] arr) {
// Sort the array. This sorts array in ascending order.
Arrays.sort(arr);
//Reverse the array. So the array will be in descending order.
reverse(arr);
// Iterate over the sorted array.
for (int i = 1; i < arr.length - 1; i = i + 2) {
// Swap the numbers.
swap(i, i + 1, arr);
}
}
/**
* Prints the array.
*
* @param arr , not null
*/
private static void printArray(final int[] arr) {
for (int i : arr) {
System.out.print(i + " ");
}
}
/**
* Reverses the array.
*
* @param arr , not null
*/
private static void reverse(int[] arr) {
int start = 0;
int end = arr.length -1;
while(start <= end) {
swap(start, end, arr);
start++;
end--;
}
}
/**
* Swaps the element at position i with element at position j.
* @param i , not null
* @param j , not null
* @param arr , not null
*/
private static void swap(final int i, final int j, int[] arr) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
Please visit java-fries site for the complete article.
- vishal naik May 09, 2014
- vishal naik June 14, 2016