## MAGMA Interview Question for Software Engineer / Developers

• 0

Comment hidden because of low score. Click to expand.
0
of 0 vote

1. By using recursion , we can solve this problem.
2. You need two Arrays one is Permuted array P[] which stores permutation numbers.
second Array is Presence Arraya Presence[] which tells whether a particular number is already included in P[] or not .

3. Compiled and tested Code for this.

``````#include<stdio.h>

#define TRUE 1
#define FALSE 0

void PrintArray(int *P, int n)
{
int i=0;
while(i<n)
{
printf(" %d , ", P[i] );
i++;
}
printf("\n");
}

/*
int P[]: Permutation Array which stores numbers permutation

int Presence[]:  it store either 0 or 1 for all numbers 1 - n .
if Presence[i] == 1, i'th number already there in P[] array.
So we will not include that number again.

int Count: tells so far how many numbers included in Permuted Array.
*/

void Permutations(int *P, int *Presence, int n, int count )
{
if( count >= n ||  n <= 0)
{
return;
}
else
{
int i = 1;

while(i <= n)
{
if( Presence[i] != TRUE )
{
Presence[i] = TRUE;
P[count] = i;
if( count + 1 == n )
{
PrintArray(P,n); // Print all elements in P[]
}
else
{
Permutations(P, Presence, n, count + 1 );
}
Presence[i] = FALSE;

}
i++;
}
}
}

int main()
{
int n = 5; // 1 to 10 numbers ermutations
int P[n] ; // Permutation Array, which store numbers permutation
int Presence[n]; // it store either 0 or 1 for all numbers 1 - n
int i =0;

/* Intializing arrays */
while( i < n )
{
P[i] = Presence[i] = 0;
i++;
}

Permutations(P, Presence, n, 0 );

return 0;
}``````

Is there any other better way ?

Comment hidden because of low score. Click to expand.
0

using one array is sufficient

Comment hidden because of low score. Click to expand.
0
of 0 vote

U can do it with single array...Call Perm(a,0).a has the integers 1 to n

``````Perm(int[] a,int start){
if(start==a.length){
Print a
return;
}
int temp;
for(int i=start;i<a.length;i++){
swap(a[start],a[i])
Perm(a,start+1)
swap(a[start],a[i])
}
}``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.