0% found this document useful (0 votes)
46 views

Competative Coding Notes

This document discusses Java code examples for working with ArrayLists and arrays. It includes code to: 1. Convert an ArrayList to an array by iterating through the ArrayList and adding each element to the array. 2. Sort an ArrayList in ascending or descending order using the Collections.sort() and Collections.reverse() methods. 3. Find the minimum and maximum elements of an integer array using Collections.min() and Collections.max(). 4. Perform time conversion that takes in a time in 12-hour format and prints it in 24-hour format. 5. Modify an array to calculate the running sum of prior elements in each index.

Uploaded by

Islamic India
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Competative Coding Notes

This document discusses Java code examples for working with ArrayLists and arrays. It includes code to: 1. Convert an ArrayList to an array by iterating through the ArrayList and adding each element to the array. 2. Sort an ArrayList in ascending or descending order using the Collections.sort() and Collections.reverse() methods. 3. Find the minimum and maximum elements of an integer array using Collections.min() and Collections.max(). 4. Perform time conversion that takes in a time in 12-hour format and prints it in 24-hour format. 5. Modify an array to calculate the running sum of prior elements in each index.

Uploaded by

Islamic India
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

ArrayList

ArrayList<Integer> arrli = new ArrayList<Integer>(n);

 arrli.add(i);

arrli.remove(3);

arrli.get(i)
// Java program to convert a ArrayList to an array
// using get() in a loop.
import java.io.*;
import java.util.List;
import java.util.ArrayList;
  
class GFG
{
    public static void main (String[] args)
    {
        List<Integer> al = new ArrayList<Integer>();
        al.add(10);
        al.add(20);
        al.add(30);
        al.add(40);
  
        Integer[] arr = new Integer[al.size()];
  
        // ArrayList to Array Conversion
        for (int i =0; i < al.size(); i++)
            arr[i] = al.get(i);
  
        for (Integer x : arr)
            System.out.print(x + " ");
    }
}

Collections.sort(list);// ArrayList in ascending order


Collections.reverse(list);//ArrayList in descending

// creating Arrays of String type


            String a[] = new String[] { "A", "B", "C", "D" };
  
            // getting the list view of Array
            List<String> list = Arrays.asList(a);
  
            // printing the list
            System.out.println("The list is: " + list);
Integer[] num = { 2, 4, 7, 5, 9 };
  
        // using Collections.min() to find minimum element
        // using only 1 line.
        int min = Collections.min(Arrays.asList(num));
  
        // using Collections.max() to find maximum element
        // using only 1 line.
        int max = Collections.max(Arrays.asList(num));

Hackerrank Time conversion


public static void main(String[] args) {

Scanner scan = new Scanner(System.in);


String time = scan.next();
String tArr[] = time.split(":");
String AmPm = tArr[2].substring(2,4);
int hh,mm,ss;
hh = Integer.parseInt(tArr[0]);
mm = Integer.parseInt(tArr[1]);
ss = Integer.parseInt(tArr[2].substring(0,2));

String checkPM = "PM",checkAM ="AM";


int h = hh;
if(AmPm.equals(checkAM) && hh==12)
h=0;
else if(AmPm.equals(checkPM)&& hh<12)
h+=12;

System.out.printf("%02d:%02d:%02d",h,mm,ss);
}

// Modify array to make each 'i' contain the running sum


of prior elements
for (int i = 1; i < n; i++) {
sum[i] += sum[i - 1];
}
for (int i = m; i < n; i++) {
// If the sum of the piece is equal to 'd'
if (sum[i] - sum[i - m] == d) {
// Increment ways counter
numberOfWays++;
}
}
 >> right shift
class Test {
    public static void main(String args[])  {
       int x = -4;
       System.out.println(x>>1);   
       int y = 4;
       System.out.println(y>>1);   
    }    
}
Output:

-2
2

Sock Merchant HR
mport java.util.*;

class Solution {

public static void main(String[] args) {


Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
HashMap<Integer, Integer> colors = new HashMap<Integer, Integer>();

while(n-- > 0) {
int c = scan.nextInt();
Integer frequency = colors.get(c);

// If new color, add to map


if(frequency == null) {
colors.put(c, 1);
}
else { // Increment frequency of existing color
colors.put(c, frequency + 1);
}
}
scan.close();

// Count and print the number of pairs


int pairs = 0;
for(Integer frequency : colors.values()) {
pairs += frequency >> 1;
}
System.out.println(pairs);
}
}

You might also like