📌  相关文章
📜  通过删除 (arr[i] + arr[i + 1])th 个元素恰好 K 次来修改数组

📅  最后修改于: 2021-09-07 04:51:15             🧑  作者: Mango

给定一个由前N 个自然数组成的数组arr[] ,其中arr[i] = i (基于 1 的索引)和一个正整数K ,任务是打印删除每个(arr[i ]后得到的数组arr[] ] + arr[i + 1])在每个i操作中恰好是K次数组中的第 th 个元素。

例子:

方法:给定的问题可以通过删去每(ARR [I] + ARR第[i + 1])术语从阵列中的每个i操作执行给定操作恰好K次可以解决。请按照以下步骤解决问题:

  • 使用变量i在范围[0, K – 1] 上迭代,并执行以下步骤:
    • 初始化一个辅助数组B[]来存储每次删除操作后数组arr[]的元素。
    • 遍历给定的数组arr[] ,如果当前索引不能被值(arr[i] + arr[i + 1])整除,则将该元素插入数组B[] 中
    • 完成上述步骤后,将数组B[] 的所有元素插入到数组arr[] 中
  • 完成上述步骤后,打印数组arr[]作为结果数组。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to modify array by removing
// every K-th element from the array
vector removeEveryKth(vector l, int k)
{
 
    for (int i = 0; i < l.size(); i++)
    {
        // Check if current element
        // is the k-th element
        if (i % k == 0)
            l[i] = 0;
    }
   
    // Stores the elements after
    // removing every kth element
    vector arr;
    arr.push_back(0);
 
    for (int i = 1; i < l.size(); i++)
    {
       
        // Append the current element
        // if it is not k-th element
        if (l[i] != 0)
            arr.push_back(l[i]);
      }
   
    // Return the new array after
    // removing every k-th element
    return arr;
}
 
// Function to print the array
void printArray(vector l)
{
 
    // Traverse the array l[]
    for (int i = 1; i < l.size(); i++)
        cout << l[i] << " ";
      cout << endl;
}
 
// Function to print the array after
// performing the given operations
// exactly k times
void printSequence(int n, int k)
{
 
    // Store first N natural numbers
    vector l(n+1);
 
    for (int i = 0; i < n + 1; i++) l[i]=i;
    int x = 1;
 
    // Iterate over the range [0, k-1]
    for (int i = 0; i < k; i++)
    {
 
        // Store sums of the two
        // consecutive terms
        int p = l[x] + l[x + 1];
 
        // Remove every p-th
        // element from the array
        l = removeEveryKth(l, p);
 
        // Increment x by 1 for
        // the next iteration
        x += 1;
      }
 
    // Print the resultant array
    printArray(l);
}
 
// Driver Code
int main()
{
    // Given arrays
    int N = 8;
    int K = 2;
 
    //Function Call
    printSequence(N, K);
 
}
 
// This code is contributed by mohit kumar  29


Java
// java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
public class GFG {
 
    // Function to modify array by removing
    // every K-th element from the array
    static int[] removeEveryKth(int l[], int k)
    {
 
        for (int i = 0; i < l.length; i++) {
            // Check if current element
            // is the k-th element
            if (i % k == 0)
                l[i] = 0;
        }
 
        // Stores the elements after
        // removing every kth element
        ArrayList list = new ArrayList<>();
        list.add(0);
 
        for (int i = 1; i < l.length; i++) {
 
            // Append the current element
            // if it is not k-th element
            if (l[i] != 0)
                list.add(l[i]);
        }
 
        // Return the new array after
        // removing every k-th element
        return list.stream().mapToInt(i -> i).toArray();
    }
 
    // Function to print the array
    static void printArray(int l[])
    {
 
        // Traverse the array l[]
        for (int i = 1; i < l.length; i++)
            System.out.print(l[i] + " ");
        System.out.println();
    }
 
