Question

Given a sorted array A with distinct values which has been rotated r times to the...

Given a sorted array A with distinct values which has been rotated r times to the left. Find r in least possible time. Provide python source code, time complexity, and pseudocode.

Input: The sorted array ? that has been rotated r times to the left.

Output: The value and ?.

Homework Answers

Answer #1
arr = [1, 2, 3, 4, 5];     
#n determine the number of times an array should be rotated    
n = 3;    
     
#Displays original array    
print("Original array: ");    
for i in range(0, len(arr)):    
    print(arr[i]),     
     
#Rotate the given array by n times toward left    
for i in range(0, n):    
    #Stores the first element of the array    
    first = arr[0];    
        
    for j in range(0, len(arr)-1):    
        #Shift element of array by one    
        arr[j] = arr[j+1];    
            
    #First element of array will be added to the end    
    arr[len(arr)-1] = first;    
     
print();    
     
#Displays resulting array after rotation    
print("Array after",n ,"left rotation: " );    
for i in range(0, len(arr)):    
    print(arr[i]),    

Time Complexity :- O(n);

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
Given an unsorted integer array A of size n, develop a python version pseudocode with time...
Given an unsorted integer array A of size n, develop a python version pseudocode with time complexity as low as possible to find two indexes i and j such that A[i] + A[j] = 100. Indexes i and j may or may not be the same value.
You are asked to implement a C++ class to model a sorted array of unsigned integers....
You are asked to implement a C++ class to model a sorted array of unsigned integers. The class is to be used in an embedded application that cannot assume the presence of the STL. The array has to be dynamically allocated in such a way that allows programmers using it to specify the required size. Your class should should: (1) provide the appropriate constructors and destructor; (2) provide methods for updating, and showing numbers in/to the array (e.g., to be...
DESCRIPTION: You will be given a 2D array, called matrix, of Strings. The array has an...
DESCRIPTION: You will be given a 2D array, called matrix, of Strings. The array has an unknown number of cells filled with data. Your goal is to iterate through the 2D array and keep a count of how many cells are full. You will be given the dimensions of the array. INPUT: All input has been handled for you: Two integers denoting the dimensions of the array. These are the integer variables rows and cols. A partially filled 2-D array....
Assume the 2D array below has been assigned valid integer values. NUM_PLAYERS and NUM_CARDS are global...
Assume the 2D array below has been assigned valid integer values. NUM_PLAYERS and NUM_CARDS are global const ints with positive values. Write a code snippet which computes the total of all the elements in the 2D array. int cards[NUM_PLAYERS][NUM_CARDS];
1a. Given an array a of n distinct integers, a[i] = m is a local minimum...
1a. Given an array a of n distinct integers, a[i] = m is a local minimum of a iff a[i − 1] > m and a[i + 1] < m. Suppose that n ≥ 3, a[0] > a[1], and a[n − 2] < a[n − 1]. Explain why these “end point” conditions guarantee that a has a local minimum. Hint: argue by way of contradiction. (10 points) 1b. Assuming that a satisfies the conditions described in part a, clearly and...
Complete the following function (detailed instructions are given below the code): private static int predecessor(int array[],...
Complete the following function (detailed instructions are given below the code): private static int predecessor(int array[], int arrayLen, int key) { // Complete this function. } Given a set of numbers, the predecessor of a number x is the highest number in the set that is less than or equal to x. Thus, if I have the set {6; 9; 10; 13; 22; 31; 34; 88}, then the predecessor of 31 is 31 itself, whereas the predecessor of 30 is...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics relating to data supplied by the user. The program will ask the user to enter the number of items making up the data. It will then ask the user to enter data items one by one. It will store the data items in a double array. Then it will perform a number of statistical operations on the data. Finally, it will display a report...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++ 11. Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array out- put parameter (still in ascending order). The function shouldn’t assume that both its input parameter arrays are the same length but can assume First array 04 Second array Result array that one array doesn’t contain two copies of...
In this lab, you complete a partially prewritten C++ program that uses an array. The program...
In this lab, you complete a partially prewritten C++ program that uses an array. The program prompts the user to interactively enter eight batting averages, which the program stores in an array. The program should then find the minimum and maximum batting average stored in the array as well as the average of the eight batting averages. The data file provided for this lab includes the input statement and some variable declarations. Comments are included in the file to help...