Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8950727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:30:17+00:00 2026-06-15T13:30:17+00:00

How can I write a function shift(int* arr, int k) cyclically shift an array

  • 0

How can I write a function shift(int* arr, int k) cyclically shift an array by some integer k? I cannot say “malloc” memory from the heap.

For instance, using pseudocode, shift([1, 2, 3, 4], 2) returns [3, 4, 1, 2], and shift([3, 1, 5, 5], 103) returns [1, 5, 5, 3]. I have tried using modulus to achieve this effect, as shown in this Java program, in which I basically iterate through half of the array and swap values.

public static int* shift(int* arr, int k) {
  int half_array_len = arr.length / 2;
  for (int i = 0; i < half_array_len; ++i) {
    // Swap!
    int newIndex = (i + k) % arr.length;
    int temp = arr[i];
    arr[i] = arr[newIndex];
    arr[newIndex] = arr[i];
  }
  return arr;
}

However, I believe that this function only works for even-lengthed arrays.

How can I implement shift for arrays of any length? Answers can be in any language you want (Java, C++, Ook Ook!, etc.).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:30:18+00:00Added an answer on June 15, 2026 at 1:30 pm

    (Has been asked several times before.) Basically, three classic in-place algorithms dedicated to solving this problem are described and analyzed in Bentley’s “Programming Pearls” (Juggling, Reversal and Block Swap algorithms). These slides describe them in sufficient detail

    http://www.cs.bell-labs.com/cm/cs/pearls/s02b.pdf

    The simplest algorithm is the Reversal Algorithm. Simply reverse the entire array and then reverse each [n – k] and [k] block independently. Done. (From which end the [k] block is counted depends on the direction of the shift.)

    Of course, it makes sense to normalize the k first, i.e, do k = k % n to make sure k is less than n.

    P.S. In C++ the answer would be to use std::rotate, which does exactly what you want. But I doubt this is the answer you seek. Although you might want to take a look at some implementations of std::rotate (if only to discover that they typically use the Reversal algorithm 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering how can I write a function: 'a list*int -> 'a list*list that
I want to write a function which removes elements from an array of integers
I'm trying to make it so I can write a function which defines a
How can i write a function to update record in Microsoft Dynamic NAV using
How can I write a function that accepts a variable number of arguments? Is
How can I write a function which will return pi (π) to a given
My question is pretty simple: How can I write a function once and make
For the following script, how can I write a function that returns all of
So I know I can write my own HTML-encoding function like this: function getHTMLEncode(t)
Can I write a hash code function for the following comparer logic? Two instances

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.