Operations on ArrayList | Practice | GeeksforGeeks

Given an arraylist of integers. The task is to perform operations on ArrayList according to the queries and print output for the required queries.
Note: use Collections.sort() to sort the list in natural order, Collections.reverseOrder() to arrange the elements in reverse, get() to return the element at index of the list, binarySearch() to find the index of the specified element in the list.
Input Format:
First line of input contains number of testcase T. For each testcase, first line of input contains Q, number of queries. Next line contains Q queries as:


a x : inserts the element x at the end of the list.


i : arrange the list in increasing order.


d : arrange the list in decreasing order.


s y : search for the element y in the list and prints the first-index(for duplicate) (0-based indexing) of that in the list. -1 if not found.



a x : inserts the element x at the end of the list.

a x : inserts the element x at the end of the list.

i : arrange the list in increasing order.

i : arrange the list in increasing order.

d : arrange the list in decreasing order.

d : arrange the list in decreasing order.

s y : search for the element y in the list and prints the first-index(for duplicate) (0-based indexing) of that in the list. -1 if not found.

s y : search for the element y in the list and prints the first-index(for duplicate) (0-based indexing) of that in the list. -1 if not found.
Output Format:
For each testcase, print the output according to the queries.
Your Task:
Your task is to complete the functions insert(), IncOrder(), Search() and DecOrder() such that driver code will be able to perform required queries.
Constraints:
1 <= T <= 100
1 <= A[i] <= 106
1 <= Q <= 103
Example:
Input:
2
6
a 2 a 3 a 4 a 2 i s 2
5
a 2 a 3 a 3 d s 5
Output:
0
-1
Explanation:
Testcase 1: After inserting elements, list is (2, 3, 4, 2) and arranging them in ascending order, we have list as (2, 2, 3, 4). Now, serching for 2, first occurence is at 0th index.
Testcase 2: After inserting elements, list is (2, 3, 3) and arranging them in descending order, we have list as (3, 3, 2). Finding 5 gives -1, as 5 is not present.
0
rahul8781 month ago
////////////////////JAVA/////////////////////////
public static void Search(ArrayList<Integer> list, int val)
    {
        
        
        int flag = -1;
        int index=-1;
        for(int i=0;i<list.size();i++){
            if(val==list.get(i)){
             flag=1;
             index=i;
             break;
            }
        }
        
        if(flag == -1){
            System.out.println("-1");
        }
        else if(flag==1)
        System.out.println(index);
        
        
    }
    
    // Function to sort list in decreasing order
    public static void DecOrder(ArrayList<Integer> list)
    {
        Collections.sort(list , Collections.reverseOrder());
    }
0
wasimakrom5203 months ago
//In JAVA //
class Geeks{   // Function to insert element into list   public static void insert(ArrayList<Integer> list, int x)   {       // Your code here       list.add(x);          }      // Function to sort list in Increasing order   public static void IncOrder(ArrayList<Integer> list)   {       // Your code here       Collections.sort(list);          }      // Function to search element in the lise   // val : element to be searched   public static void Search(ArrayList<Integer> list, int val)   {                     int flag = -1;       /*       Your Code here        * */        if(list.contains(val)){            System.out.println(list.indexOf(val));            flag =1;        }       if(flag == -1){           System.out.println("-1");       }                 }      // Function to sort list in decreasing order   public static void DecOrder(ArrayList<Integer> list)   {       // Your code here       Collections.sort(list,Collections.reverseOrder());   }}
0
wasimakrom5203 months ago
 return max*min;
0
vishalguptabayana4 months ago
How to solve Decorder function
0
khushikhushhai12107 months ago
Search function in Java
    public static void Search(ArrayList<Integer> list, int val)   {       int flag = -1;       /*       Your Code here        * */       if(list.contains(val))       {         System.out.println((Collections.binarySearch(list,val))-1);         flag=0;       }                 if(flag == -1){           System.out.println("-1");       }   }   
0
Shruti sugandhi11 months ago
Shruti sugandhi
public static void Search(ArrayList<integer> list, int val)    { int flag=-1;    Collections.sort(list);    if(list.contains(val))          flag=list.indexOf(val);         if(flag==-1)            System.out.println("flag");            else            System.out.println(flag);
    }
0
Amir Ansari1 year ago
Amir Ansari
Correct AnswerExecution Time:0.19
class Geeks{    // Function to insert element into list    public static void insert(ArrayList<integer> list, int x)    {        // Your code here         list.add(x);
    }
    // Function to sort list in Increasing order    public static void IncOrder(ArrayList<integer> list)    {        // Your code here        Collections.sort(list);
    }
    // Function to search element in the lise    // val : element to be searched    public static void Search(ArrayList<integer> list, int val)    {
        int flag = -1;if(list.contains(val)){System.out.println(list.indexOf(val));flag=1;}if(flag == -1){System.out.println("-1");}
    }
    // Function to sort list in decreasing order    public static void DecOrder(ArrayList<integer> list)    {        // Your code here        Collections.sort(list,Collections.reverseOrder());    }}
0
S.Vishakhavel2 years ago
S.Vishakhavel
list.indexOf(val);
0
Dev Kumar2 years ago
Dev Kumar
class Geeks{    public static void insert(ArrayList<integer> list, int x)    {        list.add(x);
    }    public static void IncOrder(ArrayList<integer> list)    {       Collections.sort(list);
    }    public static void Search(ArrayList<integer> list, int val)    {        if(list.contains(val)){            Collections.sort(list);            System.out.println(list.indexOf(val));        }        else{            System.out.println("-1");        }    }    public static void DecOrder(ArrayList<integer> list)    {        Collections.sort(list, Collections.reverseOrder());    }}
0
Alicia Man2 years ago
Alicia Man
public static void Search(ArrayList<integer> list, int val)    {
        int flag = -1;        if(list.contains(val)){            System.out.println(list.indexOf(val));            flag=1;        }        if(flag == -1){            System.out.println("-1");        }    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.