vbala1981
BAN USERWorking java code. in O(n)
static void ArrangeOddAndEven(int[] arr)
{
int OI=0;
int EI=1;
while(OI <= arr.length-2 && EI<= arr.length-1)
{
if( arr[OI] % 2 == 1 )
{
OI = OI+2;
}
else
{
if( arr[EI] % 2 == 1)
{
int temp = arr[OI];
arr[OI]=arr[EI];
arr[EI]=temp;
}
else
{
EI = EI+2;
}
}
}
}
Here is the working code in java. By updating the constant value "TOTAL_THREADS", we can instantiate 'n' number of threads that co-ordinates with each other to print the numbers in order 1,2,3,4,...n,1,2,3,4,...n........
import java.util.concurrent.locks.*;
class PrintTest
{
private Lock _Lock;
private int _ThreadReadyCount;
private final int TOTAL_THREADS = 5;
private Condition _Ready;
private Condition _Condition[];
synchronized public void IncreaseReadyCount()
{
_ThreadReadyCount = _ThreadReadyCount + 1;
if( _ThreadReadyCount == TOTAL_THREADS ) _Ready.signalAll();
}
synchronized public int GetThreadReadyCount()
{
return _ThreadReadyCount;
}
public PrintTest()
{
_Lock = new ReentrantLock();
_ThreadReadyCount = 0;
_Ready = _Lock.newCondition();
_Condition = new Condition[TOTAL_THREADS];
for(int i=1; i<=TOTAL_THREADS; i++)
{
_Condition[i-1] = _Lock.newCondition();
}
}
public void StartPrinting(int iStartThreadIndex) throws InterruptedException
{
_Lock.lock();
try
{
RunnableDemo Demos[] = new RunnableDemo[TOTAL_THREADS];
for(int i=1; i<=TOTAL_THREADS; i++)
{
Demos[i-1] = new RunnableDemo(i, this);
Demos[i-1].start();
}
_Ready.await(); //when all the threads are ready
System.out.println("All the three threads are ready......");
//_Condition[0].signalAll();
int Index = (int) (Math.random() * TOTAL_THREADS);
_Condition[iStartThreadIndex].signalAll();
}
finally
{
_Lock.unlock();
}
}
public void PrintRandomNumbers(int iMaxValue, int iTotalNumbers)
{
System.out.println("Randome numbers.....");
for(int i=1; i<=iTotalNumbers; i++)
System.out.println(i + ") " + (int)(Math.random() * iMaxValue));
}
public void PrintThread(int InputValue) throws InterruptedException
{
_Lock.lock();
try
{
System.out.println("Print thread is invoked from Thread-" + InputValue);
IncreaseReadyCount();
while(true)
{
_Condition[InputValue-1].await();
System.out.println(InputValue);
if( InputValue < TOTAL_THREADS )
_Condition[InputValue].signalAll();
else
_Condition[0].signalAll();
}
}
finally
{
_Lock.unlock();
}
}
}
class RunnableDemo implements Runnable
{
private PrintTest _printTest;
private Thread t;
private int _inputValue;
RunnableDemo(int iValue, PrintTest pTest)
{
_inputValue = iValue;
_printTest = pTest;
}
public void run()
{
try
{
_printTest.PrintThread(_inputValue);
}
catch(InterruptedException e)
{
}
}
public void start ()
{
if (t == null)
{
t = new Thread (this);
t.start ();
}
}
}
public class Test_SignalAll
{
public static void main(String args[])
{
String option = args[0];
if( option.equals("-h") || option.equals("-H") || option.equals("-HELP") )
{
System.out.println("The program can be invoked with following arguments...");
System.out.println("RANDOM <MAX VALUE> <TOTAL NUMBERS>");
System.out.println("PRINT <MAX VALUE>");
}
PrintTest oPrintTest = new PrintTest();
if( option.equals("RANDOM") )
{
if( args.length == 3 )
{
try
{
int iMaxValue = Integer.parseInt(args[1]);
int iTotal = Integer.parseInt(args[2]);
oPrintTest.PrintRandomNumbers(iMaxValue, iTotal);
}
catch(NumberFormatException e)
{
System.out.println("Invalid type of arguments....Please try as follows....");
System.out.println("RANDOM <MAX VALUE as integer> <TOTAL NUMBERS as integer>");
}
}
else
{
System.out.println("Invalid number of arguments....Please try as follows....");
System.out.println("RANDOM <MAX VALUE> <TOTAL NUMBERS>");
}
}
try
{
if( option.equals("PRINT") )
{
if( args.length == 2 )
{
try
{
int iMaxValue = Integer.parseInt(args[1]);
oPrintTest.StartPrinting(iMaxValue);
}
catch(NumberFormatException e)
{
System.out.println("Invalid type of arguments....Please try as follows....");
System.out.println("PRINT <MAX Number of Threads as integer>");
}
}
else
{
System.out.println("Invalid number of arguments....Please try as follows....");
System.out.println("PRINT <MAX VALUE>");
}
}
}
catch(InterruptedException e)
{
}
}
}
Here is the working java code.
public class GetAllStrings
{
public static void main(String agrs[])
{
GetAllStrings("careercup");
}
private static void GetAllStrings(String iInputString)
{
for(int i=1; i<=(iInputString.length()-2); i++)
{
for(int j=1; j+i<=(iInputString.length()-1); j++)
{
System.out.print( iInputString.substring(0, j) );
System.out.print( i );
System.out.print( iInputString.substring(j+i, iInputString.length() ) );
System.out.print( "\n" );
}
}
}
}
I believe the total number of possible combinations would be 1+2+.....+(n-2) where n is the length of the string.
Sample output of above program is like
==============================
c1reercup
ca1eercup
car1ercup
care1rcup
caree1cup
career1up
careerc1p
c2eercup
ca2ercup
car2rcup
care2cup
caree2up
career2p
c3ercup
ca3rcup
car3cup
care3up
caree3p
c4rcup
ca4cup
car4up
care4p
c5cup
ca5up
car5p
c6up
ca6p
c7p
Repdianacloweryd, Developer Program Engineer at Accolite software
I am Diana from Reston USA . I work as an Agricultural and food science technician in Jumbo Sports. I help ...
Repwaynebgrover, AT&T Customer service email at ASAPInfosystemsPvtLtd
I am 31 years old and live in San Jose with my family. I have all types of books and ...
Working version of java code with time complexity as O(n) and space complexity as O(1)
Actual Array
- vbala1981 June 11, 20151)1
2)2
3)3
4)4
5)5
After Rotation
1)3
2)4
3)5
4)1
5)2