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

📅  最后修改于: 2021-09-04 11:39:55             🧑  作者: 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


Javascript


输出:
2 1 2 1

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live