Posts

Showing posts with the label ArrayList

Stock Span Problem Geeks For Geeks

 class StoreStack{ int val; int data; } class Solution {     //Function to calculate the span of stock’s price for all n days.          public static int[] calculateSpan(int arr[], int n)     {         ArrayList<Integer> aa=new ArrayList<Integer>();         StoreStack obj[]=new StoreStack[n];         Stack<StoreStack> stack=new Stack<StoreStack>();         for(int i=0;i<n;i++){ obj[i]=new StoreStack(); obj[i].val=arr[i]; obj[i].data=i; } /*for(int i=0;i<n;i++){ System.out.println(obj[i].val+" "+obj[i].data); }*/ for(int i=0;i<n;i++){ if(stack.isEmpty()){ aa.add(-1); } else{ if(stack.peek().val>arr[i]){ aa.add(stack.peek().data); } else{ while(!stack.isEmpty() && stack.peek().val<=arr[i]){ stack.pop(); } if(stack...

Minimum Distances HackerRank

  class  Result {      /*      * Complete the 'minimumDistances' function below.      *      * The function is expected to return an INTEGER.      * The function accepts INTEGER_ARRAY a as parameter.      */      public   static   int  minimumDistances(List<Integer> a) {         HashMap<Integer,Integer> map= new  HashMap<Integer,Integer>();         ArrayList<Integer>aa= new  ArrayList<Integer>();          for ( int  i= 0 ;i<a.size();i++){              if (!ma...

Longest SubString Without Repeating Characters LeetCode

         Way 1 : Using ArrayDeque and ArrayList           int count=0;         int index=0;         ArrayList<Integer>aa=new ArrayList<Integer>();         ArrayDeque<Character> arr=new ArrayDeque<Character>();         for(int i=0;i<s.length();i++){             if(arr.contains(s.charAt(i))){                 aa.add(count); count=0;                 arr.clear(); i=index++;                              }             else{                 arr.addLast(s.charAt(i)); count++;             }         }       ...

Median Of Two Sorted Arrays LeetCode And Geeks For Geeks

 class Solution {     public double findMedianSortedArrays(int[] nums1, int[] nums2) {         int x=0;         int y=0;         int n=nums1.length;         int m=nums2.length;         PriorityQueue<Integer> pq=new PriorityQueue<Integer>();         while(x<nums1.length && y<nums2.length){             if(nums1[x]<nums2[y]){                 pq.add(nums1[x]);                 x++;             }             else if(nums1[x]>nums2[y]){                 pq.add(nums2[y]);                 y++;             }             else{ ...

Minimum Indices HackerEarth

  import java . util .*;     class TestClass { public static void main ( String args [] ) throws Exception { Scanner sc = new Scanner ( System . in ); int n = sc . nextInt (); int q = sc . nextInt (); int arr []= new int [ n ]; for ( int i = 0 ; i < n ; i ++){ arr [ i ]= sc . nextInt (); } ArrayList < Integer > aa = new ArrayList < Integer >(); ArrayList < Integer > ab = new ArrayList < Integer >(); int sum = 0 ; for ( int i = 0 ; i < n ; i ++){ String s = String . valueOf ( arr [ i ]); for ( int j = 0 ; j < s . length (); j ++){ sum = sum + Integer . valueOf ( String . valueOf ( s . charAt ( j ))); } aa . add ( sum ); sum = 0 ; } //System.out.println(aa+" "); int count = 0 ; for ( int i = 0 ; i < q ; i ++){ int a = sc . nextInt (); int b =...

ELECTRONICS SHOP HACKERRANK

APPROACH 1: TIME: O(N^2) SPACE:O(NLOG(K) ie NLOG(1) as k=1)[ArrayList and Priority Queue] static   int  getMoneySpent( int [] arr1,  int [] arr2,  int  a) {          int  b=arr1.length;          int  c=arr2.length;                  int  start= 0 ;          int  sum= 0 ;          int  end=b- 1 ;         ArrayList<Integer> aa= new  ArrayList<Integer>();          while (start<=end){              for ( int  i= 0 ;i<c;i++){                 sum=arr1[...

Sum Of Unique Elements LeetCode

Approach Explained:  Here first we will add all the numbers in the ArrayList 1 . After that we will traverse the ArrayList1 and then add all the duplicates in the ArrayList 2 . We are doing sum simultaneously for both ArrayList. Then, we will have sum of the original ArrayList (the input array) and duplicate elements sum2. We will then subtract sum2 from sum1 which is ultimately the sum of all unique elements.  First Way: TIME O(N) [LeetCode Time 3 ms] SPACE O(N) [Using of Two ArrayLists][LeetCode Memory Uasge : 36.8MB less than 34.90%]  class Solution {     public int sumOfUnique(int[] nums) {         int sum=0; int sum2=0;         ArrayList<Integer> aa=new ArrayList<Integer>(); ArrayList<Integer> ab=new ArrayList<Integer>(); for(int i=0;i<nums.length;i++){ aa.add(nums[i]); sum=sum+aa.get(i); } for(int i=0;i<aa.size();i++){ if(Collections.frequency(aa,aa.get(i))>...

K Largest Element LeetCode

 Approach 1: Using ArrayList and Sorting    TIME : O(NLOGN) [LeetCode Time: 5 ms] SPACE: O(N)                [LeetCode Space 39.2MB] class Solution {     public int findKthLargest(int[] arr, int k) {         ArrayList<Integer> aa=new ArrayList<Integer>();         for(int i=0;i<arr.length;i++){             aa.add(arr[i]);         }         Collections.sort(aa);         return (aa.get(aa.size()-k));     } } ========================================================================= Approach 2: Using Priority Queue and getting the peak element TIME: O(NLOGK) [LeetCode Time: 2ms] SPACE: O(K)              [LeetCode Space: 39.8MB] class Solution {     public int findKthLargest(int[] arr, int k) {         ...

K largest Elements Geeks For Geeks

 class Solution {     //Function to return k largest elements from an array.     public static ArrayList<Integer> kLargest(int arr[], int n, int k)     {         PriorityQueue <Integer> p=new PriorityQueue<Integer>(); for(int i=0;i<k;i++){ p.add(arr[i]); } for(int i=k;i<n;i++){ if(p.peek()<arr[i]){ p.remove(); p.add(arr[i]); } } ArrayList<Integer> aa=new ArrayList<Integer>(); aa.addAll(p); Collections.sort(aa,Collections.reverseOrder()); return aa;     } } Using Priority Queue Time: O(NLOGK) [Sorting of K elements list] [GFG Time : 3.6/6.8] Space: O(K) [Using ArrayList additionally of size k] {Here we will be adding the first k elements in the priority queue. Then for the next (n-k) elements, we will compare with the peek element of the queue. If the peek element is small then we will remove it from the queue and add the element from ...

Common Elements Geeks For Geeks

TIME O(NLOGN)  [GFG TIME : 3.2/7.2] SPACE O(N)  class Solution{         public static ArrayList<Integer> common_element(ArrayList<Integer>arr1, ArrayList<Integer>arr2)     {                   int x=0; int y=0; int n=arr1.size(); int m=arr2.size(); Collections.sort(arr1); Collections.sort(arr2); ArrayList<Integer> set=new ArrayList<Integer>(); while(x<n && y<m){ if(arr1.get(x)>arr2.get(y)){ y++; } else if(arr1.get(x)<arr2.get(y)){ x++; } else{ set.add(arr1.get(x)); x++; y++; } } Collections.sort(set); //System.out.println(set); return set;     } } Thanks For Reading.

Value equal to index value Geeks For Geeks

TIME O(N)   [GFG TIME : 2.6/4.4]  SPACE O(N) [Due To Use Of ArrayList ] class Solution {     ArrayList<Integer> valueEqualToIndex(int arr[], int n) {                      ArrayList<Integer> aa=new ArrayList<Integer>(); if(n==1){ if(arr[0]==1){ aa.add(1); return aa; } else{ aa.add(0); return aa; } } else { for(int i=0;i<n;i++){ if(i==arr[i]-1){ aa.add(i+1); } } } return aa;             } } Thanks for reading.

Common ELements Geeks For Geeks

 class Solution {     ArrayList<Integer> commonElements(int arr1[], int arr2[], int arr3[], int n1, int n2, int n3)      {                   HashSet<Integer> set1=new HashSet<Integer>(); HashSet<Integer> set2=new HashSet<Integer>(); HashSet<Integer> set3=new HashSet<Integer>(); for(int i=0;i<n1;i++){ set1.add(arr1[i]); } for(int i=0;i<n2;i++){ if(set1.contains(arr2[i])){ set2.add(arr2[i]); } } for(int i=0;i<n3;i++){ if(set2.contains(arr3[i])){ set3.add(arr3[i]); } } /*System.out.println("set1: "); for (Integer i : set1){             System.out.println(i); } System.out.println("set2: "); for (Integer i : set2){             System.out.println(i); }*/ Set<Integer> set4 = new TreeSet<Integer>(set3); /*System.out...

Angry Professor HackerRank Algorithms Implementation

TIME O(N) [ArrayList Traversal] SPACE O(1) [Only Constants Are Used]    public   static  String angryProfessor( int  k, List<Integer> aa) {          int  count= 0 ;          for ( int  i= 0 ;i<aa.size();i++){              if (aa.get(i)<= 0 ){                 count++;             }         }          if (count>=k){             // System.out.println("YES");              return   "NO" ;         }  ...

Sparse Arrays HackerRank

1.USING ARRAYLIST import  java.io.*; import  java.math.*; import  java.security.*; import  java.text.*; import  java.util.*; import  java.util.concurrent.*; import  java.util.function.*; import  java.util.regex.*; import  java.util.stream.*; import   static  java.util.stream.Collectors.joining; import   static  java.util.stream.Collectors.toList; public   class  Solution {      public   static   void  main(String[] args)  throws  IOException {         Scanner sc= new  Scanner(System.in);          int  n=sc.nextInt();         String str[]= new  String[n];         ArrayList<String> aa= new  ArrayList<String>();        ...