Update appNew update is available. Click here to update.
About
Government College of Engineering And Ceramic Technology 2022
My Stats
EXP gained
yellow-spark
2962
Level
5 (Champion)
Community stats
Discussions
0
Upvotes
0
Know more
44
Total problems solved
25
Easy
17
Moderate
2
Hard
0
Ninja
Jan Jan Feb Feb Mar Mar Apr Apr May May Jun Jun Jul Jul Aug Aug Sep Sep Oct Oct Nov Nov Dec Dec

Current streak:

0 days

Longest streak:

4 days

Less

More

Achievements
1
Ronin
Topics
Arrays
Discussions
Java Solution
Interview problems

import java.util.Stack;
import java.util.Scanner;


public class Solution {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       int t = sc.nextInt();

       while(t != 0) {

           int n = sc.nextInt();
           Stack<Integer> st = new Stack<> ();
           
           for(int i = 0; i < n; i++) {
               int temp = sc.nextInt();
               st.push(temp);
           }
           
           st = sort(st);

           while(!st.empty()) {
               int temp = st.pop();
               System.out.print(temp + " ");
               
           }

           System.out.println();
           t--;
       }
   }

   public static Stack<Integer> sort(Stack<Integer> st) {
       if(st.size() == 1) 
           return st;
       
       int temp = st.pop();
       st = sort(st);

       st = insert(st, temp);
       return st;
   }

   public static Stack<Integer> insert(Stack<Integer> st, int ele) {
       if(st.empty() || ele <= st.peek()) {
           st.push(ele);
           return st;
       }

       int temp = st.pop();
       st = insert(st, ele);

       st.push(temp);
       return st;
   }
}

profile
Sayan Mondal
Published On 25-Nov-2023
98 views
0 replies
0 upvotes