Question

In: Computer Science

for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for...

for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for find a pair of elements A[i] and A[i+1] such that A[i] != A[i+1]. can you always find such pair and why

Solutions

Expert Solution

/*If you have any query do comment in the comment section else like the solution*/

Since it is given that A[0] != A[n-1], it is always possible to find such pair. Consider below example where an array has n elements. Let's say A[0] = x and A[n-1] = y as A[0] != A[n-1]. Now we can fill elements from 1 to n-2 either with x or y, to make sure that maximum elements in the array are same and we are trying . If we fill the remaining array with x then the required pair can be found at position A[n-2] and A[n-1] and if we fill the remaining array with y then the required pair can be found at position A[0] and A[1]. In case you try to make changes to remaining elements then in that case also you will find a point where two consecutive elements are different.
Eg: n = 5, A[0] = 1, A[4] = 3
Possibilites:

1. Fill remaining array with 1 then

{1, 1, 1, 1, 4}

2. Fill remaining array with 4 then

{1, 4, 4, 4, 4}

In both the above cases we can find a pair where A[i]!=A[i+1] given a condition that A[0] != A[n-1];

Algorithm:

for i = 1 to i = n-1
   if(A[i] != A[i-1]) {
       print("Pair found")
       return true;
   }
return false;


Related Solutions

Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1]...
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1] of n elements. void ISORT (dtype A[ ], int n) { int i, j; for i = 1 to n – 1 {     //Insert A[i] into the sorted part A[0..i – 1]     j = i;     while (j > 0 and A[j] < A[j – 1]) {         SWAP (A[j], A[j – 1]);         j = j – 1 }     }...
algorithm binarySearch input bottom, top: a number    a: array a[0] to a[n-1] of numbers    x: a...
algorithm binarySearch input bottom, top: a number    a: array a[0] to a[n-1] of numbers    x: a number output result: true if x in a false otherwise Sideeffect NA Plan if (top < bottom) result := false else // get the middle of the array (refining) middle := (top - bottom)/2 + bottom (refining by +1 or -1 or integer division …) // example what is midpoint between 6 and 8 // (8-6)/2 = 1 we need to add 6 to...
Triplicates. Given three lists of N names each, devise a linearithmic algorithm to determine if there...
Triplicates. Given three lists of N names each, devise a linearithmic algorithm to determine if there is any name common to all three lists, and if so, return the first such name
1. We have an array A of size n. There are only positive integers in this...
1. We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. Design an efficient algorithm to find the maximum difference between any two...
1. We have an array A of size n. There are only positive integers in this...
1. We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. Design an efficient algorithm to find the maximum difference between any two...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
Suppose that each row of an n × n array A consists of 1’s and 0’s...
Suppose that each row of an n × n array A consists of 1’s and 0’s such that, in any row of A, all the 1’s come before any 0’s in that row. Assuming A is already in memory, describe a method running in O(n log n) time (not O(n2) time!) for counting the number of 1’s in A.
An array A[0..n - 2] contains n-1 integers from 1 to n in increasing order. (Thus...
An array A[0..n - 2] contains n-1 integers from 1 to n in increasing order. (Thus one integer in this range is missing.) Design an algorithm in ​(Theta(log n)) to find the missing integer. Your algorithm should be given in pseudo code. For example, the array A could be {1, 2, 3, 4, 6, 7, 8, 9, 10} in which 5 is missing.
Consider the following program specification: Input: An integer n > 0 and an array A[0..(n –...
Consider the following program specification: Input: An integer n > 0 and an array A[0..(n – 1)] of n integers. Output: The largest index b such that A[b] is the largest value in A[0..(n – 1)]. For example, if n = 10 and A = [ 4, 8, 1, 3, 8, 5, 4, 7, 1, 2 ] (so A[0] = 4, A[1] = 8, etc.), then the program would return 4, since the largest value in A is 8 and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT