Java Collection | Set 1 (ArrayList) Part-1 | Practice | GeeksforGeeks

ArrayList are dynamic size arrays. Try this problem using ArrayList.
Given a ArrayList of N elements and a integer Q defining the type of query(which will be either 1 or 2) :
Q = 1 includes two integers p and r. Which means insert the value r at index p in the ArrayList and print the whole updated ArrayList.
Q = 2 includes one integer p. In this query print the index at which the value p is last found in the ArrayList. If the value p is not found in the ArrayList then print "-1". 
NOTE: Assume 0 based indexing
 
Example 1:
Input:
N = 5, Q = 1 
A[] = {1, 4, 5, 9, 3} 
Query[] = {2, 6} 
Output: 
1 4 6 5 9 3 
Explanation: 
p=Query[0]=2 
r=Query[1]=6 
After inserting the element 
r=6 at index p=2 ,the updated 
arraylist ={1,4,6,5,9,3}
 
Example 2:
Input:
N = 4 , Q = 2
A[]= {1, 9, 2, 4}
Query[]= {4}
Output:
3 
Explanation: 
p = 4 
The element 4 is last found
in A at index = 3   
 
Your Task: 
You don't need to read input or print anything. Your task is to complete the function solve() which takes the N (number of elements in Array A) ,ArrayList A, Q(Type of the of query) and the ArrayList Query. If the Q = 1 then return the updated ArrayList of integers. else return the ArrayList which contains the index at which the value p is last found in the ArrayList A (where p = Query[0] ) ,If the value of p is not found then return the ArrayList which contains -1. 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 104
1 <= Q <= 2
If Q = 1 then size of Query is 2 ,
where Query[0] represents the value of p and Query[0] represents the value of r.
If Q = 2 then size of Query is 1 ,
where Query[0] represents the value of p.
1 <= A[i] <= 103
0
pankajkumarravi6 months ago
//================= Fully working Logic =============
 
ArrayList<Integer> solve(int N, ArrayList<Integer> A, int Q, ArrayList<Integer> Query) {    // code here    if (Q == 1) {        int p = Query.get(0);        int r = Query.get(1);        A.add(p, r);        return A;    } else {        ArrayList<Integer> response = new ArrayList<>();        response.add(A.lastIndexOf(Query.get(0)));        return response;    }}
0
Hrithik Gajavalli
This comment was deleted.
0
Hrithik Gajavalli
This comment was deleted.
0
Hrithik Gajavalli
This comment was deleted.
0
Sagar Boyal11 months ago
Sagar Boyal
// code here        ArrayList<integer> a = new ArrayList<integer>();        if(Query.size() == 0){            a.add(-1);            return a;        }        if(Q == 1){               A.add(Query.get(0), Query.get(1));                return A;        }else{                a.add(A.lastIndexOf(Query.get(0)));                return a;        }
0
Debojyoti Sinha1 year ago
Debojyoti Sinha
Correct Answer.Correct AnswerExecution Time:0.91
class Solution {     ArrayList<integer> solve(int N, ArrayList<integer> A, int Q, ArrayList<integer> Query)     {        if(Q == 1)        {            int p = Query.get(0);            int r = Query.get(1);                        A.add(p, r);                        return A;        }        else        {            ArrayList<integer> res = new ArrayList<integer>();                        int p = Query.get(0);                        res.add(A.lastIndexOf(p));                         return res;        }    }}
0
Amir Ansari1 year ago
Amir Ansari
Correct AnswerExecution Time:0.97
ArrayList<integer> solve(int N, ArrayList<integer> A, int Q, ArrayList<integer> Query)     {         // code here        ArrayList<integer> ans = new ArrayList<integer>();
          if(Q == 1){            int p=Query.get(0);            int r=Query.get(1);            A.add(p,r);
            for(int i=0;i<=N;i++)            {                ans.add(A.get(i));            }          }           else        {            int p=Query.get(0);            ans.add(A.lastIndexOf(p));        }          return ans;    }
-1
vasu nagpal1 year ago
vasu nagpal
I am still getting an error for some input case. Cant figure out where is the problem
https://codeshare.io/2EzRVO
-1
Shreya Patil2 years ago
Shreya Patil
/*package whatever //do not write package name here */
import java.util.*;import java.lang.*;import java.io.*;
class GFG {public static void main (String[] args) {//codeScanner sc=new Scanner(System.in);int t=sc.nextInt();
while(t-->0){    int n=sc.nextInt();    int q=sc.nextInt();    ArrayList<integer> l=new ArrayList<integer>(n);    //int num=sc.nextInt();    for(int i=0;i<n;i++) {="" l.add(sc.nextint());="" }="" switch(q)="" {="" case="" 1:="" int="" p="sc.nextInt();" int="" r="sc.nextInt();" l.add(p,r);="" for(int="" i="0;i&lt;l.size();i++)" {="" system.out.print(l.get(i)+"="" ");="" }="" system.out.println();="" break;="" case="" 2:="" int="" m="sc.nextInt();" int="" index="0;" if(l.contains(m))="" {="" index="l.lastIndexOf(m);" system.out.println(index);="" }="" else="" {="" system.out.println("-1");="" }="" break;="" }="" }="" }="" }<code="">
0
Vangel Hristov2 years ago
Vangel Hristov
Hi guys i think i manged to solve this problem but after hitting submit it says that my code doesn't give correct answer for all test cases, which i think is a format problem. Please give me hints about my errors.
import java.util.ArrayList;import java.util.Scanner;
public class QueriesTesting {
    public static void main(String[] args) {        ArrayList<integer> numList = new ArrayList<integer>();        Scanner key = new Scanner(System.in);        int T = key.nextInt(); //test cases        //Query 1: r is value, p is index        //Query 2: if it includes p return value, if not return -1
        while (T > 0) {            int n = key.nextInt(); //elements in array            int q = key.nextInt(); //type of query            //int a[] = new int[n]; //array to enter the elements            if (q == 1) {                for (int j = 0; j < n; j++) {                    int element = key.nextInt();                    numList.add(element);                }
                int p = key.nextInt();                int r = key.nextInt();                numList.add(p, r);                System.out.println(numList);                numList.clear();            }            if (q == 2) {                for (int j = 0; j < n; j++) {                    int element = key.nextInt();                    numList.add(element);                }                int p = key.nextInt();                System.out.println(numList.indexOf(p));                numList.clear();            }            T--;        }    }}
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.