Remove elements for Alternating even index values in Array Last Updated : 10 Apr, 2023 Comments Improve Suggest changes Like Article Like Report Given an array arr[], the task is to find the minimum number of elements to be removed such that every element at an even index in the array is different from the next element and return the array after removing. Examples: Input: arr[] = {5, 6, 8, 8, 7, 7, 5}Output: {5, 6, 8, 7, 7, 5}Explanation: arr2 element and arr3 element were equal. so deleting one of them Input: A[] = {2, 2, 2}Output: 2Explanation: arr2 is equal to arr1 so removing arr2 now arr3 becomes arr2 again equal to arr1 hence removing it as well. Approach: This can be solved with the following idea: The main idea is to add elements in an array and check if the current element we are adding is getting placed at an odd index (next to an even indexed element) and if it is so then it should not be equal to the last element at even index else discard the element. Add elements at odd index in the new array simply without any conditions to be checked.Take an empty vector of Integer and then iterate the given array arr from starting and add the current element to the new vector if the size of the vector is even (because in that case, we are definitely adding an element which will be at odd index in the new vector as already even a number of elements are already there in the vector and we don't have conditions on elements at even index) Or if the current element is not equal to the last element in the vector (obviously there is no issue when the last element and the current element are not equal ). Steps involved in the implementation of the above approach: Take a vector<int> which will store the final resultIterate through the given array from the startingCheck if the size of the vector or if the current element is not equal to the last added element if so then, add the element to the vectorIterate through the vector and print the result. Below is the implementation of the code: C++ // C++ Implementation #include <bits/stdc++.h> using namespace std; // Function to find array after removing element void validate(int arr[], int n) { vector<int> vec; for (int i = 0; i < n; i++) { // Checking the size of vec or the current // element not equal to the last added element if ((vec.size() % 2 == 0) or (vec.back() != arr[i])) { vec.push_back(arr[i]); } } // Printing the result for (auto x : vec) { cout << x << " "; } return; } // Driver code int main() { int arr[] = { 1, 3, 2, 2, 5 }; int n = sizeof(arr) / sizeof(arr[0]); // Function call validate(arr, n); return 0; } Java /*package whatever // do not write package name here */ import java.io.*; import java.util.ArrayList; class GFG { static void validate(int arr[], int n) { ArrayList<Integer> vec = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { // checking the size of vec or the current element not equal to the last added element if ((vec.size() % 2 == 0) || (vec.get(vec.size() - 1) != arr[i])) { vec.add(arr[i]); } } // printing the result for (int i = 0; i < vec.size(); i++) { System.out.print(vec.get(i) + " "); } } public static void main(String[] args) { int arr[] = { 1, 3, 2, 2, 5 }; int n = arr.length; validate(arr, n); } } Python3 def validate(arr, n): vec = [] for i in range(n): # Checking the size of vec or the current # element not equal to the last added element if (len(vec) % 2 == 0) or (vec[-1] != arr[i]): vec.append(arr[i]) # Printing the result for x in vec: print(x, end=' ') return # Driver code arr = [1, 3, 2, 2, 5] n = len(arr) # Function call validate(arr, n) C# using System; using System.Collections.Generic; public class Program { // Function to find array after removing element public static void Validate(int[] arr, int n) { List<int> vec = new List<int>(); for (int i = 0; i < n; i++) { // Checking the size of vec or the current // element not equal to the last added element if ((vec.Count % 2 == 0) || (vec[vec.Count - 1] != arr[i])) { vec.Add(arr[i]); } } // Printing the result foreach(int x in vec) { Console.Write(x + " "); } } // Driver code public static void Main() { int[] arr = { 1, 3, 2, 2, 5 }; int n = arr.Length; // Function call Validate(arr, n); } } JavaScript // Function to find array after removing element function validate(arr) { let vec = []; for (let i = 0; i < arr.length; i++) { // Checking the size of vec or the current // element not equal to the last added element if (vec.length % 2 === 0 || vec[vec.length - 1] !== arr[i]) { vec.push(arr[i]); } } // Printing the result console.log(vec.join(" ")); } // Driver code const arr = [1, 3, 2, 2, 5]; // Function call validate(arr); //this code is contributed by Tushar Rokade Output1 3 2 5 Time Complexity: O(n)Auxiliary Space: O(n), since n extra space has been taken. Comment More infoAdvertise with us Next Article Remove elements for Alternating even index values in Array S srivastavasuyash17 Follow Improve Article Tags : Arrays DSA Practice Tags : Arrays Similar Reads Absolute Difference of even and odd indexed elements in an Array Given an array of integers arr, the task is to find the running absolute difference of elements at even and odd index positions separately. Note: 0-based indexing is considered for the array. That is the index of the first element in the array is zero. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6} Out 6 min read Rearrange array such that all even-indexed elements in the Array is even Given an array arr[], the task is to check if it is possible to rearrange the array in such a way that every even index(1-based indexing) contains an even number. If such a rearrangement is not possible, print "No". Otherwise, print "Yes" and print a possible arrangement Examples: Input: arr[] = {2, 6 min read Rearrange Odd and Even values in Alternate Fashion in Ascending Order Given an array of integers (both odd and even), the task is to arrange them in such a way that odd and even values come in alternate fashion in non-decreasing order(ascending) respectively. If the smallest value is Even then we have to print Even-Odd pattern.If the smallest value is Odd then we have 15+ min read Find even occurring elements in an array of limited range Given an array that contains odd number of occurrences for all numbers except for a few elements which are present even number of times. Find the elements which have even occurrences in the array in O(n) time complexity and O(1) extra space. Assume array contain elements in the range 0 to 63. Exampl 8 min read Modify given array to make sum of odd and even indexed elements same Given a binary array arr[] of size N, remove at most N/2 elements from the array such that the sum of elements at odd and even indices becomes equal. The task is to print the modified array.Note: N is always even. There can be more than one possible result, print any of them. Examples: Input: arr[] 10 min read Program to print Sum of even and odd elements in an array Prerequisite - Array Basics Given an array, write a program to find the sum of values of even and odd index positions separately. Examples: Input : arr[] = {1, 2, 3, 4, 5, 6} Output :Even index positions sum 9 Odd index positions sum 12 Explanation: Here, n = 6 so there will be 3 even index position 13 min read Sum of even values and update queries on an array Given an array arr[] of integers and an array q[] of queries. For the ith query, index = q[i][0] and value = q[i][1]. The task is for every query, update arr[index] = arr[index] + value and then return the sum of all the even elements from the array. Examples: Input: arr[] = {1, 2, 3, 4}, q[] = {{0, 13 min read Program to print product of even and odd indexed elements in an Array Given an array of integers. The task is to write a program to find the product of elements at even and odd index positions separately. Note: 0-based indexing is considered for the array. That is the index of the first element in the array is zero. Examples: Input : arr = {1, 2, 3, 4, 5, 6} Output : 9 min read Minimize division by 2 to make Array of alternate odd and even elements Given an array arr[] of N positive integers. The task is to find the minimum number of operations required to make the array containing alternating odd and even numbers. In one operation, any array element can be replaced by its half (i.e arr[i]/2). Examples: Input: N=6, arr = [4, 10, 6, 6, 2, 7]Out 15+ min read Two odd occurring elements in an array where all other occur even times Given an array where all elements appear even number of times except two, print the two odd occurring elements. It may be assumed that the size of array is at-least two. Examples: Input : arr[] = {2, 3, 8, 4, 4, 3, 7, 8} Output : 2 7 Input : arr[] = {15, 10, 10, 50 7, 5, 5, 50, 50, 50, 50, 50} Outpu 15+ min read Like