Professional Documents
Culture Documents
import java.util.ArrayList;
class Main
{
public static void main(String[] args)
{
ArrayList<Integer> number = new ArrayList<>();
number.add(1);
number.add(3);
number.add(5);
System.out.println(number);
ArrayList<Integer> cloneNumber = (ArrayList<Integer>)number.clone();
System.out.println(cloneNumber);
}
}
op= [1,3,5]
[1,3,5]
Methods of ArrayList
Method Description
void add(int index, E element) It is used to insert the specified element at the specified
position in a list.
boolean addAll(Collection<? extends E> It is used to append all of the elements in the specified
c) collection to the end of this list, in the order that they are
returned by the specified collection's iterator.
boolean addAll(int index, Collection<? It is used to append all the elements in the specified
extends E> c) collection, starting at the specified position of the list.
E get(int index) It is used to fetch the element from the particular position
of the list.
Iterator()
listIterator()
int lastIndexOf(Object o) It is used to return the index in this list of the last
occurrence of the specified element, or -1 if the list does
not contain this element.
<T> T[] toArray(T[] a) It is used to return an array containing all of the elements
in this list in the correct order.
boolean contains(Object o) It returns true if the list contains the specified element
int indexOf(Object o) It is used to return the index in this list of the first
occurrence of the specified element, or -1 if the List does
not contain this element.
boolean removeIf(Predicate<? super E> It is used to remove all the elements from the list that
filter) satisfies the given predicate.
protected void removeRange(int It is used to remove all the elements lies within the given
fromIndex, int toIndex) range.
void replaceAll(UnaryOperator<E> It is used to replace all the elements from the list with the
operator) specified element.
void retainAll(Collection<?> c) It is used to retain all the elements in the list that are
present in the specified collection.
E set(int index, E element) It is used to replace the specified element in the list,
present at the specified position.
void sort(Comparator<? super E> c) It is used to sort the elements of the list on the basis of
specified comparator.
List<E> subList(int fromIndex, int It is used to fetch all the elements lies within the given
toIndex) range.
Method Description
boolean add(E e) It is used to append the specified element to the end of a list.
void add(int index, E element) It is used to insert the specified element at the specified
position index in a list.
boolean addAll(Collection<? extends It is used to append all of the elements in the specified
E> c) collection to the end of this list, in the order that they are
returned by the specified collection's iterator.
boolean addAll(int index, It is used to append all the elements in the specified
Collection<? extends E> c) collection, starting at the specified position of the list.
void addFirst(E e) It is used to insert the given element at the beginning of a list.
void addLast(E e) It is used to append the given element to the end of a list.
int indexOf(Object o) It is used to return the index in a list of the first occurrence of
the specified element, or -1 if the list does not contain any
element.
int lastIndexOf(Object o) It is used to return the index in a list of the last occurrence of
the specified element, or -1 if the list does not contain any
element.
ListIterator<E> listIterator(int index) It is used to return a list-iterator of the elements in proper
sequence, starting at the specified position in the list.
boolean offer(E e) It adds the specified element as the last element of a list.
E set(int index, E element) It replaces the element at the specified position in a list with
the specified element.
Object[] toArray() It is used to return an array containing all the elements in a
list in proper sequence (from first to the last element).
<T> T[] toArray(T[] a) It returns an array containing all the elements in the proper
sequence (from first to the last element); the runtime type of
the returned array is that of the specified array.
import java.util.*;
public class Linkedlist
{
public static void main(String args[])
{
LinkedList<String> al=new LinkedList<String>();
al.add("Ravi");
al.add("Vijay");
Iterator<String> itr=al.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
al.add(0,"siva");
System.out.println(al);
al.addFirst("prem");
System.out.println(al);
al.addLast("cherry");
System.out.println(al);
am.clear();
System.out.println(am);
System.out.println(al.contains("siva"));
Iterator x = al.descendingIterator();
while (x.hasNext()) {
System.out.println(x.next());
}
System.out.println(al.element());
System.out.println(al.get(1));
System.out.println(al.getFirst());
System.out.println(al.getLast());
System.out.println(al.indexOf("siva"));
System.out.println(al.lastIndexOf("siva"));
System.out.println(al);
ListIterator itrf = al.listIterator(3);
ListIterator itrb = al.listIterator(4);
while(itrf.hasNext())
System.out.println(itrf.next());
System.out.println(al);
al.offer("satya");
al.offerFirst("bhuvana");
al.offerLast("kali");
System.out.println(al);
System.out.println(al.peek());
System.out.println(al.peekFirst());
System.out.println(al.peekLast());
System.out.println(al.poll());
System.out.println(al.pollFirst());
System.out.println(al.pollLast());
System.out.println(al);
al.remove();
System.out.println(al);
al.remove(4);
System.out.println(al);
al.remove("sai");
System.out.println(al);
System.out.println(al.removeFirst());
}
}
Ravi
Vijay
[siva, Ravi, Vijay]
[siva, Ravi, Vijay, jaanu, teju]
[siva, siva, sai, Ravi, Vijay, jaanu, teju]
[prem, siva, siva, sai, Ravi, Vijay, jaanu, teju]
[prem, siva, siva, sai, Ravi, Vijay, jaanu, teju, cherry]
[]
[siva, sai]
[siva, sai]
true
cherry
teju
jaanu
Vijay
Ravi
sai
siva
siva
prem
prem
siva
prem
cherry
1
2
[prem, siva, siva, sai, Ravi, Vijay, jaanu, teju, cherry]
sai
Ravi
Vijay
jaanu
teju
cherry
Iterating in backward direction from 2nd position
sai
siva
siva
prem
[prem, siva, siva, sai, Ravi, Vijay, jaanu, teju, cherry]
[bhuvana, prem, siva, siva, sai, Ravi, Vijay, jaanu, teju, cherry, satya, kali]
bhuvana
bhuvana
kali
bhuvana
prem
kali
[siva, siva, sai, Ravi, Vijay, jaanu, teju, cherry, satya]
[siva, sai, Ravi, Vijay, jaanu, teju, cherry, satya]
[siva, sai, Ravi, Vijay, teju, cherry, satya]
[siva, Ravi, Vijay, teju, cherry, satya]
Siva
The diagram given below shows a quadrilateral ABCD and the sum of its internal angles. All the
internal angles sum up to 360°.
Thus, ∠A + ∠B + ∠C + ∠D = 360°
There are 5 types of quadrilaterals on the basis of their shape. These 5 quadrilaterals are:
1. Rectangle
2. Square
3. Parallelogram
4. Rhombus
5. Trapezium
Rectangle:
A rectangle is a quadrilateral with four right angles. Thus, all the angles in a rectangle are equal
(360°/4 = 90°). Moreover, the opposite sides of a rectangle are parallel and equal, and diagonals
bisect each other.
Properties of rectangles
Square:
Square is a quadrilateral with four equal sides and angles. It’s also a regular quadrilateral as both its
sides and angles are equal. Just like a rectangle, a square has four angles of 90° each. It can also be
seen as a rectangle whose two adjacent sides are equal.
Properties of a square
For a quadrilateral to be a square, it has to have certain properties. Here are the three properties of
squares:
Parallelogram
A parallelogram, as the name suggests, is a simple quadrilateral whose opposite sides are parallel.
Thus, it has two pairs of parallel sides. Moreover, the opposite angles in a parallelogram are equal and
its diagonals bisect each other.
Properties of parallelogram
If the length of a parallelogram is ‘l’, breadth is ‘b’ and height is ‘h’ then:
1. Perimeter of parallelogram= 2 × (l + b)
2. Area of the parallelogram = l × h
Rhombus
A rhombus is a quadrilateral whose all four sides are equal in length and opposite sides are parallel to
each other. However, the angles are not equal to 90°. A rhombus with right angles would become a
square. Another name for rhombus is ‘diamond’ as it looks similar to the diamond suit in playing cards.
Properties of rhombus
Trapezium
A trapezium (called Trapezoid in the US) is a quadrilateral that has only one pair of parallel sides. The
parallel sides are referred to as ‘bases’ and the other two sides are called ‘legs’ or lateral sides.
Properties of Trapezium
Important quadrilateral formulas
The below table summarizes the formulas on the area and perimeter of different types of
quadrilaterals:
Quadrilateral
Rectangle Square Parallelogram Rhombus Trapezium
formulas
2 × (l + Sum of all
Perimeter 4a 2 × (l + b) 4a
b) the sides
Time conversion:
Import java.util.*;
Public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String time = in.next();
String listTime[] = time.split(":");
String hour = listTime[0];
String minutes = listTime[1];
String secounds = listTime[2].substring(0, 2);
String caser = listTime[2].substring(2, 4);
if(caser.equals("AM"))
{
if(hour.equals("12"))
hour="00";
System.out.println(hour+":"+minutes+":"+secounds);
}
else
{
if(!hour.equals("12"))
{
int h = Integer.parseInt(hour);
h = h +12;
hour =""+h;
}
System.out.println(hour+":"+minutes+":"+secounds);
}
}
}
Find Digits:
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++)
{
int n=sc.nextInt();
int d=0,count=0;
int num=n;
while(n>0)
{
d=n%10;
if(d!=0 && num%d==0)
{
count++;
}
n/=10;
}
System.out.println(count);
}
}
}
Pointers in c:
Like normal variables, pointer variables must be declared before using them. General syntax for
Here, data_type can be any valid C data types and pointer_name can be any valid C identifier.
Making a pointer variable to point other variables by providing address of that variable to the pointer
some valid address and it is important to note that without referencing, pointer variables are
meaningless.
pointer_variable = &normal_variable;
int a=5;
int *ptr;
ptr = &a;
float val=5.5;
float *p;
p = &val;
float x=30.4;
int *iptr;
iptr = &x;
Dereferencing of Pointer
So far, the operator * (star) used in front of the name of the pointer variable is known as pointer or
dereferencing or indirection operator. After valid referencing of pointer variable, *
pointer_variable gives the value of the variable pointed by pointer variable and this is known as
dereferencing of pointer. For the sake of simplicity, *pointer_variable after referencing instructs
compilers that go to the memory address stored by pointer_variable and get value from that memory
address.
int a=5;
int *ptr;
ptr = &a;
printf(“a = %d”, *ptr);
float val=5.5;
float *p;
p = &val;
printf(“val = %f”, *p);
1 decaliter 10 liters
1 decameter 10 meters
1 angstrom 1 x 10-10 meters
1 fermi 1 x 10-15 meters
Mass Conversion
1 decagram 10 gram
The game is simple and is played on a piece of paper. Sumit writes down a long list of names on that
paper and passes it to Rohil. Rohil gets confused on seeing so many names on that paper and asks
Sumit about the game. So, Sumit explains him the rules of the game. Rohil is supposed to partition the
names into groups, such that:
Note: Two strings are called anagrams if it's possible to form one string from the other by changing
the order of its characters.
Rohil would have won the game easily, if he would have been fit and fine but since he is ill right now
he needs your help in winning the game. So, help out Rohil and do give him your blessings.
Input:
The first line contains a single integer N indicating the size of the list. This is followed by N lines where
each line contains a name listed by Sumit.
Output:
In a single line print minimum number of groups in a partition that satisfy above conditions
Constraints:
1<= N <=100
1 <= Length of a name <= 100
Sample Input
6
vinay
vainy
vinit
viint
avinash
aasivnh
Sample Output
3
import java.util.*;
class SumitAndRohil
{
public static void main(String args[] ) throws Exception
{
Scanner in = new Scanner(System.in);
int T = Integer.parseInt(in.nextLine());
String names[] = new String[T];
int grouped[] = new int[T];
int total = 0;
for (int i = 0; i < T; i++)
names[i] = in.nextLine();
for (int i = 0; i < T; i++)
{
if(grouped[i] == 0)
{
grouped[i] = 1;
total++;
char[] chars = names[i].toCharArray();
Arrays.sort(chars);
String str1 = new String(chars);
for (int j = i+1; j < T; j++)
{
if(grouped[j] == 0)
{
if( ( (names[i].charAt(0)) == (names[j].charAt(0)) ) &&
( (names[i].charAt(names[i].length()-1)) ==
(names[j].charAt(names[j].length()-1)) ) )
{
char[] chars2 = names[j].toCharArray();
Arrays.sort(chars2);
String str2 = new String(chars2);
if(str1.equals(str2))
{
grouped[j] = 1;
}
}
}
}
}
}
System.out.println(total);
}
}