Comments on: Rearrange array into alternate positive and negative numbers https://demo.ritambhara.in/rearrange-array-into-alternate-positive-and-negative-numbers/ Coding / System Design Interviews Sat, 12 Sep 2020 06:47:27 +0000 hourly 1 https://wordpress.org/?v=6.7.2 By: Sagar https://demo.ritambhara.in/rearrange-array-into-alternate-positive-and-negative-numbers/#comment-4581 Sat, 12 Sep 2020 06:47:27 +0000 http://www.ritambhara.in/?p=2766#comment-4581 Take an integer array or list with some negative numbers and some positive numbers. Now write logic to arrange the numbers in Propetic order. Propetic order is arrange positive elements and negative elements in ascending order in alternative manner. first element should be +ve number next element should be -ve number and so on in ascending order . eg input x = [10,11,-10,-11,2,-90] output should be [2,-90,10,-11,11,-10] . eg input x = [10,5,-3,4,7,8], then output should be [4,-3,5,7,8,10] *

]]>
By: Rehan Sahil https://demo.ritambhara.in/rearrange-array-into-alternate-positive-and-negative-numbers/#comment-1960 Sat, 22 Mar 2014 07:54:21 +0000 http://www.ritambhara.in/?p=2766#comment-1960 error_reporting(0);//for hiding if offset not exist notice error
$exist_array=array(1, 2, -2, -5, 6, 7, -8);
//Output: {1, -2, 2, -5, 6, -8, 7}
$positive=array();
$negative=array();
foreach($exist_array as $i=>$k)
{
if($k<0)
{
array_push($negative, $k);
}
else
{
array_push($positive, $k);
}
}
$lenth=(count($positive)>count($negative)?count($positive):count($negative));
for($i=0;$i<$lenth;$i++)
{
echo $positive[$i];
echo " ";
echo $negative[$i];
echo " ";
}

]]>
By: Rehan Sahil https://demo.ritambhara.in/rearrange-array-into-alternate-positive-and-negative-numbers/#comment-1961 Sat, 22 Mar 2014 07:54:21 +0000 http://www.ritambhara.in/?p=2766#comment-1961 error_reporting(0);//for hiding if offset not exist notice error
$exist_array=array(1, 2, -2, -5, 6, 7, -8);
//Output: {1, -2, 2, -5, 6, -8, 7}
$positive=array();
$negative=array();
foreach($exist_array as $i=>$k)
{
if($k<0)
{
array_push($negative, $k);
}
else
{
array_push($positive, $k);
}
}
$lenth=(count($positive)>count($negative)?count($positive):count($negative));
for($i=0;$i<$lenth;$i++)
{
echo $positive[$i];
echo " ";
echo $negative[$i];
echo " ";
}

]]>