Question

There is an extremely large array A such that the first n entries are positive integers,...

There is an extremely large array A such that the first n entries are positive integers, and all remaining entries are 0. Find n. You can use any programming language you want, but state the runtime of the program.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ans.cpp

#include<iostream>
#include<vector>
using namespace std;
int findn(vector<int> v)
{
int length = v.size(); // total length of array
int idx =0; // return 0 if all number are zero
int st=0;
int end =length-1;
while(st<=end)
{
int mid =(st+end)/2; // search at middle
if(v[mid]==0) // if middle element is 0
end = mid-1; // set end to mid-1
else
st = mid+1; // set st to mid+1
}
return st;
}
int main()
{
int t;
cin>>t;
while(t--)
{ int n,x;
std::vector<int> v;
cout<<"enter vector length"<<endl;
cin>>n;
cout<<"enter vector of size "<<n<<endl;
for(int i=0;i<n;i++)
{ cin>>x;
v.push_back(x);
}
int ans =findn(v);
cout<<"n = "<<ans<<endl<<endl;
}
// find n in this position of last postive integer in vector

}

Runtime complexity is O(log(size of array A)). logrithmic time complexity

Add a comment
Know the answer?
Add Answer to:
There is an extremely large array A such that the first n entries are positive integers,...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program to subtract large unsigned integers. Your program should prompt and read in two...

    Write a program to subtract large unsigned integers. Your program should prompt and read in two large unsigned integers. The two large integers should be stored arrays, one array for each integer. The integers should be stored with one digit per location in the two arrays. The first integer should be no smaller than the second. Your program should subtract the second integer from the first. The result should be stored in an array, again one digit per location in...

  • Write a program to subtract large unsigned integers. Your program should prompt and read in two...

    Write a program to subtract large unsigned integers. Your program should prompt and read in two large unsigned integers. The two large integers should be stored arrays, one array for each integer. The integers should be stored with one digit per location in the two arrays. The first integer should be no smaller than the second. Your program should subtract the second integer from the first. The result should be stored in an array, again one digit per location in...

  • 1. (16 pts.) Sorted Array Given a sorted array A of n (possibly negative) distinct integers,...

    1. (16 pts.) Sorted Array Given a sorted array A of n (possibly negative) distinct integers, you want to find out whether there is an index i for which Al = i. Give a divide-and-conquer algorithm that runs in time O(log n). Provide only the main idea and the runtime analysis.

  • Given an array A[1..n] of positive integers and given a number x, find if any two...

    Given an array A[1..n] of positive integers and given a number x, find if any two numbers in this array sum upto x. That is, are there i,j such that A[i]+A[j] = x? Give an O(nlogn) algorithm for this. Suppose now that A was already sorted, can you obtain O(n) algorithm?

  • 3. Write a program to read a (binary) file of integers, sort the integers, and write...

    3. Write a program to read a (binary) file of integers, sort the integers, and write them back to the same file. Assume that all the numbers can be stored in an array. (Exercise 3) 4. Repeat exercise 3, but assume that only 20 numbers can be stored in memory (in an array) at any one time. Hint: you will need to use at least two additional files for temporary output. Please finish the question in C language programming, thank...

  • Let S be a sequence of n distinct integers stored in an array as array elements...

    Let S be a sequence of n distinct integers stored in an array as array elements S[1], S[2], · · · , S[n]. Use the technique of dynamic programming to find the length of a longest ascending subsequence of entries in S. For example, if the entries of S are 11, 17, 5, 8, 6, 4, 7, 12, 3, then one longest ascending subsequence is 5, 6, 7, 12. Specifically: (a) define a proper function and find the recurrence for...

  • implement void Quick_Sort(int A[],int l,int r) to sort array of integers in ascending order using the...

    implement void Quick_Sort(int A[],int l,int r) to sort array of integers in ascending order using the first element as a pivot, you can add any parameter you want Note : you are only allowed to use java language

  • Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8...

    Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...

  • Write a program that asks the user to enter 1000 integers to be stored in an...

    Write a program that asks the user to enter 1000 integers to be stored in an array called "numbers". Since the same integer might occur (exist) in the array multiple times, your program needs to fill a second array, called "Isolate" that contains all the integers from the first array but NOT REPAPTED (every integer will exist only once). Finally, your program will print the integers of the second array sorted by occurrence (occurrence is: the number of times the...

  • Consider an array of length n containing positive and negative integers in random. Write a C++...

    Consider an array of length n containing positive and negative integers in random. Write a C++ code that rearranges the integers so that the negative integers appear before the positive integers. Your solution should use: a. O(n^2) b. O(n)

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT