Table of Contents
The method used in the following program is as follows
Output
Home Backend Development C++ Rearrange first N numbers so that they are K distance apart in C++

Rearrange first N numbers so that they are K distance apart in C++

Sep 11, 2023 pm 03:13 PM

在 C++ 中重新排列前 N 个数字,使它们处于 K 距离

Given integer variables, say N and K. The task is to first compute the permutations of N and then rearrange the permutations so that they are a distance K from each element.

Let's look at various input and output scenarios -

input- int n = 20, int k = 2

output

strong>− Rearrange the first N numbers so that they are at K distance: 3 4 1 2 7 8 5 6 11 12 9 10 15 16 13 14 19 20 17 18.

Explanation

Explanation strong>− We are given integer variables 'N' i.e. 20 and 'K' i.e. 2. Now we will calculate the permutations of 'N' i.e. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18. 19, 20 . Now, we The elements will be arranged in such a way that all elements are "k" away from each element.

Input− int n = 10, int k = 3

Input− int n = 10, int k = 3

Input p>

Output - Rearrange the first N numbers so that they are at K distance: impossible

Explanation - We give The integer variable 'N' is 10, and 'K' is 3. Now we will calculate the permutations of 'N' i.e. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. Now, we will arrange the elements in such a way that all elements are "k" distance away from each element, but that is not possible for the given input value.

The method used in the following program is as follows

  • Input an integer type element, namely 'N' and 'K'.

  • Call the function Rearrangement(int n, int k) by passing N and K as parameters to the function.

  • Internal function Rearrangement(int n, int k)

    • Declare an integer variable as temp and set it to n % (2 * k ).

    • Declare an integer array as ptr 1 of size n, that is, prt[n 1].

    • Check IF k = 0 and then start looping FOR from i to 1 until i is less than size and increase i by 1 and print i.

    • Check IF temp is not equal to 0, then print NOT POSSIBLE.

    • Start the FOR loop from i to 1 until i is less than

    • Start the FOR loop from i to 1 until i is less than n, and set i to i 2 * k. Inside the loop, start another loop FOR from j to 1 until j is less than k and increment j by 1. Inside the loop, the swa method is called by passing ptr[i j -1] and ptr[k i j - 1] as arguments.

    • Start a FOR loop from i to 1 until i is less than N and increment i by 1.

    • Print prt[i].

  • Print the result.

  • Example
    #include <bits/stdc++.h>
    using namespace std;
    void Rearrangement(int n, int k){
       int temp = n % (2 * k);
       int ptr[n + 1];
       if(k == 0){
          for(int i = 1; i <= n; i++){
             cout << i << " ";
          }
          return;
       }
       if(temp != 0){
          cout<<"Not Possible";
          return;
       }
       for(int i = 1; i <= n; i++){
          ptr[i] = i;
       }
       for(int i = 1; i <= n; i += 2 * k){
          for(int j = 1; j <= k; j++){
             swap(ptr[i + j - 1], ptr[k + i + j - 1]);
          }
       }
       for(int i = 1; i <= n; i++){
          cout << ptr[i] << " ";
       }
    }
    int main(){
       int n = 20;
       int k = 2;
       cout<<"Rearrangement of first N numbers to make them at K distance is: ";
       Rearrangement(n, k);
       return 0;
    }
    Copy after login

    Output

    If we run the above code it will generate the following output

    Rearrangement of first N numbers to make them at K distance is: 3 4 1 2 7 8 5 6 11 12 9 10 15 16 13 14 19 20 17 18
    Copy after login

    The above is the detailed content of Rearrange first N numbers so that they are K distance apart in C++. For more information, please follow other related articles on the PHP Chinese website!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot Article Tags

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    C language function format letter case conversion steps C language function format letter case conversion steps Mar 03, 2025 pm 05:53 PM

    C language function format letter case conversion steps

    What are the types of values ​​returned by c language functions? What determines the return value? What are the types of values ​​returned by c language functions? What determines the return value? Mar 03, 2025 pm 05:52 PM

    What are the types of values ​​returned by c language functions? What determines the return value?

    Gulc: C library built from scratch Gulc: C library built from scratch Mar 03, 2025 pm 05:46 PM

    Gulc: C library built from scratch

    What are the definitions and calling rules of c language functions and what are the What are the definitions and calling rules of c language functions and what are the Mar 03, 2025 pm 05:53 PM

    What are the definitions and calling rules of c language functions and what are the

    How does the C   Standard Template Library (STL) work? How does the C Standard Template Library (STL) work? Mar 12, 2025 pm 04:50 PM

    How does the C Standard Template Library (STL) work?

    Where is the return value of the c language function stored in memory? Where is the return value of the c language function stored in memory? Mar 03, 2025 pm 05:51 PM

    Where is the return value of the c language function stored in memory?

    distinct usage and phrase sharing distinct usage and phrase sharing Mar 03, 2025 pm 05:51 PM

    distinct usage and phrase sharing

    How do I use algorithms from the STL (sort, find, transform, etc.) efficiently? How do I use algorithms from the STL (sort, find, transform, etc.) efficiently? Mar 12, 2025 pm 04:52 PM

    How do I use algorithms from the STL (sort, find, transform, etc.) efficiently?

    See all articles