Find the size of the final imaginary Array after removing the balls
Last Updated :
03 Nov, 2023
Given 2 arrays color[]and radius[] of length N each representing N balls, 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 imaginary 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 radius 5. So, they are removed. Now, we have got our color[]={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:
Use a vector to store the indices of the remaining elements in the final sequence and remove adjacent pairs that have the same color and radius.
Below are the steps mentioned to implement the code:
- Create an empty vector to store the indices of the remaining elements in the final sequence.
- Loop through the elements of the given sequence.
- For each element, check if the last element in the ‘ans’ vector has the same color and radius as the current element.
- If they are the same, remove the last element from the ‘ans‘ vector. If they are not the same, add the current index to the ‘ans’ vector.
- Finally, return the size of the ‘ans‘ vector, which represents the length of the final sequence after removing all adjacent pairs that have the same color and radius.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int find_Length( int n, vector< int > color,
vector< int > radius)
{
vector< int > ans;
for ( int i = 0; i < n; i++) {
if (ans.size() != 0 && color[i] == color[ans.back()]
&& radius[ans.back()] == radius[i]) {
ans.pop_back();
}
else {
ans.push_back(i);
}
}
return ans.size();
}
int main()
{
int n = 3;
vector< int > color(n), radius(n);
color = { 2, 2, 5 };
radius = { 3, 3, 4 };
int ans = find_Length(n, color, radius);
cout << ans << "\n" ;
return 0;
}
|
Java
import java.util.*;
class Main {
static int findLength( int n, List<Integer> color,
List<Integer> radius) {
List<Integer> ans = new ArrayList<>();
for ( int i = 0 ; i < n; i++) {
if (!ans.isEmpty() && color.get(i).equals(color.get(ans.get(ans.size() - 1 )))
&& radius.get(ans.get(ans.size() - 1 )).equals(radius.get(i))) {
ans.remove(ans.size() - 1 );
} else {
ans.add(i);
}
}
return ans.size();
}
public static void main(String[] args) {
int n = 3 ;
List<Integer> color = new ArrayList<>(Arrays.asList( 2 , 2 , 5 ));
List<Integer> radius = new ArrayList<>(Arrays.asList( 3 , 3 , 4 ));
int ans = findLength(n, color, radius);
System.out.println(ans);
}
}
|
Python3
def find_length(n, color, radius):
ans = []
for i in range (n):
if ans and color[i] = = color[ans[ - 1 ]] and radius[ans[ - 1 ]] = = radius[i]:
ans.pop()
else :
ans.append(i)
return len (ans)
if __name__ = = "__main__" :
n = 3
color = [ 2 , 2 , 5 ]
radius = [ 3 , 3 , 4 ]
ans = find_length(n, color, radius)
print (ans)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int FindLength( int n, List< int > color, List< int > radius)
{
List< int > ans = new List< int >();
for ( int i = 0; i < n; i++)
{
if (ans.Count > 0 && color[i] == color[ans[ans.Count - 1]]
&& radius[ans[ans.Count - 1]] == radius[i])
{
ans.RemoveAt(ans.Count - 1);
}
else
{
ans.Add(i);
}
}
return ans.Count;
}
public static void Main( string [] args)
{
int n = 3;
List< int > color = new List< int > { 2, 2, 5 };
List< int > radius = new List< int > { 3, 3, 4 };
int ans = FindLength(n, color, radius);
Console.WriteLine(ans);
}
}
|
Javascript
function FindLength(n, color, radius) {
let ans = [];
for (let i = 0; i < n; i++) {
if (ans.length > 0 && color[i] === color[ans[ans.length - 1]]
&& radius[ans[ans.length - 1]] === radius[i]) {
ans.pop();
} else {
ans.push(i);
}
}
return ans.length;
}
let n = 3;
let color = [2, 2, 5];
let radius = [3, 3, 4];
let ans = FindLength(n, color, radius);
console.log(ans);
|
Time Complexity: O(n), since we traverse once through the array.
Auxiliary Space: O(n), since we are using a vector to store the elements.
Approach – 2: Using Stack
1. Initializing an empty stack to store the colors and radii of the imaginary circles as pairs.
2. For each circle, we check if it has the same color and radius as the top element in the stack. If so, we pop the top element.
3. If the circle’s color and radius are different from the top element, we push it onto the stack.
4. At the end, the size of the stack will be the length of the longest imaginary.
5. Finally, return the size of the stack as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int find_Length( int N, vector< int > color,
vector< int > radius)
{
stack<pair< int , int >> st;
for ( int i = 0; i < N; i++) {
bool flag = true ;
int col = color[i], rad = radius[i];
while (!st.empty() && (st.top().first == col && st.top().second == rad)) {
st.pop();
flag = false ;
}
if (flag) st.push({col,rad});
}
return st.size();
}
int main()
{
int n = 3;
vector< int > color(n), radius(n);
color = { 2, 2, 5 };
radius = { 3, 3, 4 };
int ans = find_Length(n, color, radius);
cout << ans << "\n" ;
return 0;
}
|
Java
import java.util.Stack;
public class Main {
public static void main(String[] args) {
int n = 3 ;
int [] color = { 2 , 2 , 5 };
int [] radius = { 3 , 3 , 4 };
int ans = findLength(n, color, radius);
System.out.println(ans);
}
static int findLength( int N, int [] color, int [] radius) {
Stack<Pair> st = new Stack<>();
for ( int i = 0 ; i < N; i++) {
boolean flag = true ;
int col = color[i];
int rad = radius[i];
while (!st.isEmpty() && st.peek().color == col && st.peek().radius == rad) {
st.pop();
flag = false ;
}
if (flag) {
st.push( new Pair(col, rad));
}
}
return st.size();
}
}
class Pair {
int color;
int radius;
public Pair( int color, int radius) {
this .color = color;
this .radius = radius;
}
}
|
Python3
def find_Length(N, color, radius):
st = []
for i in range (N):
flag = True
col = color[i]
rad = radius[i]
while st and (st[ - 1 ][ 0 ] = = col and st[ - 1 ][ 1 ] = = rad):
st.pop()
flag = False
if flag:
st.append((col, rad))
return len (st)
n = 3
color = [ 2 , 2 , 5 ]
radius = [ 3 , 3 , 4 ]
ans = find_Length(n, color, radius)
print (ans)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static int FindLength( int N, List< int > color, List< int > radius)
{
Stack<Tuple< int , int >> stack = new Stack<Tuple< int , int >>();
for ( int i = 0; i < N; i++)
{
bool flag = true ;
int col = color[i];
int rad = radius[i];
while (stack.Count > 0 && (stack.Peek().Item1 == col && stack.Peek().Item2 == rad))
{
stack.Pop();
flag = false ;
}
if (flag)
{
stack.Push(Tuple.Create(col, rad));
}
}
return stack.Count;
}
static void Main()
{
int n = 3;
List< int > color = new List< int > { 2, 2, 5 };
List< int > radius = new List< int > { 3, 3, 4 };
int ans = FindLength(n, color, radius);
Console.WriteLine(ans);
}
}
|
Javascript
class Pair {
constructor(color, radius) {
this .color = color;
this .radius = radius;
}
}
function findLength(N, color, radius) {
const stack = [];
for (let i = 0; i < N; i++) {
let flag = true ;
const col = color[i];
const rad = radius[i];
while (stack.length > 0 && stack[stack.length - 1].color === col && stack[stack.length - 1].radius === rad) {
stack.pop();
flag = false ;
}
if (flag) {
stack.push( new Pair(col, rad));
}
}
return stack.length;
}
const n = 3;
const color = [2, 2, 5];
const radius = [3, 3, 4];
const ans = findLength(n, color, radius);
console.log(ans);
|
Time Complexity: O(n), as traversing over the array so time complexity is O(n).
Auxiliary Space: O(n), as taken a stack so space complexity is O(n).
Share your thoughts in the comments
Please Login to comment...