Find K Smallest Elements in an Array - java

I want to find the K smallest elements in an array, and I was able to basically sort my array into a min-heap structure but I am still getting the wrong output.
Here are my inputs:
arr = [9,4,7,1,-2,6,5]
k = 3
Here's the Code:
public static int[] findKSmallest(int[] arr, int k) {
int[] result = new int[k];
int heapSize = arr.length;
// Write - Your - Code
for (int i = (heapSize - 1) / 2; i >= 0; i--) {
minHeap(arr, i, heapSize);
}
for (int j = 0; j < k; j++) {
result[j] = arr[j];
}
return result;
}
public static void minHeap(int[] arr, int index, int heapSize) {
int smallest = index;
while (smallest < heapSize / 2) {
int left = (2 * index) + 1; // 1 more than half the index
int right = (2 * index) + 2; // 2 more than half the index
if (left < heapSize && arr[left] < arr[index]) {
smallest = left;
}
if (right < heapSize && arr[right] < arr[smallest]) {
smallest = right;
}
if (smallest != index) {
int temp = arr[index];
arr[index] = arr[smallest];
arr[smallest] = temp;
index = smallest;
} else {
break;
}
}
}
Here is my expected output:
[-2,1,4]
Though my output is:
[-2,1,5]
Please I would like to know where I went wrong.

After you build your minHeap you have to extract element and appropriately adjust tree. You simply took elements from array by index, which is not how heap works.
I slightly modified your minHeap() method to use recursion and you should check arr[smallest] < arr[left] not the orther way around.
public static int[] findKSmallest(int[] arr, int k) {
int[] result = new int[k];
int heapSize = arr.length;
// Write - Your - Code
for (int i = (heapSize - 1) / 2; i >= 0; i--) {
minHeap(arr, heapSize, i);
}
// extract elements from heap
for (int i = heapSize - 1; i > 0; i--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
minHeap(arr, 0, i);
}
for (int j = 0; j < k; j++) {
result[j] = arr[j];
}
return result;
}
public static void minHeap(int[] arr, int index, int heapSize) {
int smallest = index;
int left = (2 * index) + 1; // 1 more than half the index
int right = (2 * index) + 2; // 2 more than half the index
if (left < heapSize && arr[smallest] < arr[left]) {
smallest = left;
}
if (right < heapSize && arr[smallest] < arr[right]) {
smallest = right;
}
if (smallest != index) {
int temp = arr[index];
arr[index] = arr[smallest];
arr[smallest] = temp;
minHeap(arr, smallest, heapSize);
}
}
Hope this helps. As expected the result is:
[-2, 1, 4]

You can use Arrays.stream​(int[]) method:
int[] arr = {9, 4, 7, 1, -2, 6, 5};
int k = 3;
int[] minHeap = Arrays.stream(arr).sorted().limit(k).toArray();
System.out.println(Arrays.toString(minHeap)); // [-2, 1, 4]

Since Java 8 we can do Sorting stream
Alternative code:
Arrays.stream(arr).sorted().boxed().collect(Collectors.toList()).subList(0, k);
where:
int[] arr = {9,4,7,1,-2,6,5};
int k = 3; // toIndex
Alternative code in context and testbench:
public static void main(String[] args) {
int[] arr = {9, 4, 7, 1, -2, 6, 5};
int k = 3; // toIndex
List<Integer> listResult = Arrays.stream(arr)
.sorted().boxed().collect(Collectors.toList()).subList(0, k);
// print out list result
System.out.println("Output of list result: " + listResult);
int[] arrayResult = listResult.stream().mapToInt(i -> i).toArray();
// print out array result
List<String> arrayResultAsString = Arrays.stream(arrayResult)
.boxed().map(i -> String.valueOf(i)).collect(Collectors.toList());
System.out.println("Output of array result: " + arrayResultAsString);
}
Output:
Output of list result: [-2, 1, 4]
Output of array result: [-2, 1, 4]

If K is relatively small to the array size my recommendation is to use selection sort, so it may be a design pattern Strategy - depending on K to array size relation with for example two procedures: selection sort for small K and quicksort for huge K. Instead of playing with design patterns good may be also simple if statement e.g. selection<=30%<quick

Related

Given an array of n positive integers and k. I have to find min number of swaps required to bring all the numbers less than or equal to k together

