Hello I have been struggling with this problem for quite som

Hello, I have been struggling with this problem for quite some time and any help would be great.

1) Write a function noDuplicates(arr1) that takes an array of integers as a parameter. The function should return True if there are no duplicates in the array and False otherwise. STIPULATION: You may only iterate through the array a single time.

My code:

def noDuplicates(arr1):
for duplicate in range(len(arr1)):
if len(arr1) != len(set(arr1)):
return False
else:
return True

2)Write a function named innerOuter(arr1, arr2) that takes 2 Sorted arrays of integers. Return True if every element in arr1 is in arr2.

ex. innerOuter( [1, 5, 7] , [1, 2, 3, 4, 5, 6, 7, 8, 9]) would return True

STIPULATION: You are only allowed to use a single loop to solve this problem. NO nested loops.

My Code:

def innerOuter(arr1, arr2):
for i in range(0,len(arr2)):
if arr1 == arr2[0:i]:
return True
else:
return False

Solution

Explanation: Here We are going to use one iteration to check whether there is any duplicates in arraay or not. We will start iterating aray from index 0.
arr[abs(arr1[i])] = -arr[abs(arr1[i])];

Here abs means absolute value.
Suppose array contains [1 2 3 1] Now when i=0 arr[abs(arr1[0])] = -arr[abs(arr1[0])] i.e. arr[1]=-arr[1] i.e array become [1 -2 3 1]

now i=1 arr[abs(arr1[1])] = -arr[abs(arr1[1])] i.e. arr[2]=-arr[2] i.e array become [1 -2 -3 1]

now i=2 arr[abs(arr1[2])] = -arr[abs(arr1[2])] i.e. arr[3]=-arr[3] i.e array become [1 -2 -3 -1]
now i=3 arr[abs(arr1[3])] = -arr[abs(arr1[3])] i.e. arr[1]=-arr[1] now here arr[1] is already negative It means we have duplicate element at ith position

boolean noDuplicates(int arr1[])
{
int i;
int size= len(arr1);
printf(\"The repeating elements are: \ \");
for (i = 0; i < size; i++)
{
if (arr[abs(arr1[i])] >= 0)
arr[abs(arr1[i])] = -arr[abs(arr1[i])];
else
return 0;
}
return 1;

}

Explanation:

As array are sorted so question becomes more easy.We will take two variable initially being 0 for example i,j here.
1)We will compare value of elemests of arrays. If value found to be equal ,we will increment both \'i\',\'j\'.So that we can comparre next element of array.
2)If element value in bigger array(arr2) found to be smaller than element value in smaller size array(arr1).We will use next element of bigger array for comparision hence increment \'j\'.
3)If element value in smaller array(arr1) is bigger than element value in bigger size array(arr2).We will return false(0). As for smaller array to be subset of bigger array ,all its elements must smaller than one of the bigger size array element.

In last we will check if value of \'i\' is greater than or equal to size of smaller array.If this is the case that means we found all elements of smaller size array(arr1) in bigger size aray(arr2). As we incremented \'i\' only when element got found in bigger array(arr2)

arr1[1, 5, 7] ,arr2= [1, 2, 3, 4, 5, 6, 7, 8, 9]
boolean innerOuter(int arr1[],int arr2[]){

int n=len(arr1));
int m =len(arr2);

int i=0;
int j=0;

while( i < n && j < m )
{
if( arr1[j] <arr2[i] )
j++;
else if( arr1[j] == arr2[i] )
{
j++;
i++;
}
else if( arr1[j] > arr2[i] )
return 0;
}

if( i < n )
return 0;
else
return 1;
}

Hello, I have been struggling with this problem for quite some time and any help would be great. 1) Write a function noDuplicates(arr1) that takes an array of i
Hello, I have been struggling with this problem for quite some time and any help would be great. 1) Write a function noDuplicates(arr1) that takes an array of i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site