0;j--){
insert(arr,j);
}
}
}
}
====== 41. --------------------------- STACK ---------------------------
package com.java.hackerrank.ForkJava.MOdule4;
import java.util.Stack;
/**
* @Author pankaj
* @create 11/20/21 5:29 PM
* This Java module deals with Stacks, Queues, and ArrayLists. We'll perform various operations on them.
*
* Given a stack of integers and Q queries. The task is to perform operation on stack according to the query.
*
* Note: use push() to add element in the stack, peek() to get topmost element without removal, pop() gives topmost element with removal, search() to return position if found else -1.
*
* Input Format:
* First line of input contains nubmer of testcases T. For each testcase, first line of input contains Q, number of queries. Next line contains Q queries seperated by space. Queries are as:
*
* i x : (adds element x in the stack).
*
* r : (returns and removes the topmost element from the stack).
*
* h : Prints the topmost element.
*
* f y : (check if the element y is present or not in the stack). Print "Yes" if present, else "No".
*
* Output Format:
* For each testcase, perform Q queries and print the output wherever required.
*
* Your Task:
* Your task is to complete functions insert(), remove(), headOf_Stack() and find(), to insert, remove returning top element and findiing the elment in stack respectively.
*
* Constraints:
* 1 <= T <= 100
* 1 <= Q <= 103
*
* Example:
* Input:
* 2
* 6
* i 2 i 4 i 3 i 5 h f 8
* 4
* i 3 i 4 r f 3
*
* Output:
* 5
* No
* Yes
*
* Explanation:
* Testcase 1: Inserting 2, 4, 3, and 5 onto the stack. Returning top element which is 5. Finding 8 will give No, as 8 is not in the stack.
*/
public class StackOperations {
// Function to insert element to stack
public static void insert(Stack st, int x) {
// Your code here
st.push(x);
}
// Function to pop element from stack
public static void remove(Stack st) {
int x = st.pop();
}
// Function to return top of stack
public static void headOf_Stack(Stack st) {
int x = st.peek();
System.out.println(x + " ");
}package com.java.hackerrank.ForkJava.MOdule4;
import java.util.Stack;
/**
* @Author pankaj
* @create 11/20/21 5:29 PM
* This Java module deals with Stacks, Queues, and ArrayLists. We'll perform various operations on them.
*
* Given a stack of integers and Q queries. The task is to perform operation on stack according to the query.
*
* Note: use push() to add element in the stack, peek() to get topmost element without removal, pop() gives topmost element with removal, search() to return position if found else -1.
*
* Input Format:
* First line of input contains nubmer of testcases T. For each testcase, first line of input contains Q, number of queries. Next line contains Q queries seperated by space. Queries are as:
*
* i x : (adds element x in the stack).
*
* r : (returns and removes the topmost element from the stack).
*
* h : Prints the topmost element.
*
* f y : (check if the element y is present or not in the stack). Print "Yes" if present, else "No".
*
* Output Format:
* For each testcase, perform Q queries and print the output wherever required.
*
* Your Task:
* Your task is to complete functions insert(), remove(), headOf_Stack() and find(), to insert, remove returning top element and findiing the elment in stack respectively.
*
* Constraints:
* 1 <= T <= 100
* 1 <= Q <= 103
*
* Example:
* Input:
* 2
* 6
* i 2 i 4 i 3 i 5 h f 8
* 4
* i 3 i 4 r f 3
*
* Output:
* 5
* No
* Yes
*
* Explanation:
* Testcase 1: Inserting 2, 4, 3, and 5 onto the stack. Returning top element which is 5. Finding 8 will give No, as 8 is not in the stack.
*/
public class StackOperations {
// Function to insert element to stack
public static void insert(Stack st, int x) {
// Your code here
st.push(x);
}
// Function to pop element from stack
public static void remove(Stack st) {
int x = st.pop();
}
// Function to return top of stack
public static void headOf_Stack(Stack st) {
int x = st.peek();
System.out.println(x + " ");
}
// Function to find the element in stack
public static void find(Stack st, int val) {
if (st.search(val)!=-1) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
// Function to find the element in stack
public static void find(Stack st, int val) {
if (st.search(val)!=-1) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
42. ----------------------------------------- Queue operation --------------------------------------
package com.java.hackerrank.ForkJava.MOdule4;
import java.util.Queue;
/**
* @Author pankaj
* @create 11/20/21 6:39 PM
Here, we will learn operations on queues.
Given N integers, the task is to insert those elements in the queue. Also, given M integers, task is to find the frequency of each number in the Queue.
Example:
Input:
8
1 2 3 4 5 2 3 1
5
1 3 2 9 10
Output:
2
2
2
-1
-1
Explanation:
After inserting 1, 2, 3, 4, 5, 2, 3, 1 into the queue,
frequency of 1 is 2, 3 is 2, 2 is 2, 9 is -1 and 10 is
-1 (since, 9 and 10 is not there in the queue).
Your Task:
Your task is to complete the functions insert() and findFrequency() which inserts the element into the queue and find the count of occurences of element in the queue respectively.
*/
public class QueueOperations {
// Function to insert element into the queue
static void insert(Queue q, int k){
// Your code here
q.add(k);
}
// Function to find frequency of an element
// rteturn the frequency of k
static int findFrequency(Queue q, int k){
// Your code here
int count =0;
for (int i=0;i q, int k){
// Your code here
//Just insert k in q and don't return anything
q.add(k);
}
// Function to find an element k
static boolean find(PriorityQueue q, int k){
// Your code here
// If k is in q return true else return false
if (q.contains(k))
return true;
else
return false;
}
// Function to delete the max element from queue
static int delete(PriorityQueue q){
// Your code here
//Delete the max element from q. The priority queue property might be useful here
return q.poll();
}
}
44. ---------------------------------------------- ArrayList Operation ---------------------------------
Given an ArrayList of N lowercase characters. The task is to count frequency of elements in the list.
Note: use add() to append element in the list and contains() to check an element is present or not in the list and collections.frequency() to find the frequency of the element in the list.
Input Format:
First line of testcase contains T, number of testcases. For each testcase, first line contains number of queries Q. Each query may be any one of the two type:
1. "i" with "c" : insert the element "c" into the ArrayList
2. "f" with "c": find the frequency of "c" in the ArrayList.
Output Format:
For each testcase, output the frequency of elements. Print "Not Present" if element is not present in the list else its frequency in new line for every query.
Your Task:
Your task is to complete the functions insert() and freq() which are used to insert and find frequency of element respectively.
Constraints:
1 <= T <= 100
1 <= N <= 105
1 <= Q <= 102
Example:
Input:
2
6
i g i e i e i k i s f e
4
i c i p i p f f
Output:
2
Not Present
Explanation:
Testcase 1: Inserting g, e, e, k, s into the list. Frequency of e is 2 in the list.
Testcase 2: Inserting c, p, p into the list. Frequency of f is 0 in the list.
public class ArrayListOperation {
// Function to insert element
public static void insert(ArrayList clist, char c) {
//add c to clist
clist.add(c);
}
// Function to count frequency of element
public static void freq(ArrayList clist, char c)
{
if(clist.contains(c))
System.out.println(Collections.frequency(clist,c));
else
System.out.println("Not Present");
}
}
45. ---------------------------------- Operations on ArrayList -------------------------------------
package com.java.hackerrank.ForkJava.MOdule4;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
/**
* @Author pankaj
* @create 11/20/21 7:35 PM
* 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.
*
* 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.
*/
public class OperationsOnArrayList {
// Function to insert element into list
public static void insert(ArrayList list, int x) {
// Your code here
list.add(x);
}
// Function to sort list in Increasing order
public static void IncOrder(ArrayList list) {
// Your code here
Collections.sort(list);
}
// Function to search element in the lise
// val : element to be searched
public static void Search(ArrayList 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);}
}
// Function to sort list in decreasing order
public static void DecOrder(ArrayList list) {
// Your code here
Collections.sort(list,Collections.reverseOrder());
}
}
46. --------------------------------------------