code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

Leftrotateanarraybydplaces

import java.util.*;
/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

class LeftRotateAnArrayByDPlaces {

  static int[] func(int[] nums, int place){
    int[] temp = new int[place];
    for(int i = 0; i < place; i++){
      temp[i] = nums[i];
    }
    for(int i = place; i < nums.length; i++){
      nums[i - place] = nums[i];
    }
    for(int i = 0; i < place; i++){
      nums[nums.length-place+i] = temp[i];
    }
    return nums;
  }

  public static void main(String[] args){
    int[] nums = { 10, 20, 30, 40, 50 };
    int place = 2;
    int[] result = func(nums, place);
    System.out.println(Arrays.toString(result));
  }
}

Created 2021-10-27T16:13:12+01:00 · Edit