Question

int function3(int arr1[], int arr2[], int n) {    int count = 0; for (int i...

int function3(int arr1[], int arr2[], int n) {   
int count = 0;
for (int i = 1; i <= n / i && arr1[i] != arr2[i]; i++) {
count++;
}
return count;

find time complex

Homework Answers

Answer #1

The function basically counts the total number of elements in array A which differ from the elements of array B at the same index.

Example: A = [1,2,3,4,5] and B = [2,2,3,4,6] , The answer for these two arrays will be 2. At index 0 the element differs and at index 4 the element differs.

Time Complexity: The for loop will run till n which is the total number of number of elements in the array. Rest of the operations are of unit 1 time complexity which are constant. So the overall time complexity will be O(n).

Happy Learning!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i];...
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i]; } return (double)sum/n; } Rewrite the function to eliminate the array subscripting (a[i]), using pointer arithmatic instead. Write a program that reads a line of input and checks if it is a palindrome (ignoring spaces) Write a program that takes the name of a file as a command line argument, and prints the contents of the file (similar to the linux 'cat' program). Write...
Given the following function: int bar(int n) {     if( n < 0 )         return...
Given the following function: int bar(int n) {     if( n < 0 )         return -2;     else if (n <= 1)         return 2;     else       return (3 + bar(n-1) + bar(n-2)); } What is returned by bar (4)? Show all the steps
public int linearSearch2(ArrayList<Integer> pList, Integer pKey) { for (int i = 0; i <= pList.size() /...
public int linearSearch2(ArrayList<Integer> pList, Integer pKey) { for (int i = 0; i <= pList.size() / 2; ++i) { if (pList.get(i).equals(pKey)) { return i; } else if (pList.get(pList.size() - 1 - i).equals(pKey)) { return pList.size() - 1 - i; } } return -1; } ----- Q1)Which function f(n) counts how many times the key operation(s) is(are) performed as a function of the list size n when pKey is not in pList (the worst case behavior of linearSearch2() occurs when pKey...
Define a recurrence F(n) for the code: int a(int i, int j){ if( j <= 1)...
Define a recurrence F(n) for the code: int a(int i, int j){ if( j <= 1) return i+20; else return j + a(i+10, j-3); } This recurrence I've come up with is F(n) = 2 if j <= 1 and F(n) = 2 + F(i+10, j-3). Now I need to solve this recurrence and represent it in big O notation which I'm unsure how to do. The main thing throwing me off is that there are two variables in the...
In C can you change this code to use Tail Call Recursion int min (int n,...
In C can you change this code to use Tail Call Recursion int min (int n, int[] input) { int i; int result; for (result = input[0], i = 0; i < n; i += 1) { if (result > input[i]) result = input[i]; } return result; }
bool update(char userInput){ int i = board_height - 1; int j = userInput - 'A'; while(i>=0){...
bool update(char userInput){ int i = board_height - 1; int j = userInput - 'A'; while(i>=0){ if(board[i][j] == ' '){ board[i][j] = next; if(checkRow(i,j,next) || checkColumn(i,j,next) || checkLeftDiagonal(i,j,next) || checkRightDiagonal(i,j,next)){ global_state = next - '0'; } if(next == '1'){ next = '2'; }else{ next = '1'; } break; } i--; } if(i == -1){ return false; } return true; } How can this be turned into int update instead of bool update
What is the time complexity ? int fact(int n) { if( n==1) {return 1;} n=n*fact(n-1); return...
What is the time complexity ? int fact(int n) { if( n==1) {return 1;} n=n*fact(n-1); return n; }
Convert the following C program to inline assembly int main(){ int i; printf("Hex Dec Oct Ch\n");...
Convert the following C program to inline assembly int main(){ int i; printf("Hex Dec Oct Ch\n"); for (i = 32; i < 256; i++){ printf("%2x %3d %3o %c\n",i,i,i,i); } return 0; } the code below shows where to do the assembly at: int main(){ int i = 0; char *hdr = "Hex Dec Oct Ch\n"; char *msg = " %2x %3d %3o %c\n"; printf("%s\n",hdr); ———asm{ // DO ASSEMBLY HERE } system("pause"); }
Assume an array a [0 … n –1] of int, and assume the following trace: for...
Assume an array a [0 … n –1] of int, and assume the following trace: for (int i = 0; i < n - 1; i++) if (a [i] > a [i + 1]) System.out.println (i); What is worstTime(n)? Statement Worst Case Number of Executions i = 0 1 i < n – 1 n i++ n - 1 a[i] > a[i+1] n - 1 System.out.println() n - 1 That is, worstTime(n) is: 4n – 2. So this is part...
convert the while loop into for loop x = int(input('Enter initial value: ')) count = 0...
convert the while loop into for loop x = int(input('Enter initial value: ')) count = 0 if(x<1): print('Error') exit() print('Initial value is: ',x,' ',end='') while(x!=1): if(x%2==0): x=x/2 else: x=3*x+1 count+=1 if(x!=1): rint('Next value is: ',int(x),' ',end='') else: print('Final value ',int(x),', ',end='') print('number of operations performed ',int(count),' ',end='') break
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT