Answer to Question #226832 in Java | JSP | JSF for Ayush

Question #226832
Suppose there is a list s1->s2->s3->………->sn-1
->sn. given to you. You need to modify this list in
such a way that it will be s1->sn
->s2->sn-1
->s3->sn-2…….
Note: algorithm should be inplace without modifying the values of the nodes
Algorithm explanation + Pseudo-code + runtime and data structure used)
1
Expert's answer
2021-08-17T12:54:14-0400

s1->s2->s3->………->sn-1->sn.

Data structure that can be used to modified the list above is an array.

Algorithm

Input:

An array of integers representing the list.

Output:

s1->sn->s2-sn-1...

Explanation

Create an array to store the final list.

Traverse the whole array with a loop.


 Pseudo-code

This code is written in C programming language.
The program uses an array of size 5 to implement the logic above. 
The elements and the size of this array can be changed 




#include <stdio.h>
#include <time.h>
int main()
{
    double timeSpent = 0.0;
 
    clock_t beginTime = clock();
 
    int arr[5] = {3,4,5,6,7};
   
   int arr4[5];
   int n = 5;
    int k= 1;
    for(int j=0; j<5; j++){
    	
			int index=0;
			if(j == 0){
				index = j;
			}
			else if(j !=0 && j% 2!=0){
				index = n-1;
				n--;
			}
			else if(j%2==0){
				
				index = k;
				k = k + 1;
				
			}
    		
    	arr4[j] = arr[index];
        
    }
       printf("\nThe array becomes: \t");
for(int i=0; i<5; i++){
         printf(" %d->\t",arr4[i]);
    }
	   clock_t endTime = clock();
	   timeSpent += (double)(endTime - beginTime) / CLOCKS_PER_SEC;
 
    printf("\nRunning time %f seconds", timeSpent);
	    


    return 0;
}




Running time is 0.002000 seconds





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS