Computer Science, asked by maninichoudhury686, 8 months ago

Given an array ar[]={11,22,33,44,55,66,77}, Answer the following questions:

i. What is the 4th element of an array?

ii. What is the start index and end index of array ar[]​

Answers

Answered by pandeysushilkumar3
0

Answer:

i) Search for courses, skills, and videos

Main content

Binary search

Implementing binary search of an array

Google ClassroomFacebookTwitter

Email

Let's see how to think about binary search on a sorted array. Yes, JavaScript already provides methods for determining whether a given element is in an array and, if it is, its location (as do many programming languages), but we want to implement it ourselves, to understand how you can implement such methods. Here's a JavaScript array of the first 25 prime numbers, in order:

var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];

Suppose we want to know whether the number 67 is prime. If 67 is in the array, then it's prime.

We might also want to know how many primes are smaller than 67. If we find the position of the number 67 in the array, we can use that to figure out how many smaller primes exist.

The position of an element in an array is known as its index. Array indices start at 0 and count upwards. If an element is at index 0 then it is the first element in the array. If an element is at index 3, then it has 3 elements which come before it in the array.

Looking at the example below, we can read the array of prime numbers from left to right, one at a time, until we find the number 67 (in the pink box) and see that it is at array index 18. Looking through the numbers in order like this is a linear search.

Once we know that the prime number 67 is at index 18, we can identify that it is a prime. We can also quickly identify that there are 18 elements which come before 67 in the array, meaning that there are 18 prime numbers smaller than 67.

Explanation:

ii) Find start and ending index of an element in an unsorted array

Given an array of integers, task is to find the starting and ending position of a given key.

Examples:

Input : arr[] = {1, 2, 3, 4, 5, 5}

Key = 5

Output : Start index: 4

Last index: 5

Explanation: Starting index where 5

is present is 4 and ending address is 5.

Input :arr[] = {1, 3, 7, 8, 6},

Key = 2

Output : Key not present in array

Input :arr[] = {1, 8, 7, 8, 6},

Key = 7

Output : Only one occurrence of

key is present at index 2

Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

We traverse array from beginning to find first occurrence. If element is present, then we traverse from end also to find last occurrence.

// CPP program to find starting and ending

// indexes of repeated numbers in an array

#include <iostream>

using namespace std;

// Function to find starting and end index

void findIndex(int a[], int n, int key)

{

int start = -1;

// Traverse from beginning to find

// first occurrence

for (int i = 0; i < n; i++) {

if (a[i] == key) {

start = i;

break;

}

}

if (start == -1) {

cout << "Key not present in array";

return;

}

// Traverse from end to find last

// occurrence.

int end = start;

for (int i = n - 1; i >= start; i--) {

if (a[i] == key) {

end = i;

break;

}

}

if (start == end)

cout << "Only one key is present at index : "

<< start;

else {

cout << "Start index: " << start;

cout << "nLast index: " << end;

}

}

// Driver Code

int main()

{

int a[] = { 1, 2, 7, 8, 8, 9, 8, 0, 0, 0, 8 };

int n = sizeof(a) / sizeof(a[0]);

// Key to find

int key = 8;

// Calling function

findIndex(a, n, key);

return 0;

}

Output:

Start index: 3

Last index: 10

Similar questions