Logic used to solve the question
Maintain a count variable, two pointers i and j and scan them from left to right(i) and right to left(j).
Assign i the index of first element <= k.
If i and j stop at a particular position that means they need to be exchanged and update count.
Exit the loop when i and j cross each other.
Return no of exchanges happened ; that is return count.
Question
Can anybody help me find out why count returns wrong for some cases(like the one in unit testing where it returns 4 but correct output is 3)? What's the bug here?
public class MinSwaps {
public static int minSwap(int[] arr, int n, int k) {
int count = 0;
int lo = 0;
int hi = arr.length - 1;
int i = lo - 1;
int j = hi + 1;
// assign i the position of first element <= k in arr
for (int l = 0; l < n; l++) {
if (arr[l] <= k) {
i = l;
break;
}
}
while (true) {
// keep scanning i(from left to right) until arr[i] > k
while (arr[++i] <= k) {
if (i == hi) {
break;
}
}
// keep scanning j(right to left) until arr[j] <= k
while (arr[--j] > k) {
if (j == lo) {
break;
}
}
// exit loop if i and j cross each other
if (i >= j) {
break;
}
// exchange elements at i and j ( when they have stopped )
exch(arr, i, j);
count++;
}
return count;
}
private static void exch(int[] a, int i, int j) {
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
// unit testing
public static void main(String[] args) {
int[] arr = {4, 16, 3, 8, 13, 2, 19, 4, 12, 2, 7, 17, 4, 19, 1};
StdOut.println(minSwap(arr, 15, 9));
}
}

How to find missing Smallest Positive Integer using the lamda expresion in java

that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.
For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5.
Given A = [1, 2, 3], the function should return 4.
Given A = [−1, −3], the function should return 1.
Write an efficient algorithm for the following assumptions:
import java.util.*;
class Main {
/* Utility function that puts all non-positive
(0 and negative) numbers on left side of
arr[] and return count of such numbers */
static int segregate(int arr[], int size)
{
int j = 0, i;
for (i = 0; i < size; i++) {
if (arr[i] <= 0) {
int temp;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
// increment count of non-positive
// integers
j++;
}
}
return j;
}
/* Find the smallest positive missing
number in an array that contains
all positive integers */
static int findMissingPositive(int arr[], int size)
{
int i;
// Mark arr[i] as visited by making
// arr[arr[i] - 1] negative. Note that
// 1 is subtracted because index start
// from 0 and positive numbers start from 1
for (i = 0; i < size; i++) {
int x = Math.abs(arr[i]);
if (x - 1 < size && arr[x - 1] > 0)
arr[x - 1] = -arr[x - 1];
}
// Return the first index value at which
// is positive
for (i = 0; i < size; i++)
if (arr[i] > 0)
return i + 1; // 1 is added becuase indexes
// start from 0
return size + 1;
}
/* Find the smallest positive missing
number in an array that contains
both positive and negative integers */
static int findMissing(int arr[], int size)
{
// First separate positive and
// negative numbers
int shift = segregate(arr, size);
int arr2[] = new int[size - shift];
int j = 0;
for (int i = shift; i < size; i++) {
arr2[j] = arr[i];
j++;
}
// Shift the array and call
// findMissingPositive for
// positive part
return findMissingPositive(arr2, j);
}
// main function
public static void main(String[] args)
{
int arr[] = { 0, 10, 2, -10, -20 };
int arr_size = arr.length;
int missing = findMissing(arr, arr_size);
System.out.println("The smallest positive missing number is " + missing);
}
}
}
If you need to use stream, the more straightfoward, but not optimal way to do it is to create an infinite stream, starting at 1 and return the first that is not in arr:
int[] arr = { 1, 3, 6, 4, 1, 2 };
Set<Integer> arrSet = Arrays.stream(arr).boxed().collect(Collectors.toSet());
Optional<Integer> found = IntStream.iterate(1, o -> o + 1).boxed()
.filter(value -> !arrSet.contains(value))
.findFirst();
found.ifPresent(System.out::println);
Output
5
As pointed out this is very inefficient, but in terms of computational complexity I believe is optimal, at least for the worst case i.e. the one you have to look at all the elements.
Below you can find the missing positive integer using Streams -
int ar[] = { 0, 10, 2, -10, -20 };
int max = Arrays.stream(ar).max().getAsInt();
System.err.println("maxvalue "+max);
int val = IntStream.range(1, max).filter(i->!Arrays.stream(ar).anyMatch(x->x==i))
.findFirst().getAsInt();
System.out.println(val);
int[] val1 = IntStream.range(1, max).filter(i->!Arrays.stream(ar).anyMatch(x->x==i)).map(p->p).toArray();
System.out.println("------------------");
IntStream.of(val1).forEach(System.out::println);
int[] valEven = IntStream.range(1, max).filter(i->Arrays.stream(val1).anyMatch(x->i%2==0)).map(p->p).toArray();
System.out.println("------------------");
IntStream.of(valEven).forEach(System.out::println);
int[] valOdd = IntStream.range(1, max).filter(i->!Arrays.stream(val1).anyMatch(x->i%2==0)).map(p->p).toArray();
System.out.println("------------------");
IntStream.of(valOdd).forEach(System.out::println);
int[] valOdd1 = IntStream.range(1, max).filter(i->Arrays.stream(val1).noneMatch(x->i%2==0)).map(p->p).toArray();
System.out.println("------------------");
IntStream.of(valOdd1).forEach(System.out::println);
int[] valEven1 = IntStream.range(1, max).filter(i->!Arrays.stream(val1).noneMatch(x->i%2==0)).map(p->p).toArray();
System.out.println("------------------");
IntStream.of(valEven1).forEach(System.out::println);
You can also do a mix of stream and primitive int loop:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numberSet1 = {1, 3, 6, 4, 1, 2};
int[] numberSet2 = {-1, -3};
int[] numberSet3 = {1, 2, 3};
System.out.println(calcularPrimero(numberSet1));
System.out.println(calcularPrimero(numberSet2));
System.out.println(calcularPrimero(numberSet3));
}
public static int calcularPrimero (int[] A) {
//IntStream intStream = Arrays.stream(A).filter(x -> x >= 0).distinct().sorted();
int[] B = Arrays.stream(A).filter(x -> x > 0).distinct().sorted().toArray();
for (int i = 0, index = 1; i < B.length; i++, index++) {
if (index != B[i]) {
return index;
}
}
return B.length + 1;
}
}

