📌  相关文章
📜  仅插入元素即可使长度为K的所有子数组的总和相等

📅  最后修改于: 2021-05-14 01:49:59             🧑  作者: Mango

给定长度N的数组arr []使得(1 <= arr [i] <= N),任务是通过仅插入范围[1,N]内的元素来修改数组,使得长度为K的所有子数组都相等。如果可能,打印修改后的数组。否则打印“不可能”。
例子:

方法:

  • 由于不允许删除元素,因此,只有当数组中最多有K个不同的元素时,才能实现这种修改后的数组。
  • 因此,首先检查给定数组中是否有超过K个不同的元素。如果是,则打印“不可能”。
  • 否则,将给定数组的所有不同元素存储在向量中。
  • 如果不同元素的数量小于K,则在向量中添加/重复一些元素,并使向量的大小等于K。
  • 现在,向量具有K个元素。现在以相同的顺序重复向量的元素,以显示修改后的数组。进行此重复以显示所有长度为K的子数组具有相同的总和。

下面是上述方法的实现

C++
// C++ implementation of above approach
  
#include 
using namespace std;
  
// Function that prints another array
// whose all subarray of length k
// have an equal sum
void MakeArray(int a[], int n, int k)
{
    unordered_map mp;
  
    // Store all distinct elements in
    // the unordered map
    for (int i = 0; i < n; i++) {
        if (mp.find(a[i]) == mp.end())
            mp[a[i]] = 1;
    }
  
    // Condition if the number of
    // distinct elements is greater
    // than k
    if (mp.size() > k) {
        cout << "Not possible\n";
        return;
    }
  
    vector ans;
  
    // Push all distinct elements
    // in a vector
    for (auto i : mp) {
        ans.push_back(i.first);
    }
  
    // Push 1 while the size of
    // vector not equal to k
    while (ans.size() < k) {
        ans.push_back(1);
    }
  
    // Print the vector 2 times
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < k; j++)
            cout << ans[j] << " ";
    }
}
  
// Driver Code
int main()
{
    int arr[] = { 1, 2, 2, 1 };
    int K = 2;
    int size = sizeof(arr) / sizeof(arr[0]);
  
    MakeArray(arr, size, K);
  
    return 0;
}


Java
// Java implementation of above approach
import java.util.*;
class GFG{
    
// Function that prints another array
// whose all subarray of length k
// have an equal sum
static void MakeArray(int a[], int n, int k)
{
    HashMap mp = new HashMap();
    
    // Store all distinct elements in
    // the unordered map
    for (int i = 0; i < n; i++)
    {
        if (!mp.containsKey(a[i]))
            mp.put(a[i], 1);
    }
    
    // Condition if the number of
    // distinct elements is greater
    // than k
    if (mp.size() > k) 
    {
        System.out.print("Not possible\n");
        return;
    }
    
    Vector ans = new Vector();
    
    // Push all distinct elements
    // in a vector
    for (Map.Entry i : mp.entrySet()) 
    {
        ans.add(i.getKey());
    }
    
    // Push 1 while the size of
    // vector not equal to k
    while (ans.size() < k) 
    {
        ans.add(1);
    }
    
    // Print the vector 2 times
    for (int i = 0; i < 2; i++) 
    {
        for (int j = 0; j < k; j++)
            System.out.print(ans.get(j) + " ");
    }
}
    
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 2, 1 };
    int K = 2;
    int size = arr.length;
    
    MakeArray(arr, size, K);
}
}
   
// This code is contributed by Rohit_ranjan


Python3
# Python3 implementation of above approach
  
# Function that prints another array
# whose all subarray of length k
# have an equal sum
def MakeArray(a, n, k):
    mp = dict()
  
    # Store all distinct elements in
    # the unordered map
    for i in a:
        if i not in mp:
            mp[a[i]] = 1
  
    # Condition if the number of
    # distinct elements is greater
    # than k
    if(len(mp) > k):
       print("Not possible")
       return
  
    ans = []
  
    # Push all distinct elements
    # in a vector
    for i in mp:
        ans.append(i)
  
    # Push 1 while the size of
    # vector not equal to k
    while(len(ans) < k):
          ans.append(1)
  
    # Print the vector 2 times
    for i in range(2):
        for j in range(k):
            print(ans[j], end = " ")
  
# Driver Code
if __name__ == '__main__':
      
    arr = [ 1, 2, 2, 1 ]
    K = 2
    size = len(arr)
      
    MakeArray(arr, size, K)
      
# This code is contributed by mohit kumar 29


C#
// C# implementation of above approach
using System;
using System.Collections.Generic;
  
class GFG{
  
// Function that prints another array
// whose all subarray of length k
// have an equal sum
static void MakeArray(int []a, int n, int k)
{
    Dictionary mp = new Dictionary();
  
    // Store all distinct elements in
    // the unordered map
    for(int i = 0; i < n; i++)
    {
        if (!mp.ContainsKey(a[i]))
             mp.Add(a[i], 1);
    }
  
    // Condition if the number of
    // distinct elements is greater
    // than k
    if (mp.Count > k) 
    {
        Console.Write("Not possible\n");
        return;
    }
  
    List ans = new List();
  
    // Push all distinct elements
    // in a vector
    foreach(KeyValuePair i in mp) 
    {
        ans.Add(i.Key);
    }
  
    // Push 1 while the size of
    // vector not equal to k
    while (ans.Count < k) 
    {
        ans.Add(1);
    }
  
    // Print the vector 2 times
    for(int i = 0; i < 2; i++) 
    {
        for(int j = 0; j < k; j++)
            Console.Write(ans[j] + " ");
    }
}
  
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 2, 2, 1 };
    int K = 2;
    int size = arr.Length;
  
    MakeArray(arr, size, K);
}
}
  
// This code is contributed by amal kumar choubey


输出:
2 1 2 1