    // Function to print the array after
    // performing the given operations
    // exactly k times
    static void printSequence(int n, int k)
    {
 
        // Store first N natural numbers
        int l[] = new int[n + 1];
 
        for (int i = 0; i < n + 1; i++)
            l[i] = i;
        int x = 1;
 
        // Iterate over the range [0, k-1]
        for (int i = 0; i < k; i++) {
 
            // Store sums of the two
            // consecutive terms
            int p = l[x] + l[x + 1];
 
            // Remove every p-th
            // element from the array
            l = removeEveryKth(l, p);
 
            // Increment x by 1 for
            // the next iteration
            x += 1;
        }
 
        // Print the resultant array
        printArray(l);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Given arrays
        int N = 8;
        int K = 2;
 
        // Function Call
        printSequence(N, K);
    }
}
 
// This code is contributed by Kingash.


Python3
# Python approach for the above approach
 
# Function to modify array by removing
# every K-th element from the array
def removeEveryKth(l, k):
   
    for i in range(0, len(l)):
       
        # Check if current element
        # is the k-th element
        if i % k == 0:
            l[i] = 0
 
    # Stores the elements after
    # removing every kth element
    arr = [0]
     
    for i in range(1, len(l)):
         
        # Append the current element
        # if it is not k-th element
        if l[i] != 0:
            arr.append(l[i])
 
    # Return the new array after
    # removing every k-th element
    return arr
 
# Function to print the array
def printArray(l):
   
    # Traverse the array l[]
    for i in range(1, len(l)):
        print(l[i], end =" ")
 
    print()
 
# Function to print the array after
# performing the given operations
# exactly k times
def printSequence(n, k):
 
    # Store first N natural numbers
    l = [int(i) for i in range(0, n + 1)]
 
    x = 1
 
    # Iterate over the range [0, k-1]
    for i in range(0, k):
 
        # Store sums of the two
        # consecutive terms
        p = l[x] + l[x + 1]
 
        # Remove every p-th
        # element from the array
        l = removeEveryKth(l, p)
         
        # Increment x by 1 for
        # the next iteration
        x += 1
     
    # Print the resultant array
    printArray(l)
 
# Driver Code
N = 8
K = 2
 
# Function Call
printSequence(N, K)


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
 
    // Function to modify array by removing
    // every K-th element from the array
    static List removeEveryKth(List l, int k)
    {
 
        for (int i = 0; i < l.Count; i++) {
            // Check if current element
            // is the k-th element
            if (i % k == 0)
                l[i] = 0;
        }
 
        // Stores the elements after
        // removing every kth element
        List arr = new List();
        arr.Add(0);
 
        for (int i = 1; i < l.Count; i++) {
 
            // Append the current element
            // if it is not k-th element
            if (l[i] != 0)
                arr.Add(l[i]);
        }
 
        // Return the new array after
        // removing every k-th element
        return arr;
    }
 
    // Function to print the array
    static void printArray(List l)
    {
 
        // Traverse the array l[]
        for (int i = 1; i < l.Count; i++)
            Console.Write(l[i] + " ");
        Console.WriteLine();
    }
 
    // Function to print the array after
    // performing the given operations
    // exactly k times
    static void printSequence(int n, int k)
    {
 
        // Store first N natural numbers
        List l = new List();
 
        for (int i = 0; i < n + 1; i++)
            l.Add(i);
        int x = 1;
 
        // Iterate over the range [0, k-1]
 
        for (int i = 0; i < k; i++) {
 
            // Store sums of the two
            // consecutive terms
            int p = l[x] + l[x + 1];
 
            // Remove every p-th
            // element from the array
            l = removeEveryKth(l, p);
 
            // Increment x by 1 for
            // the next iteration
            x += 1;
        }
 
        // Print the resultant array
        printArray(l);
    }
 
    // Driver Code
    public static void Main()
    {
       
        // Given arrays
        int N = 8;
        int K = 2;
 
        // Function Call
        printSequence(N, K);
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
1 2 4 5 7

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

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