📌  相关文章
📜  构造一个长度为 N 的数组,其中恰好包含可被其位置整除的 K 个元素

📅  最后修改于: 2022-05-13 01:56:07.432000             🧑  作者: Mango

构造一个长度为 N 的数组,其中恰好包含可被其位置整除的 K 个元素

给定两个整数NK,任务是构造一个长度为N的数组,其中正好包含可被其位置整除的K个元素。

例子:

方法:可以使用基于以下观察的贪婪方法轻松解决问题:

插图:

请按照以下步骤实施上述方法:

  • 创建一个包含从1 到 NN个连续正整数的数组。
  • 在索引NK之后,会剩下K-1 个元素,我们不会干扰这些元素。所以,我们有K-1 个元素,它们可以被它们的位置整除。
  • 我们将使数组的第一个元素等于索引NK处的元素。这也可以被它的位置整除。
  • 我们将使剩余的元素(即从索引1 到 NK )等于紧邻它们的元素。这些所有NK元素将不能被它们的位置整除,剩余的K 个元素将被它们的位置整除。

下面是上述方法的实现:

C++
// C++ program for above approach
#include 
using namespace std;
 
// Function to construct an array of size
// N such that it contains all numbers from
// 1 to N and only K elements are divisible by
// their position (i.e. index+1)
vector constructArray(int N, int K)
{
    // Declaring array of size N
    vector A(N, 0);
 
    // Initializing array as {1, 2, 3....N}
    for (int i = 0; i < N; i++) {
        A[i] = i + 1;
    }
 
    // N-K index stored in a variable "target"
    // After target there will be k-1 elements
    // which are divisible by their position
    int target = N - K;
 
    // Initializing "prev" variable that helps in
    // shifting elements to their right
    int prev = A[0];
 
    // Assigning first element the value at target
    // index
    A[0] = A[target];
 
    // Making all elements from index 1 to target
    // equal to their immediate left element
    // as any number would not be divisible
    // by its next number
    for (int i = 1; i <= target; i++) {
        int temp = A[i];
        A[i] = prev;
        prev = temp;
    }
 
    return A;
}
 
// Driver Code
int main()
{
    int N = 6, K = 2;
 
    // Calling function
    // to construct the array
    vector A = constructArray(N, K);
 
    // Printing resultant array
    for (int i = 0; i < N; i++)
        cout << A[i] << " ";
    cout << endl;
}


Java
// JAVA program for above approach
import java.util.*;
class GFG
{
   
    // Function to construct an array of size
    // N such that it contains all numbers from
    // 1 to N and only K elements are divisible by
    // their position (i.e. index+1)
    public static int[] constructArray(int N, int K)
    {
       
        // Declaring array of size N
        int A[] = new int[N];
        for (int i = 0; i < A.length; ++i) {
            A[i] = 0;
        }
 
        // Initializing array as {1, 2, 3....N}
        for (int i = 0; i < N; i++) {
            A[i] = i + 1;
        }
 
        // N-K index stored in a variable "target"
        // After target there will be k-1 elements
        // which are divisible by their position
        int target = N - K;
 
        // Initializing "prev" variable that helps in
        // shifting elements to their right
        int prev = A[0];
 
        // Assigning first element the value at target
        // index
        A[0] = A[target];
 
        // Making all elements from index 1 to target
        // equal to their immediate left element
        // as any number would not be divisible
        // by its next number
        for (int i = 1; i <= target; i++) {
            int temp = A[i];
            A[i] = prev;
            prev = temp;
        }
 
        return A;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 6, K = 2;
 
        // Calling function
        // to construct the array
        int A[] = constructArray(N, K);
 
        // Printing resultant array
        for (int i = 0; i < N; i++)
            System.out.print(A[i] + " ");
        System.out.println();
    }
}
 
// This code is contributed by Taranpreet


Python3
# Python program for above approach
 
# Function to construct an array of size
# N such that it contains all numbers from
# 1 to N and only K elements are divisible by
# their position (i.e. index+1)
def constructArray(N, K):
   
    # Declaring array of size N
    A = [0]*N
 
    # Initializing array as {1, 2, 3....N}
    for i in range(N):
      A[i] = i + 1
 
    # N-K index stored in a variable "target"
    # After target there will be k-1 elements
    # which are divisible by their position
    target = N - K
 
    # Initializing "prev" variable that helps in
    # shifting elements to their right
    prev = A[0]
 
    # Assigning first element the value at target
    # index
    A[0] = A[target]
 
    # Making all elements from index 1 to target
    # equal to their immediate left element
    # as any number would not be divisible
    # by its next number
    for i in range(1,target+1):
        temp = A[i]
        A[i] = prev
        prev = temp
 
    return A
 
# Driver Code
N = 6
K = 2
 
# Calling function
# to construct the array
A = constructArray(N, K)
 
# Printing resultant array
for i in range(N):
   print(A[i],end=" ")
 
# This code is contributed by shinjanpatra


C#
// C# program for above approach
using System;
class GFG {
 
  // Function to construct an array of size
  // N such that it contains all numbers from
  // 1 to N and only K elements are divisible by
  // their position (i.e. index+1)
  static int[] constructArray(int N, int K)
  {
 
    // Declaring array of size N
    int[] A = new int[N];
    for (int i = 0; i < A.Length; ++i) {
      A[i] = 0;
    }
 
    // Initializing array as {1, 2, 3....N}
    for (int i = 0; i < N; i++) {
      A[i] = i + 1;
    }
 
    // N-K index stored in a variable "target"
    // After target there will be k-1 elements
    // which are divisible by their position
    int target = N - K;
 
    // Initializing "prev" variable that helps in
    // shifting elements to their right
    int prev = A[0];
 
    // Assigning first element the value at target
    // index
    A[0] = A[target];
 
    // Making all elements from index 1 to target
    // equal to their immediate left element
    // as any number would not be divisible
    // by its next number
    for (int i = 1; i <= target; i++) {
      int temp = A[i];
      A[i] = prev;
      prev = temp;
    }
 
    return A;
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 6, K = 2;
 
    // Calling function
    // to construct the array
    int[] A = constructArray(N, K);
 
    // Printing resultant array
    for (int i = 0; i < N; i++)
      Console.Write(A[i] + " ");
    Console.WriteLine();
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
5 1 2 3 4 6 

时间复杂度: O(N)
辅助空间: O(N)