How do I shift elements in an array to the left by 1 if there is a zero?

Ok so I have to go through an array and if there is a zero in the array then I have to shift the elements right of the zero to the left by 1.
For example, given:
[3,0,1,2]
after a call to the method I should get:
[3,1,2,0]
This is what I have so far;
public int[] shiftArray(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] == 0) {
arr[i] = arr[i + 1];
}
arr[arr.length - 1] = 0;
}
return null;
}
I'm stuck on how to shift all the elements right of zero to the left by 1 and not just the one right next to the zero.
public int[] shiftArray(int[] arr)
{
int shift = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i]==0) {
shift++; // if you only want to handle the 1st zero, use shift = 1
} else if (shift>0) {
arr[i-shift] = arr[i];
}
}
for (int i = arr.length - shift; i < arr.length; i++) {
arr[i] = 0;
}
return null;
}
The main thing your algorithm/procedure should focus on is this:
If the value in an index is zero and it isn't the last index, perform two operations:
Move the value of the index to the right
Move the number previously at the right to the left.
The steps above are very important. I see that you've neglected step 2 in your question.
Here's how I'd handle it:
public int[] shiftArray(int[] arr){
for(int i = 0; i < arr.length - 1; i ++) {
if((arr[i] == 0) && (i != (arr.length - 1))){
int prev = arr[i];
int next = arr[i + 1];
arr[i] = next;
arr[i + 1] = prev;
}
}
return arr;
}
I hope this helps.. Merry coding!
Here are two ways to do it, one with loops and the other with streams
public static int[] ZeroRight(int[] arr) {
int[] temp = new int[arr.length];
int leftIndex = 0;
int rightIndex = arr.length - 1;
for (int i : arr) {
if (i == 0) {
temp[rightIndex] = i;
rightIndex--;
} else {
temp[leftIndex] = i;
leftIndex++;
}
}
return temp;
}
public static int[] ZeroRightStream(int[] arr) {
return Arrays.stream(arr)
.boxed()
.sorted((a, b) -> b == 0 ? -1 : 0)
.mapToInt(i -> i)
.toArray();
}
You can use Arrays.stream method to iterate over the elements of this array, filter out unnecessary elements and reassemble the array of the same length as follows:
int[] arr = {3, 0, 1, 0, 2};
int length = arr.length;
System.out.println(Arrays.toString(arr)); // [3, 0, 1, 0, 2]
arr = Arrays.stream(
Arrays.stream(arr)
.filter(i -> i != 0)
.boxed()
.toArray(q -> new Integer[length]))
.mapToInt(i -> i == null ? 0 : i)
.toArray();
System.out.println(Arrays.toString(arr)); // [3, 1, 2, 0, 0]
This approach is best used for an array of objects rather than an array of primitives.See: How can you reduce the indices of elements in a string array after deleting an element from it?

