isPermutation function

Hello guys,

I need help in writing this kind of function

bool isPermutation( const unsigned a[], unsigned elements );
{3, 0, 2, 1} return true. While {3, 0, 2, 3} return false.

here is my miserable attempt
1
2
3
4
5
6
7
8
9
10
11
12
  unsigned diff = 0, pro = 1;
	for (unsigned i = 0; i < elements; i++)
	{
		if (a[i] != 0)
			pro *= a[i];
		
	}
	
	if (diff == 0)
		return true;
	else 
		return false;
diff will always be 0, because you aren't doing anything with it after you initialize it.
what should be done to fix it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
bool isPermutation(const unsigned a[], unsigned elements)
{
	unsigned temp = 0;
	unsigned arrayCopy[???];

	for (unsigned i = 0; i < elements; i++)
	{
		arrayCopy[i] = a[i];
	}

	for (unsigned k = 0; k < elements; k++)
		for (unsigned i = 0; i < elements - 1; i++)
		{
			if (arrayCopy[i] > arrayCopy[i + 1])
			{
				temp = arrayCopy[i];
				arrayCopy[i] = arrayCopy[i + 1];
				arrayCopy[i + 1] = temp;
			}
		}
	for (unsigned i = 0; i < elements; i++)
	{
		if (i != arrayCopy[i])
		{return false;}
	}
	return true;

}//isPermutation 


what can I say in here unsigned arrayCopy[doesn't like elements];
1
2
3
4
5
6
7
8
9
10
11
bool func(const unsigned ar[], unsigned size){
	
	for(int i=0; i<size; i++){
		bool b=false;
		for(int j=0; j<size; j++){
			if(i==ar[j]) b=true;
		}
		if(!b) return false;
	}
	return true;
}


ar[3] = {1,2,3} >> false
ar[3] = {2,0,1} >> true



what can I say in here unsigned arrayCopy[doesn't like elements];

you need dynamic memory allocation for unsigned arrayCopy[???];
ar[3] = {1,2,3} >> false
ar[3] = {2,0,1} >> true
------------------------------

it should return true in both cases
I am not sure what is the function you wrote?
I am not sure what is the function you wrote?


same as what your isPermutation function does.
are you not sure what your function does ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

bool isPermutation(const unsigned a[], unsigned elements)
{
	unsigned temp = 0;
	//unsigned arrayCopy[???];
	unsigned *arrayCopy = new unsigned[elements];	

	for (unsigned i = 0; i < elements; i++)
	{
		arrayCopy[i] = a[i];
	}

	for (unsigned k = 0; k < elements; k++)
		for (unsigned i = 0; i < elements - 1; i++)
		{
			if (arrayCopy[i] > arrayCopy[i + 1])
			{
				temp = arrayCopy[i];
				arrayCopy[i] = arrayCopy[i + 1];
				arrayCopy[i + 1] = temp;
			}
		}
	for (unsigned i = 0; i < elements; i++)
	{
		if (i != arrayCopy[i])
		{
			delete[] arrayCopy;
			return false;
		}
	}
	
delete[] arrayCopy;
return true;
}//isPermutation 

int main(){
	unsigned arr[] = {3,0,2,1};
	cout << isPermutation(arr, 4);
}
ar[3] = {1,2,3} >> false
ar[3] = {2,0,1} >> true
------------------------------

it should return true in both cases

Please describe in detail what the function is supposed to do. A permutation is a rearrangement of a set of items and that isn't want this is doing. Is it just returning true if there are no duplicates?

What should it return for these inputs:
{}
{1}
{ 18 300}
{100 101 102}
{1 2 5}
{1 2 3 4 1}
the isPermutation function which OP wrote returns true, if the n size array contains all of 0 to n-1; otherwise returns false.
OP probably wants different outcome.
Last edited on
To see if an array of N elements contains exactly the values 0 to N-1 you don't need to sort the auxiliary array. In fact, the aux array can just indicate whether you've seen the item already:
1
2
3
4
5
6
7
8
9
10
11
bool isPermutation(const unsigned a[], unsigned elements)
{
    vector<bool> alreadySeen(elements, false);
    for (unsigned i=0; i<elements; ++i) {
        if (a[i] >= elements || alreadySeen[a[i]]) {
            return false;
        }
        alreadySeen[a[i]] = true;
    }
    return true;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <set>
using namespace std;

bool func1(const unsigned ar[], unsigned size){	
	for(int i=0; i<size; i++){
		bool b=0;
		for(int j=0; j<size; j++){
			if(i==ar[j]) b=1;
		}
		if(!b) return 0;
	}
	return 1;
}

bool func2(const unsigned ar[], unsigned size){
	set<unsigned> su(ar, ar+size);
	set<unsigned>::reverse_iterator it = su.rbegin();
	if(su.size() != size || *it>=size ) return false;
	else return true;	
}

int main(){
	unsigned ar[] = {1,3,2,0};
	cout << func2(ar,4);
				
return 0;	
}
#anup30, thanks

this worked.
1
2
3
4
5
6
7
8
9
10
11
bool func(const unsigned ar[], unsigned size){
	
	for(int i=0; i<size; i++){
		bool b=false;
		for(int j=0; j<size; j++){
			if(i==ar[j]) b=true;
		}
		if(!b) return false;
	}
	return true;
}


and the code you added pointer to an array also worked.

so thank you again. What are the other lines of code you posted

{} = 0
{1} = 0
{ 18 300} =0
{100 101 102} =1
{1 2 5} =0
{1 2 3 4 1} =0
Topic archived. No new replies allowed.