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...
int algorithm ( int n ) {    if (n == 0)           return 1; else...
int algorithm ( int n ) {    if (n == 0)           return 1; else { return 2 * algorithm(n-1);    } }
1. public int add100(int[] array) { if (array.length < 100) { return 0; } int sum...
1. public int add100(int[] array) { if (array.length < 100) { return 0; } int sum = 0; for (int i = 0; i < 100; i++) { sum += array[i]; } return sum; } 2. int sum = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= i; j++) { sum++; sum++; } } for (int i = 0; i < n; i += 2) { sum++; } 3. nt...
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
For different input n, i.e., n = 1, n = 10, n = 20, write down...
For different input n, i.e., n = 1, n = 10, n = 20, write down the final value of counter for function1, function2, and function3. Explain the counter result through math calculation. #include <iostream> using namespace std; void function1(int n){ int count = 0; for(int x = 0; x < 12; x++){ cout<<"counter:"<< count++<<endl; } } void function2(int n){ int count = 0; for(int x = 0; x < n; x++){ cout<<"--- x="<<x<<"------"<<endl; for(int i =0; i < n;...
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; }
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT