Length of Array after removing consecutive balls
Last Updated :
09 Aug, 2023
Given two arrays color[] and radius[] of length N each, where color[i] represents the color of the ith ball while radius[i] represents the radius of the ith ball. If two consecutive balls have the same color and size, both are removed from the array. The task is to find the length of the final array.
Examples:
Input: N = 3, color[] = {2, 2, 5}, radius[] = {3, 3, 4}
Output: 1
Explanation: First ball and second ball have the same color 2 and the same radius 3.So, after removing only one ball is left. It could not be removed from the array. Hence, the final array has length 1.
Input: N = 4, color[] = {1, 3, 3, 1}, radius[] = {2, 5, 5, 2}
Output: 0
Explanation: Ball 2 and ball 3 have the same color 3 and the same radius5.So, they are removed. Now, we have got ourcolor[]={1, 1} and radius[]={2, 2}. Both the left balls are consecutive now and they are having same color and radius. So, these two balls are removed as well. Now, we are left with the empty final array. Hence, the length of the final array is 0.
Approach: This can be solved with the following idea:
This can be solved using a Stack data structure. If the top of the stack is equal to the next element remove and skip the element.
Below are the steps involved in the implementation of the code:
- We create a Pair class to store the color and radius together of the ith column.
- We declare a Stack<Pair> to store the color and radius in pair form.
- We run a loop from 0 to N and if the stack is empty, we push an ith column of color and radius together in the stack.
- If the stack is not empty, we check if the current ith element of color matches with the topmost element of the stack
(st.peek().a == color[i]) && ith element of radius matches with the topmost element of the stack (st.peek().b == radius[i]), we pop the particular pair.
- Else we push it into the stack.
- At last, the pair left in the stack is the length of the final imaginary array, and we return the stack’s size().
Below is the implementation of the code:
C++
#include <bits/stdc++.h>
using namespace std;
struct pair1 {
int a, b;
pair1( int x, int y)
{
a = x;
b = y;
}
};
int finLength( int N, int color[], int radius[])
{
stack<pair1> st;
for ( int i = 0; i < N; i++) {
if (st.empty()) {
st.push(pair1(color[i], radius[i]));
}
else {
if (st.top().a == color[i]
&& st.top().b == radius[i]) {
st.pop();
}
else {
st.push(pair1(color[i], radius[i]));
}
}
}
return st.size();
}
int main()
{
int N = 3;
int color[] = { 2, 2, 5 };
int radius[] = { 3, 3, 4 };
cout << finLength(N, color, radius) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class pair {
int a, b;
public pair( int a, int b)
{
this .a = a;
this .b = b;
}
}
class GFG {
public static int finLength( int N, int [] color,
int [] radius)
{
Stack<pair> st = new Stack<>();
for ( int i = 0 ; i < N; i++) {
if (st.isEmpty()) {
st.push( new pair(color[i], radius[i]));
}
else {
if (st.peek().a == color[i]
&& st.peek().b == radius[i]) {
st.pop();
}
else {
st.push( new pair(color[i], radius[i]));
}
}
}
return st.size();
}
public static void main(String[] args)
{
int N = 3 ;
int color[] = { 2 , 2 , 5 };
int radius[] = { 3 , 3 , 4 };
System.out.println(finLength(N, color, radius));
}
}
|
Python3
class pair:
def __init__( self , a, b):
self .a = a
self .b = b
def finLength(N, color, radius):
st = []
for i in range (N):
if not st:
st.append(pair(color[i], radius[i]))
else :
if st[ - 1 ].a = = color[i] and st[ - 1 ].b = = radius[i]:
st.pop()
else :
st.append(pair(color[i], radius[i]))
return len (st)
if __name__ = = '__main__' :
N = 3
color = [ 2 , 2 , 5 ]
radius = [ 3 , 3 , 4 ]
print (finLength(N, color, radius))
|
C#
using System;
using System.Collections.Generic;
class pair
{
public int a, b;
public pair( int a, int b)
{
this .a = a;
this .b = b;
}
}
class GFG
{
public static int FinLength( int N, int [] color, int [] radius)
{
Stack<pair> st = new Stack<pair>();
for ( int i = 0; i < N; i++)
{
if (st.Count == 0)
{
st.Push( new pair(color[i], radius[i]));
}
else
{
if (st.Peek().a == color[i] && st.Peek().b == radius[i])
{
st.Pop();
}
else
{
st.Push( new pair(color[i], radius[i]));
}
}
}
return st.Count;
}
public static void Main( string [] args)
{
int N = 3;
int [] color = { 2, 2, 5 };
int [] radius = { 3, 3, 4 };
Console.WriteLine(FinLength(N, color, radius));
}
}
|
Javascript
class GFG {
constructor(color, radius) {
this .color = color;
this .radius = radius;
}
}
function findFinalLength(N, colors, radii) {
const stack = [];
for (let i = 0; i < N; i++) {
if (stack.length === 0) {
stack.push( new GFG(colors[i], radii[i]));
} else {
if (stack[stack.length - 1].color === colors[i] &&
stack[stack.length - 1].radius === radii[i]) {
stack.pop();
} else {
stack.push( new Pair(colors[i], radii[i]));
}
}
}
return stack.length;
}
function main() {
const N = 3;
const colors = [2, 2, 5];
const radii = [3, 3, 4];
console.log(findFinalLength(N, colors, radii));
}
main();
|
Time Complexity: O(n), since we traverse once through the array.
Auxiliary Space: O(n), since we are using a Stack for storing the elements in pairwise form.
Share your thoughts in the comments
Please Login to comment...