📌  相关文章
📜  检查是否可以对距离为K的元素进行条件交换来对数组进行排序

📅  最后修改于: 2021-05-17 20:16:27             🧑  作者: Mango

给定n个元素的数组arr [] ,我们必须将索引i与另一个索引i + k交换任意次,并检查是否可以对给定的数组arr []进行排序。如果是,则打印“是”,否则打印“否”。
例子:

方法:
为了解决上述问题,我们必须从索引0开始采用元素,然后将K的倍数相加,即0、0 + k,0 +(2 * k),依此类推。从0到K-1的所有索引交换这些位置,并检查最终数组是否已排序。如果是,则返回“是”,否则返回“否”。
下面是上述方法的实现:

C++
// CPP implementation to Check if it is possible to sort an
// array with conditional swapping of elements at distance K
#include 
using namespace std;
 
// Function for finding if it possible
// to obtain sorted array or not
bool fun(int arr[], int n, int k)
{
    vector v;
 
    // Iterate over all elements until K
    for (int i = 0; i < k; i++) {
        // Store elements as multiples of K
        for (int j = i; j < n; j += k) {
            v.push_back(arr[j]);
        }
 
        // Sort the elements
        sort(v.begin(), v.end());
 
        int x = 0;
 
        // Put elements in their required position
        for (int j = i; j < n; j += k) {
            arr[j] = v[x];
            x++;
        }
 
        v.clear();
    }
 
    // Check if the array becomes sorted or not
    for (int i = 0; i < n - 1; i++) {
        if (arr[i] > arr[i + 1])
            return false;
    }
    return true;
}
 
// Driver code
int main()
{
    int arr[] = { 4, 2, 3, 7, 6 };
 
    int K = 2;
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    if (fun(arr, n, K))
        cout << "yes" << endl;
 
    else
        cout << "no" << endl;
 
    return 0;
}


Java
// Java implementation to check if it
// is possible to sort an array with
// conditional swapping of elements
// at distance K
import java.lang.*;
import java.io.*;
import java.util.*;
 
class GFG{
     
// Function for finding if it possible
// to obtain sorted array or not    
public static boolean fun(int[] arr, int n,
                                     int k)
{
    Vector v = new Vector();
 
    // Iterate over all elements until K
    for(int i = 0; i < k; i++)
    {
        
       // Store elements as multiples of K
       for(int j = i; j < n; j += k)
       {
          v.add(arr[j]);
       }
        
       // Sort the elements
       Collections.sort(v);
        
       int x = 0;
        
       // Put elements in their
       // required position
       for(int j = i; j < n; j += k)
       {
          arr[j] = v.get(x);
          x++;
       }
       v.clear();
    }
 
    // Check if the array becomes
    // sorted or not
    for(int i = 0; i < n - 1; i++)
    {
       if (arr[i] > arr[i + 1])
       {
           return false;
       }
    }
    return true;
}
 
// Driver code
public static void main (String args[])
{
    int[] arr = { 4, 2, 3, 7, 6 };
    int K = 2;
    int n = arr.length;
 
    if (fun(arr, n, K))
    {
        System.out.println("yes");
    }
    else
    {
        System.out.println("no");
    }
}
}
 
// This code is contributed by sayesha


Python3
# Python3 implementation to Check if it is possible to sort an
# array with conditional swapping of elements at distance K
 
# Function for finding if it possible
# to obtain sorted array or not
def fun(arr, n, k):
 
    v = []
 
    # Iterate over all elements until K
    for i in range(k):
         
        # Store elements as multiples of K
        for j in range(i, n, k):
            v.append(arr[j]);
 
        # Sort the elements
        v.sort();
 
        x = 0
 
        # Put elements in their required position
        for j in range(i, n, k):
            arr[j] = v[x];
            x += 1
 
        v = []
 
    # Check if the array becomes sorted or not
    for i in range(n - 1):
        if (arr[i] > arr[i + 1]):
            return False
    return True
 
# Driver code
arr= [ 4, 2, 3, 7, 6 ]
 
K = 2;
 
n = len(arr)
 
if (fun(arr, n, K)):
    print("yes")
else:
    print("no")
     
# This code is contributed by apurva raj


C#
// C# implementation to check if it
// is possible to sort an array with
// conditional swapping of elements
// at distance K
using System;
using System.Collections.Generic;
class GFG{
     
// Function for finding if it possible
// to obtain sorted array or not    
public static bool fun(int[] arr,
                       int n, int k)
{
    List v = new List();
 
    // Iterate over all elements until K
    for(int i = 0; i < k; i++)
    {      
       // Store elements as multiples of K
       for(int j = i; j < n; j += k)
       {
          v.Add(arr[j]);
       }
        
       // Sort the elements
       v.Sort();
        
       int x = 0;
        
       // Put elements in their
       // required position
       for(int j = i; j < n; j += k)
       {
          arr[j] = v[x];
          x++;
       }
       v.Clear();
    }
 
    // Check if the array becomes
    // sorted or not
    for(int i = 0; i < n - 1; i++)
    {
       if (arr[i] > arr[i + 1])
       {
           return false;
       }
    }
    return true;
}
 
// Driver code
public static void Main(String []args)
{
    int[] arr = {4, 2, 3, 7, 6};
    int K = 2;
    int n = arr.Length;
    if (fun(arr, n, K))
    {
        Console.WriteLine("yes");
    }
    else
    {
        Console.WriteLine("no");
    }
}
}
 
// This code is contributed by shikhasingrajput


输出:
no