Java - Rotating array

So the goal is to rotate the elements in an array right a times.
As an example; if a==2, then array = {0,1,2,3,4} would become array = {3,4,0,1,2}
Here's what I have:
for (int x = 0; x <= array.length-1; x++){
array[x+a] = array[x];
}
However, this fails to account for when [x+a] is greater than the length of the array. I read that I should store the ones that are greater in a different Array but seeing as a is variable I'm not sure that's the best solution.
Thanks in advance.
Add a modulo array length to your code:
// create a newArray before of the same size as array
// copy
for(int x = 0; x <= array.length-1; x++){
newArray[(x+a) % array.length ] = array[x];
}
You should also create a new Array to copy to, so you do not overwrite values, that you'll need later on.
In case you don't want to reinvent the wheel (maybe it's an exercise but it can be good to know), you can use Collections.rotate.
Be aware that it requires an array of objects, not primitive data type (otherwise you'll swap arrays themselves in the list).
Integer[] arr = {0,1,2,3,4};
Collections.rotate(Arrays.asList(arr), 2);
System.out.println(Arrays.toString(arr)); //[3, 4, 0, 1, 2]
Arraycopy is an expensive operation, both time and memory wise.
Following would be an efficient way to rotate array without using extra space (unlike the accepted answer where a new array is created of the same size).
public void rotate(int[] nums, int k) { // k = 2
k %= nums.length;
// {0,1,2,3,4}
reverse(nums, 0, nums.length - 1); // Reverse the whole Array
// {4,3,2,1,0}
reverse(nums, 0, k - 1); // Reverse first part (4,3 -> 3,4)
// {3,4,2,1,0}
reverse(nums, k, nums.length - 1); //Reverse second part (2,1,0 -> 0,1,2)
// {3,4,0,1,2}
}
public void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
Another way is copying with System.arraycopy.
int[] temp = new int[array.length];
System.arraycopy(array, 0, temp, a, array.length - a);
System.arraycopy(array, array.length-a, temp, 0, a);
I think the fastest way would be using System.arrayCopy() which is native method:
int[] tmp = new int[a];
System.arraycopy(array, array.length - a, tmp, 0, a);
System.arraycopy(array, 0, array, a, array.length - a);
System.arraycopy(tmp, 0, array, 0, a);
It also reuses existing array. It may be beneficial in some cases.
And the last benefit is the temporary array size is less than original array. So you can reduce memory usage when a is small.
Time Complexity = O(n)
Space Complexity = O(1)
The algorithm starts with the first element of the array (newValue) and places it at its position after the rotation (newIndex). The element that was at the newIndex becomes oldValue. After that, oldValue and newValue are swapped.
This procedure repeats length times.
The algorithm basically bounces around the array placing each element at its new position.
unsigned int computeIndex(unsigned int len, unsigned int oldIndex, unsigned int times) {
unsigned int rot = times % len;
unsigned int forward = len - rot;
// return (oldIndex + rot) % len; // rotating to the right
return (oldIndex + forward) % len; // rotating to the left
}
void fastArrayRotation(unsigned short *arr, unsigned int len, unsigned int rotation) {
unsigned int times = rotation % len, oldIndex, newIndex, length = len;
unsigned int setIndex = 0;
unsigned short newValue, oldValue, tmp;
if (times == 0) {
return;
}
while (length > 0) {
oldIndex = setIndex;
newValue = arr[oldIndex];
while (1) {
newIndex = computeIndex(len, oldIndex, times);
oldValue = arr[newIndex];
arr[newIndex] = newValue;
length--;
if (newIndex == setIndex) { // if the set has ended (loop detected)
break;
}
tmp = newValue;
newValue = oldValue;
oldValue = tmp;
oldIndex = newIndex;
}
setIndex++;
}
}
int[] rotate(int[] array, int r) {
final int[] out = new int[array.length];
for (int i = 0; i < array.length; i++) {
out[i] = (i < r - 1) ? array[(i + r) % array.length] : array[(i + r) % array.length];
}
return out;
}
The following rotate method will behave exactly the same as the rotate method from the Collections class used in combination with the subList method from the List interface, i.e. rotate (n, fromIndex, toIndex, dist) where n is an array of ints will give the same result as Collections.rotate (Arrays.asList (n).subList (fromIndex, toIndex), dist) where n is an array of Integers.
First create a swap method:
public static void swap (int[] n, int i, int j){
int tmp = n[i];
n[i] = n[j];
n[j] = tmp;
}
Then create the rotate method:
public static void rotate (int[] n, int fromIndex, int toIndex,
int dist){
if(fromIndex > toIndex)
throw new IllegalArgumentException ("fromIndex (" +
fromIndex + ") > toIndex (" + toIndex + ")");
if (fromIndex < toIndex){
int region = toIndex - fromIndex;
int index;
for (int i = 0; i < dist % region + ((dist < 0) ? region : 0);
i++){
index = toIndex - 1;
while (index > fromIndex)
swap (n, index, --index);
}
}
}
Java solution wrapped in a method:
public static int[] rotate(final int[] array, final int rIndex) {
if (array == null || array.length <= 1) {
return new int[0];
}
final int[] result = new int[array.length];
final int arrayLength = array.length;
for (int i = 0; i < arrayLength; i++) {
int nIndex = (i + rIndex) % arrayLength;
result[nIndex] = array[i];
}
return result;
}
For Left Rotate its very simple
Take the difference between length of the array and number of position to shift.
For Example
int k = 2;
int n = 5;
int diff = n - k;
int[] array = {1, 2, 3, 4, 5};
int[] result = new int[array.length];
System.arraycopy(array, 0, result, diff, k);
System.arraycopy(array, k, result, 0, diff);
// print the output
Question : https://www.hackerrank.com/challenges/ctci-array-left-rotation
Solution :
This is how I tried arrayLeftRotation method with complexity o(n)
looping once from k index to (length-1 )
2nd time for 0 to kth index
public static int[] arrayLeftRotation(int[] a, int n, int k) {
int[] resultArray = new int[n];
int arrayIndex = 0;
//first n-k indexes will be populated in this loop
for(int i = k ; i
resultArray[arrayIndex] = a[i];
arrayIndex++;
}
// 2nd k indexes will be populated in this loop
for(int j=arrayIndex ; j<(arrayIndex+k); j++){
resultArray[j]=a[j-(n-k)];
}
return resultArray;
}
package com.array.orderstatistics;
import java.util.Scanner;
public class ArrayRotation {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int r = scan.nextInt();
int[] a = new int[n];
int[] b = new int[n];
for (int i = 0; i < n; i++) {
a[i] = scan.nextInt();
}
scan.close();
if (r % n == 0) {
printOriginalArray(a);
} else {
r = r % n;
for (int i = 0; i < n; i++) {
b[i] = a[(i + r) < n ? (i + r) : ((i + r) - n)];
System.out.print(b[i] + " ");
}
}
}
private static void printOriginalArray(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
Following routine rotates an array in java:
public static int[] rotateArray(int[] array, int k){
int to_move = k % array.length;
if(to_move == 0)
return array;
for(int i=0; i< to_move; i++){
int temp = array[array.length-1];
int j=array.length-1;
while(j > 0){
array[j] = array[--j];
}
array[0] = temp;
}
return array;
}
You can do something like below
class Solution {
public void rotate(int[] nums, int k) {
if (k==0) return;
if (nums == null || nums.length == 0) return;
for(int i=0;i<k;i++){
int j=nums.length-1;
int temp = nums[j];
for(;j>0;j--){
nums[j] = nums[j-1];
}
nums[0] = temp;
}
}
}
In the above solution, k is the number of times you want your array to rotate from left to right.
Question : Rotate array given a specific distance .
Method 1 :
Turn the int array to ArrayList. Then use Collections.rotate(list,distance).
class test1 {
public static void main(String[] args) {
int[] a = { 1, 2, 3, 4, 5, 6 };
List<Integer> list = Arrays.stream(a).boxed().collect(Collectors.toList());
Collections.rotate(list, 3);
System.out.println(list);//[4, 5, 6, 1, 2, 3]
}// main
}
I use this, just loop it a times
public void rotate(int[] arr) {
int temp = arr[arr.length - 1];
for(int i = arr.length - 1; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = temp;
}

How do you perform a left shift on a circular array of ints?

Is there an existing method that performs a left shift on a circular array of ints?
Specifically, given an array with 4 items {1,2,3,4} and a shift amount of 2, I would like a method that shifts the first two letters to the back of the array, making it appear like so: {3,4,1,2}.
Would this algorithm work to shift a circular array by one?
algShiftByOne(Array)
{
temp=array[0];
i=1
while(i < Array.length - 1) // Loop from 1 up to array.length == last index
{
// If there is no exception i assume it copies value from
// initial array starting from 1 up to array.length
Array[i - 1] = Array[i];
i++;
}
Array[Array.length]=temp;
}
Here is my go at it... (here is an ideone.com demo)
import java.util.Arrays;
public class Test {
public static void circularShiftLeft(int[] arr) {
if (arr.length == 0)
return;
int first = arr[0];
System.arraycopy(arr, 1, arr, 0, arr.length - 1);
arr[arr.length - 1] = first;
}
public static void main(String[] arg) {
int[] arr = { 1, 2, 3, 4 };
System.out.println(Arrays.toString(arr));
circularShiftLeft(arr);
System.out.println(Arrays.toString(arr));
}
}
I had this one as an interview question. A simple in place (and somewhat intuitive) O(2n) solution for rotating m is to take the array, reverse it, then reverse the [0, m] and (m, n] subarrays. My solution, though a little less obvious, is inplace and O(n). Basically the idea is you rotate items forward one at a item, and eventually you will pass through all the elements. The catch is if the array is a multiple of the distance, which is where the GCD comes in. The following will do a rotate right, rotate left is left to the reader as an exercise:
public static void main(String[] args) {
int[] f = {0, 4, 8, 2, 6, 7, 4, 5, 3};
System.out.println(Arrays.toString(f));
rotate(f, 3);
System.out.println(Arrays.toString(f));
}
public static void rotate(int[] arr, int dist){
int tmp, tmp2, gcd = GCD(arr.length, dist);
for(int off=0;off<gcd;off++){
tmp = arr[off];
for(int i=0,idx=off;i<arr.length/gcd;idx=(idx+dist)%arr.length,i++){
tmp2 = arr[(idx+dist)%arr.length];
arr[(idx+dist)%arr.length] = tmp;
tmp = tmp2;
}
}
}
public static int GCD(int a, int b) {
if (b==0) return a;
return GCD(b,a%b);
}
Assuming that you want to shift by n:
Copy the first n elements in an array named , for example, tempNumbers
For each element from n to the last one, shift it to the left by n
Copy the elements from tempNumbers to the end of the original array
Why don't you use a circular (doubly) linked list? In that case you only have to change your 'start pointer'.
Here is some pseudo-code to do what you want.
Array shift(Array a, int shiftLength) {
Array b;
for(i = shiftLength; i < a.size(); i++)
b.add(a.at(i));
for(i = 0; i < shiftLength; i++)
b.add(a.at(i));
return b;
}
This would shift the array a one to the left.
int[] a = new int[] { 1, 2, 3, 4, 5 };
int[] b = new int[a.length];
System.arraycopy(a, 1, b, 0, a.length - 1);
b[a.length - 1] = a[0];
// b = {2,3,4,5,1}
// edit
a = b;
public static void shift(int[] arr, int offs) {
// e.g. arr = 1,2,3,4,5,6,7,8,9; offs = 3
offs %= arr.length;
offs = offs < 0 ? arr.length + offs : offs;
if (offs > 0) {
// reverse whole array (arr = 9,8,7,6,5,4,3,2,1)
for (int i = 0, j = arr.length - 1; i < j; i++, j--)
swap(arr, i, j);
// reverse left part (arr = 7,8,9,6,5,4,3,2,1)
for (int i = 0, j = offs - 1; i < j; i++, j--)
swap(arr, i, j);
// reverse right part (arr = 7,8,9,1,2,3,4,5,6)
for (int i = offs, j = arr.length - 1; i < j; i++, j--)
swap(arr, i, j);
}
}
private static void swap(int[] arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}

Categories