Circular Shift in an Array
Let an Array is of n elements. You are asked to shift elements in a circular way in left or right direction. Array=[12,13,14,15,16]; here n=5. if shifting element s=2, Left shifting will result in [14,15,16,12,13], Right shifting will result in [15,16,12,13,14]. I am only going to show the left circular shift. First solution: Store left most element in a variable,then left shift whole array,push the stored element at the end of the array. Do this process s times. Costly solution because n number of shifting is required for s times. Second solution: Storing index (s+1) to n of a[] are copied to a new array Na[14,15,16,0,0] int k=s+1; loop(i,1,n-s) { Na[i]=a[k]; k++; } Storing index 1 to s of a[] are copied to a new array Na[14,15,16, 12,13 ] k=1; ...