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 591303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:37:14+00:00 2026-05-13T15:37:14+00:00

Good evening, people! I’m trying to solve a rather simple problem, but.. well, it

  • 0

Good evening, people!

I’m trying to solve a rather simple problem, but.. well, it seems that I can’t. 🙂

The idea is that I have a FIFO list (FIFO queue) with n elements and it’s given a value, k (k < n). My little program has to move the elements to the left with k elements. (e.g. for n=4, k=3, a[]=(1, 2, 3, 4), the result is 4 1 2 3).

But well, I get nowhere near that.

This is what I’ve written so far:

#include <iostream>
using namespace std;

void move (int a[100], unsigned n, unsigned k) {
        int t[100];
        unsigned i;
        for (i=0; i<=n-1; i++) t[i]=a[i];
        for (i=0; i<=k-1; i++) a[i]=a[i+k-1];
        for (i=k; i<=n-1; i++) a[i]=t[i+1];
}

int main () {
        int a[100];
        unsigned k, n, i;
        cout<<"n; k= "; cin>>n>>k;
        for (i=0; i<=n-1; i++) cin>>a[i];
        move (a, n, k);
        for (i=0; i<=n-1; i++) cout<<a[i]<<" ";
}

Any help would be greatly appreciated. Thank you in advance.

  • 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-05-13T15:37:15+00:00Added an answer on May 13, 2026 at 3:37 pm

    I’m not sure if I’ve understood your question completely. But looks like you effectively want to rotate the contents of the array.

    To rotate the array contents to the left k times. You can do the following:

    • Reverse the first K elements.
    • Reverse the remaining N-K elements.
    • Reverse the entire array.

    Example:

    N = 5, K = 3, and array = [1 2 3 4 5]

    • step 1: reverse the first 3 elements:
      [3 2 1 4 5]
    • step 2: reverse the remaining 2
      elements: [3 2 1 5 4]
    • step 3: reverse the entire array: [4
      5 1 2 3]

    C++ function to do the same:

    void move (int a[100], int n, int k) {
            int t[100];
            int i,j;
            for (i=k-1,j=0; i>=0; i--,j++) t[j]=a[i];
            for (i=n-1; i>=k; i--,j++) t[j]=a[i];
            for (i=n-1,j=0; i>=0; i--,j++) a[j]=t[i];
    }
    

    A better way to do it in constant space is to do the reversal in-place:

    void arr_rev(int a[100], int start, int end) {
            int temp;
    
            for(;start<end;start++,end--) {
                    temp = a[start];
                    a[start] = a[end];
                    a[end] = temp;
            }
    }
    
    void move2 (int a[100], int n, int k) {
            arr_rev(a,0,k-1);
            arr_rev(a,k,n-1);
            arr_rev(a,0,n-1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good Evening, I have been trying to implement a simple Android(tm) application that utilizes
Good evening, i need to know how can i have one .h file that
Good evening. I'm developing an android application that uses Facebook SDK. I've no problem
Good evening all, I'm trying to write a method that creates and returns a
Good Evening, The problem is that i have both xcode 3.2 and xcode 4
Good evening all, I'm trying to implement jquery table sorter, all seems fine and
Good evening guys. I'm currently trying to get started on development of a project
Good evening, In my app that I'm currently developing, I have a class that
Good evening all, I've been working on an MD5 tool in C# that takes
Good evening everyone. I am puzzling over the oddity that the jQuery Marquee plugin

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.