Given elements of a stack, clone the stack without using extra space. 🔗Goto
Example 1:
Input:
N = 10
st[] = {1, 1, 2, 2, 3, 4, 5, 5, 6, 7}
Output:
1
Full Code
import java.io.*;
import java.util.*;
class GFG {
public static void main(String args[]) throws IOException {
BufferedReader read =
new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {
int N = Integer.parseInt(read.readLine());
String S[] = read.readLine().split(" ");
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> copy = new ArrayList<>();
for(int i=0; i<N; i++)
{
st.push(Integer.parseInt(S[i]));
copy.add(Integer.parseInt(S[i]));
}
Collections.reverse(copy);
Stack<Integer> cloned = new Stack<Integer>();
Solution ob = new Solution();
ob.clonestack(st,cloned);
ArrayList<Integer> check = new ArrayList<>();
while(cloned.size() != 0)
check.add(cloned.pop());
int flag = 0;
if(copy.equals(check))
flag = 0;
else
flag = 1;
System.out.println(1-flag);
}
}
}class Solution {
void clonestack(Stack<Integer> st, Stack<Integer> cloned) {
// // approach 1:
// if(st.isEmpty())
// return;
// int top = st.peek();
// st.pop();
// clonestack(st,cloned);
// cloned.push(top);
// // approach 2:
cloned.addAll(st);
// // approach 3:
// if(st.isEmpty())
// return;
// int top = st.pop();
// clonestack(st,cloned);
// cloned.push(top);
// // approach 4:
// for(int i=0; i<st.size(); i++){
// cloned.push(st.get(i));
// }
}
}