Question

In: Computer Science

1 : An array a is defined to be self-referential if for i=0 to a.length-1, a[i]...

1 : An array a is defined to be self-referential if for i=0 to a.length-1, a[i] is the count of the number of times that the value i appears in the array. As the following table indicates, {1, 2, 1, 0} is a self-referential array.

Here are some examples of arrays that are not self-referential:

{2, 0, 0} is not a self-referential array. There are two 0s and no 1s. But unfortunately there is a 2 which contradicts a[2] = 0.

{0} is not a self-referential array because there is one 0, but since a[0] = 0, there has to be no 0s.

{1} is not a self-referential array because there is not a 0 in the array as required by a[0] = 1.

Self-referential arrays are rare. Here are the self-referential arrays for arrays of lengths up to 10 elements:

{1, 2, 1, 0} (see above)
{2, 0, 2, 0} (there are two 0s, no 1s, two 2s and no 3s
{2, 1, 2, 0, 0}  (there are two 0s, one 1, two 2s, no 3s and no 4s)
{3, 2, 1, 1, 0, 0, 0} (there are three 0s, two 1s, one 2, one 3, no 4s, 5s or 6s)
{4, 2, 1, 0, 1, 0, 0, 0} (there are four 0s, two 1s, one 2, no 3s, one 4, and no 5s, 6s, or 7s)
{5, 2, 1, 0, 0, 1, 0, 0, 0} (there are five 0s, two 1s, one 2, no 3s or 4s, one 5, and no 6s, 7s, or 8s)
{6, 2, 1, 0, 0, 0, 1, 0, 0, 0} (there are six 0s, two 1s, one 2, no 3s, 4s, or 5s, one 6, and no 7s, 8s, or 9s)

Write a function named isSelfReferential that returns 1 if its array argument is self-referential, otherwise it returns 0.

If you are programming in Java or C#, the function signature is
   int isSelfReferential(int[ ] a)

If you are programming in C or C++, the function signature is
   int isSelfReferential(int a[ ], int len) where len is the number of elements in the array

Solutions

Expert Solution

We have to just verify the condition whether a[i] is the count that element i appears in the array for that we can keep a count array for 0 to n-1 and then verify whether it matches the count or not in the original array.

The code is

#include <iostream>

using namespace std;
int isSelfReferential(int a[],int len){
int counts[len]={};//counts array to store the counts of integers
for(int i=0;i<len;i++){
if(a[i]<len){
//if a[i] is in range of 0 to n-1 then increment the count
counts[a[i]]++;
}
}
//verifying the result now
for(int i=0;i<len;i++)
{
if(a[i]!=counts[i])
return 0;//if count of i is not a[i]
}
return 1;
}
int main() {
//testing
int a[]={1, 2, 1, 0};
int b[]={5, 2, 1, 0, 0, 1, 0, 0, 0};
int c[]={5, 2, 1, 0, 0, 1, 3, 2, 0};
int d[]={1,2, 2, 1, 0, 0, 1, 0, 0, 0};
cout<<isSelfReferential(a,sizeof(a)/sizeof(int))<<"\n";
cout<<isSelfReferential(b,sizeof(b)/sizeof(int))<<"\n";
cout<<isSelfReferential(c,sizeof(c)/sizeof(int))<<"\n";
cout<<isSelfReferential(d,sizeof(d)/sizeof(int))<<"\n";
}


Related Solutions

Prove that the function defined to be 1 on the Cantor set and 0 on the...
Prove that the function defined to be 1 on the Cantor set and 0 on the complement of the Cantor set is discontinuous at each point of the Cantor set and continuous at every point of the complement of the Cantor set.
f(t) = 1- t 0<t<1 a function f(t) defined on an interval 0 < t <...
f(t) = 1- t 0<t<1 a function f(t) defined on an interval 0 < t < L is given. Find the Fourier cosine and sine series of f and sketch the graphs of the two extensions of f to which these two series converge
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for...
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for find a pair of elements A[i] and A[i+1] such that A[i] != A[i+1]. can you always find such pair and why
Let the Fibonacci sequence be defined by F0 = 0, F1 = 1 and Fn =...
Let the Fibonacci sequence be defined by F0 = 0, F1 = 1 and Fn = Fn−1 + Fn−2 for n ≥ 2. Use induciton to prove that F0F1 + F1F2 + · · · + F2n−1 F2n = F^2 2n for all positive integer n.
Fibonacci numbers are defined by F0 = 0, F1 = 1 and Fn+2 = Fn+1 +...
Fibonacci numbers are defined by F0 = 0, F1 = 1 and Fn+2 = Fn+1 + Fn for all n ∈ N ∪ {0}. (1) Make and prove an (if and only if) conjecture about which Fibonacci numbers are multiples of 3. (2) Make a conjecture about which Fibonacci numbers are multiples of 2020. (You do not need to prove your conjecture.) How many base cases would a proof by induction of your conjecture require?
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++)...
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++) sum = sum + I; printf(“sum=%d”, sum); 2) int i, v[10]; int min, k; for(i=0; i < 10; i++) scanf(“%d”, &v[i]); min = v[0] for(i=1; I < 10; i++) if(min > v[i]){ min = v[i] k = I; } printf(“%d=>%d”, k, min);
Simulate an ATM machine. Create ten accounts in an array with id 0, 1, . ....
Simulate an ATM machine. Create ten accounts in an array with id 0, 1, . . . , 9, and initial balance $100. The system prompts the user to enter an id. If the id is entered incorrectly, ask the user to enter a correct id. Once an id is accepted, the main menu is displayed as shown in the sample run. You can enter a choice 1 for viewing the current balance, 2 for withdrawing money, 3 for depositing...
Given an array A[1..n] of integers - all of whose numbers are in the range [0,...
Given an array A[1..n] of integers - all of whose numbers are in the range [0, n^3 − 1] give an algorithm which sorts them in O(n) time.
Polynomial Multiplication by Divide-&-Conquer A degree n-1 polynomial ? (?) =Σ(n-1)(i=0) ???i = ?0 + ?1?...
Polynomial Multiplication by Divide-&-Conquer A degree n-1 polynomial ? (?) =Σ(n-1)(i=0) ???i = ?0 + ?1? + ?2?2 ... + ??−1?n-1 can be represented by an array ?[0. . ? − 1] of its n coefficients. Suppose P(x) and Q(x) are two polynomials of degree n-1, each given by its coefficient array representation. Their product P(x)Q(x) is a polynomial of degree 2(n-1), and hence can be represented by its coefficient array of length 2n-1. The polynomial multiplication problem is to...
Consider the number of strings on {0, 1} defined by the requirement below. For each construct...
Consider the number of strings on {0, 1} defined by the requirement below. For each construct a dfa. Every 00 is followed by a 1. For example, the strings 101, 0010, 0010011001 are in the language, but 0001 and 00100 are not. The leftmost symbol differs from the rightmost one.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT