GeeksforGeeks » Algorithms
Amazon Banglore Online Written
(6 posts)-
In given array of elements like [a1,a2,a3,..an,b1,b2,b3,..bn,c1,c2,c3,...cn] Write a program to merge them like [a1,b1,c1,a2,b2,c2,...an,bn,cn].
PS: Doing without using extra memory would fetch more points
Sample Testcases:
Input #00:
{1,2,3,4,5,6,7,8,9,10,11,12}
Output #00:
{1,5,9,2,6,10,3,7,11,4,8,12}
Explanation:
Here as you can notice, the array is of the form
{a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4} -
I have just give it a try using less memory. But program runs in O(n^2) time. Correct me if i am wrong?
#include "stdafx.h" #include <iostream> using namespace std; int GetDestinationPosition( int current_pos, int num_arr, int num_ele_in_arr) { /* By giving current position, it will give where that position to be saved */ int which_arr = current_pos/num_ele_in_arr + 1; int array_pos = current_pos%num_ele_in_arr; if( array_pos == 0) { which_arr--; array_pos = num_ele_in_arr; } return which_arr + ( array_pos - 1 ) * num_arr; } bool IsPositionAlreadySet(int current_position, int num_arr, int num_ele_in_arr) { int j = 1; for(int i=1; i < current_position; ) { j = GetDestinationPosition(j, num_arr, num_ele_in_arr); if( j == current_position ) /* Current position is already set*/ return true; if( j == i ) { i++; j = i; } } return false; /*Current position is not set yet*/ } #define TOTAL_ELEMENTS 17 int _tmain(int argc, _TCHAR* argv[]) { /* first element is not used as to start the index from 1 */ int num[TOTAL_ELEMENTS]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; int num_arr = 3; /*Number of arrays*/ int num_ele_in_arr = 5; /*Number of elements in each array*/ int num_ele_used = num_arr*num_ele_in_arr; int j = 1; for(int i =1; i <= num_ele_used; ) { j = GetDestinationPosition(j, num_arr, num_ele_in_arr); if(j == i) { while(true) { i++; /* check whether position i is already set earlier, if set check i+1 */ if( ! IsPositionAlreadySet(i, num_arr, num_ele_in_arr) || i > num_ele_used) { break; } } j = i; }else{ int temp = num[j]; num[j] = num[i]; num[i] = temp; } } for( int i = 1; i <= num_ele_used; i++) cout << num[i] << " "; cout << endl; return 0; } -
@vikram.kuruguntla : little explanation of your algo will be helpful...
thanks in advance -
@atul007
I think logic is little clumpsy. I am trying my best to explain it.
Let a[12] contains elements like below.
a1 a2 a3 a4
b1 b2 b3 b4
c1 c2 c3 c4
Here Number of arrays ( number of rows ) = 3
Number of elements in each array = 4In final solution, elements will be saved like below.
1-a1 2-b1 3-c1 4-a2
5-b2 6-c2 7-a3 8-b3
9-c3 10-a4 11-b4 12-c4Below are the positions fo array1(a1, a2, a3, a4) saved in final solution
1, 4, 7, 10
Below are the positions fo array2(b1, b2, b3, b4) saved in final solution
2, 5, 8, 11
Below are the positions fo array2(c1, c2, c3, c4) saved in final solution
3, 6, 9, 12If you observe all the sequences,
common difference is 3 ( which is number of arrays)
initial term is 1 for first array, 2 for second array,
3 for third array ( array number can be treated as initial term)By taking all these into consideration, it is forming arthematic sequence.
a + ( n - 1) d
a - initial sequence - array number
n - position of the element in the array
d - common difference - Number of arrays-> GetDestinationPosition() function takes the original position as input
and returns the position where it is saved in final solution
for EX:
position 6 in original array( which is b2)
check in which array 6 belongs. That is second array.
check the position of 6 in that array. That is 2 in second array.
return second array + ( position in array - 1) * number of arrays
return 2 + ( 2 - 1) * 3
return 5 ( This is the position of b2 where it will be saved finally ).Main()
Loop all elements from an array i = 1...12
For i = 1,
Get the position of a1 where it will be saved in the final solution. Which is 1.
if( final saved position == current position ) ==> if( 1 == 1) true
increment i ==> i = 1 + 1 = 2
For i = 2
Get the position of a2 where it will be saved in final solution. Which is 4.
if( final saved position == current position ) ==> falseswap current position (which is i) and the destination position( which is 4),
Now a2 is saved in 4th position. The element in 4th position which is ( a4 )
came to position i(i.e 2). Which means using same i we will repeat the steps to
figure out where it will be saved in finally.
For i = 2
Using the original position position of a4( which is 4 saved in i=2 location),
find the final position. which is 10.
swap current position (which is i) and the destination position( which is 10).continue till current position and destination position is same.
Continue till i becomes 12.IsPositionAlreadySet()
Once i gets incremented in main loop, this function checks whether that position is alreay set earlier. By using 1..i-1, if we already set i's position then do not use that i location as it is already set.for ex if i = 4, then check from i = 1...3 whether 4th position is already set earlier. If already set then we have to increment i and repeat the steps.
Little debug may be helpful.
-
Nice Logic..gotta appreciate you..
-
guys, your soln is a bit overengineered: this is a simple array transpose:
int get_index(int idx, int N) { return (idx % 3) * N + (idx / 3); } void transform_array2(char *s[], int len) { int N = len / 3; for(int i = 0; i < len; i++) { int new_idx = get_index(i, N); while(new_idx < i) { new_idx = get_index(new_idx, N); } printf("i: %d; new_idx: %d\n", i, new_idx); std::swap(s[i], s[new_idx]); } for(int i = 0; i < len; i++) { printf("%s ", s[i]); } printf("\n"); }
Reply
You must log in to post.