Modify array by removing (arr[i] + arr[i + 1])th element exactly K times
Last Updated :
14 May, 2021
Given an array arr[] consisting of first N natural numbers, where arr[i] = i ( 1-based indexing ) and a positive integer K, the task is to print the array arr[] obtained after removing every (arr[i] + arr[i + 1])th element from the array in every ith operation exactly K times.
Examples:
Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8}, K = 2
Output: 1 2 4 5 7
Explanation:
Initially, the array arr[] is {1, 2, 3, 4, 5, 6, 7, 8}.
Operation 1: Delete every (A[1] + A[2])th element, i.e., every 3rd element from the array arr[]. Now the array modifies to {1, 2, 4, 5, 7, 8}.
Operation 2: Delete every (A[2] + A[3])th element, i.e., every 6th element from the array arr[]. Now the array modifies to {1, 2, 4, 5, 7}.
After performing the above operations, the array obtained is {1, 2, 4, 5, 7}.
Input: N = 10, K = 3
Output: 1 2 4 5 7 10
Approach: The given problem can be solved by performing the given operation exactly K times by deleting every (arr[i] + arr[i + 1])th term from the array in every ith operation. Follow the steps below to solve the problem:
- Iterate over the range [0, K - 1] using the variable i, and perform the following steps:
- Initialize an auxiliary array B[] to stores the elements of the array arr[] after each deletion operation.
- Traverse the given array arr[] and if the current index is not divisible by the value (arr[i] + arr[i + 1]) then insert that element in the array B[].
- After the above steps, insert all the elements of the array B[] in the array arr[].
- After completing the above steps, print the array arr[] as the resultant array.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to modify array by removing
// every K-th element from the array
vector<int> removeEveryKth(vector<int> 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<int> 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<int> 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<int> 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<Integer> 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<int> removeEveryKth(List<int> 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<int> arr = new List<int>();
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<int> 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<int> l = new List<int>();
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
<script>
// Javascript program for the above approach
// Function to modify array by removing
// every K-th element from the array
function removeEveryKth(l, k) {
for (let 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
let arr = [0];
for (let i = 1; i < l.length; i++) {
// Append the current element
// if it is not k-th element
if (l[i] != 0)
arr.push(l[i]);
}
// Return the new array after
// removing every k-th element
return arr;
}
// Function to print the array
function printArray(l) {
// Traverse the array l[]
for (let i = 1; i < l.length; i++)
document.write(l[i] + " ");
}
// Function to print the array after
// performing the given operations
// exactly k times
function printSequence(n, k) {
// Store first N natural numbers
let l = [0];
for (let i = 0; i < n + 1; i++) l[i] = i;
let x = 1;
// Iterate over the range [0, k-1]
for (let i = 0; i < k; i++) {
// Store sums of the two
// consecutive terms
let 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
// Given arrays
let N = 8;
let K = 2;
//Function Call
printSequence(N, K);
// This code is contributed by Hritik
</script>
Time Complexity: O(N*K)
Auxiliary Space: O(N)
Similar Reads
Minimize the sum of MEX by removing all elements of array Given an array of integers arr[] of size N. You can perform the following operation N times: Pick any index i, and remove arr[i] from the array and add MEX(arr[]) i.e., Minimum Excluded of the array arr[] to your total score. Your task is to minimize the total score. Examples: Input: N = 8, arr[] =
7 min read
Sum of all elements repeating 'k' times in an array Given an array, we have to find the sum of all the elements repeating k times in an array. We need to consider every repeating element just once in the sum. Examples: Input : arr[] = {2, 3, 9, 9} k = 1 Output : 5 2 + 3 = 5 Input : arr[] = {9, 8, 8, 8, 10, 4} k = 3 Output : 8 One simple solution is t
8 min read
Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space Given an array arr[] of size n where every element is in the range from 0 to n-1. Rearrange the given array so that arr[i] becomes arr[arr[i]]. This should be done with O(1) extra spaceExamples: Input: arr[] = [3, 2, 0, 1]Output: arr[] = [1, 0, 3, 2]Explanation: arr[arr[0]] is 1 so arr[0] in output
5 min read
Min steps to empty an Array by removing a pair each time with sum at most K Given an array arr[] and a target value K. The task is to find the minimum number of steps required to take all elements from the array. In each step, at most two elements can be selected from array such that their sum must not exceed target value K. Note: All the elements of the array are less than
6 min read
Minimum increments to make all array elements equal with sum same as the given array after exactly one removal Given an array arr[] of size N and an integer K, the task is to check if all array elements can be made equal by removing an array element and incrementing the value of all other array elements such that the total sum of the array elements remains the same. If found to be true, then print "YES". Oth
7 min read
Remove an occurrence of most frequent array element exactly K times Given an array arr[], the task is to remove an occurrence of the most frequent array element exactly K times. If multiple array elements have maximum frequency, remove the smallest of them. Print the K deleted elements. Examples: Input: arr[] = {1, 3, 2, 1, 4, 1}, K = 2Output: 1 1Explanation: The fr
12 min read
Remove elements from the array which appear more than k times Given an array of integers, remove all the occurrences of those elements which appear strictly more than k times in the array.Examples: Input : arr[] = {1, 2, 2, 3, 2, 3, 4} k = 2Output : 1 3 3 4Input : arr[] = {2, 5, 5, 7} k = 1Output : 2 7Approach: Take a hash map, which will store the frequency o
8 min read
Last element of an array after repeatedly removing the first element and appending it to the end of the array twice exactly K times Given an array arr[] consisting of N integers and a positive integer K, the task is to find the last element present in the array obtained by repeatedly removing the first element of the array and appending it twice to the end of the array K times. Examples: Input: arr[] = {1, 2, 3, 4, 5}, K = 5Outp
7 min read
Find final Array after subtracting maximum from all elements K times Given an array arr[] of N integers and an integer K, the task is to do the below operations with this array K times. Operations are as follows: Find the maximum element (say M) from the array.Now replace every element with M-arri. where 1 ⤠i ⤠N. Examples: Input: N = 6, K = 3, arr[] = {5, 38, 4, 96
9 min read
Reduce the array such that each element appears at most K times Given a sorted array arr of size N, the task is to reduce the array such that each element can appear at most K times.Examples: Input: arr[] = {1, 2, 2, 2, 3}, K = 2 Output: {1, 2, 2, 3} Explanation: Remove 2 once, as it occurs more than 2 times.Input: arr[] = {3, 3, 3}, K = 1 Output: {3} Explanatio
10 min read