Open In App

Sort elements of the array that occurs in between multiples of K

Last Updated : 25 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] and an integer K. The task is to sort the elements that are in between any two multiples of K.

Examples: 

Input: arr[] = {2, 1, 13, 3, 7, 8, 21, 13, 12}, K = 2 
Output: 2 1 3 7 13 8 13 21 12 
The multiples of 2 in the array are 2, 8 and 12. 
The elements that are in between the first two multiples of 2 are 1, 13, 3 and 7. 
Hence, these elements in sorted order are 1, 3, 7 and 13. 
Similarly, the elements between 8 and 12 in sorted order will be 13 and 21.

Input: arr[] = {11, 10, 9, 7, 4, 5, 12, 22, 13, 15, 17, 16}, K = 3 
Output: 11 10 9 4 5 7 12 13 22 15 17 16 

Approach: Traverse the array and keep track of the multiples of K, starting from the 2nd multiple of K sort every element between the current and the previous multiple of K. Print the updated array at the end.

Below is the implementation of the above approach: 

C++




// C++ implementation of the approach
#include<bits/stdc++.h>
using namespace std;
 
// Utility function to print
// the contents of an array
void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << (arr[i]) << " ";
}
 
// Function to sort elements
// in between multiples of k
void sortArr(int arr[], int n, int k)
{
 
    // To store the index of
    // previous multiple of k
    int prev = -1;
    for (int i = 0; i < n; i++)
    {
        if (arr[i] % k == 0)
        {
 
            // If it is not the
            // first multiple of k
            if (prev != -1)
 
                // Sort the elements in between
                // the previous and the current
                // multiple of k
                sort(arr + prev + 1, arr + i);
 
            // Update previous to be current
            prev = i;
        }
    }
 
    // Print the updated array
    printArr(arr, n);
}
 
// Driver code
int main()
{
    int arr[] = {2, 1, 13, 3, 7,
                 8, 21, 13, 12};
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 2;
    sortArr(arr, n, k);
}
 
// This code is contributed by
// Surendra_Gangwar


Java




// Java implementation of the approach
import java.util.Arrays;
class GFG {
 
    // Utility function to print
    // the contents of an array
    static void printArr(int arr[], int n)
    {
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Function to sort elements
    // in between multiples of k
    static void sortArr(int arr[], int n, int k)
    {
 
        // To store the index of
        // previous multiple of k
        int prev = -1;
        for (int i = 0; i < n; i++) {
            if (arr[i] % k == 0) {
 
                // If it is not the
                // first multiple of k
                if (prev != -1)
 
                    // Sort the elements in between
                    // the previous and the current
                    // multiple of k
                    Arrays.sort(arr, prev + 1, i);
 
                // Update previous to be current
                prev = i;
            }
        }
 
        // Print the updated array
        printArr(arr, n);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 2, 1, 13, 3, 7, 8, 21, 13, 12 };
        int n = arr.length;
        int k = 2;
        sortArr(arr, n, k);
    }
}


Python3




# Python3 implementation of the approach
 
# Utility function to print
# the contents of an array
def printArr(arr, n) :
    for i in range(n) :
        print(arr[i], end = " ");
 
# Function to sort elements
# in between multiples of k
def sortArr(arr, n, k) :
     
    # To store the index of
    # previous multiple of k
    prev = -1;
    for i in range(n) :
        if (arr[i] % k == 0) :
             
            # If it is not the first
            # multiple of k
            if (prev != -1) :
                 
                # Sort the elements in between
                #the previous and the current
                # multiple of k
                temp = arr[prev + 1:i];
                temp.sort();
                arr = arr[ : prev + 1] + temp + arr[i : ];
                 
            # Update previous to be current
            prev = i;
 
    # Print the updated array
    printArr(arr, n);
 
# Driver code
if __name__ == "__main__" :
     
    arr = [ 2, 1, 13, 3, 7, 8, 21, 13, 12 ];
    n = len(arr);
    k = 2;
     
    sortArr(arr, n, k);
 
# This code is contributed by Ryuga


C#




// C# implementation of the approach
using System.Collections;
using System;
class GFG {
 
    // Utility function to print
    // the contents of an array
    static void printArr(int []arr, int n)
    {
        for (int i = 0; i < n; i++)
            Console.Write(arr[i] + " ");
    }
 
    // Function to sort elements
    // in between multiples of k
    static void sortArr(int []arr, int n, int k)
    {
 
        // To store the index of
        // previous multiple of k
        int prev = -1;
        for (int i = 0; i < n; i++) {
            if (arr[i] % k == 0) {
 
                // If it is not the
                // first multiple of k
                if (prev != -1)
 
                    // Sort the elements in between
                    // the previous and the current
                    // multiple of k
                    Array.Sort(arr, prev + 1, i-(prev + 1));
 
                // Update previous to be current
                prev = i;
            }
        }
 
        // Print the updated array
        printArr(arr, n);
    }
 
    // Driver code
    public static void Main(String []args)
    {
        int []arr = { 2, 1, 13, 3, 7, 8, 21, 13, 12 };
        int n = arr.Length;
        int k = 2;
        sortArr(arr, n, k);
    }
}
//contributed by Arnab Kundu


Javascript




<script>
 
// Javascript implementation of the approach
 
// Utility function to print
// the contents of an array
function printArr(arr, n)
{
    for (var i = 0; i < n; i++)
        document.write(arr[i] + " ");
}
 
// Function to sort elements
// in between multiples of k
function sortArr(arr, n, k)
{
 
    // To store the index of
    // previous multiple of k
    var prev = -1;
    for (var i = 0; i < n; i++)
    {
        if (arr[i] % k == 0)
        {
 
            // If it is not the
            // first multiple of k
            if (prev != -1)
 
                var tmp = arr.slice(prev+1, i).sort((a,b)=> a-b);
                // Sort the elements in between
                // the previous and the current
                // multiple of k
                for(var j=prev+1; j< i; j++)
                {
                    arr[j] = tmp[j-prev-1];
                }
            // Update previous to be current
            prev = i;
        }
    }
 
    // Print the updated array
    printArr(arr, n);
}
 
// Driver code
var arr = [2, 1, 13, 3, 7,
             8, 21, 13, 12];
var n = arr.length;
var k = 2;
sortArr(arr, n, k);
 
 
</script>


Output: 

2 1 3 7 13 8 13 21 12

 

Time Complexity: O(n2*log(n))

Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads