You are on page 1of 24

Make a copy of ArrayList

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 add(E e) It is used to append the specified element at the end of 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.

void clear() It is used to remove all of the elements from this list.

void ensureCapacity(int It is used to enhance the capacity of an ArrayList instance.


requiredCapacity)

E get(int index) It is used to fetch the element from the particular position
of the list.

boolean isEmpty() It returns true if the list is empty, otherwise false.

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.

Object[] toArray() It is used to return an array containing all of the elements


in this list in the correct order.

<T> T[] toArray(T[] a) It is used to return an array containing all of the elements
in this list in the correct order.

Object clone() It is used to return a shallow copy of an ArrayList.

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.

E remove(int index) It is used to remove the element present at the specified


position in the list.

boolean remove(Object o) It is used to remove the first occurrence of the specified


element.

boolean removeAll(Collection<?> c) It is used to remove all the elements from the list.

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.

Spliterator<E> spliterator() It is used to create spliterator over the elements in a list.

List<E> subList(int fromIndex, int It is used to fetch all the elements lies within the given
toIndex) range.

int size() It is used to return the number of elements present in the


list.
void trimToSize() It is used to trim the capacity of this ArrayList instance to
be the list's current size.

Methods of Java LinkedList

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.

void clear() It is used to remove all the elements from a list.

Object clone() It is used to return a shallow copy of an ArrayList.

boolean contains(Object o) It is used to return true if a list contains a specified element.

Iterator<E> descendingIterator() It is used to return an iterator over the elements in a deque in


reverse sequential order.

E element() It is used to retrieve the first element of a list.

E get(int index) It is used to return the element at the specified position in a


list.

E getFirst() It is used to return the first element in a list.

E getLast() It is used to return the last element in 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.

boolean offerFirst(E e) It inserts the specified element at the front of a list.

boolean offerLast(E e) It inserts the specified element at the end of a list.

E peek() It retrieves the first element of a list

E peekFirst() It retrieves the first element of a list or returns null if a list is


empty.

E peekLast() It retrieves the last element of a list or returns null if a list is


empty.

E poll() It retrieves and removes the first element of a list.

E pollFirst() It retrieves and removes the first element of a list, or returns


null if a list is empty.

E pollLast() It retrieves and removes the last element of a list, or returns


null if a list is empty.

E pop() It pops an element from the stack represented by a list.

void push(E e) It pushes an element onto the stack represented by a list.

E remove() It is used to retrieve and removes the first element of a list.

E remove(int index) It is used to remove the element at the specified position in a


list.

boolean remove(Object o) It is used to remove the first occurrence of the specified


element in a list.

E removeFirst() It removes and returns the first element from a list.

boolean It is used to remove the first occurrence of the specified


removeFirstOccurrence(Object o) element in a list (when traversing the list from head to tail).

E removeLast() It removes and returns the last element from a list.

boolean It removes the last occurrence of the specified element in a


removeLastOccurrence(Object o) list (when traversing the list from head to tail).

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.

int size() It is used to return the number of elements in a list.

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);

LinkedList<String> am=new LinkedList<String>(Arrays.asList("jaanu","teju"));


al.addAll(am);
System.out.println(al);

LinkedList<String> an=new LinkedList<String>(Arrays.asList("siva","sai"));


al.addAll(1,an);
System.out.println(al);

al.addFirst("prem");
System.out.println(al);

al.addLast("cherry");
System.out.println(al);

am.clear();
System.out.println(am);

LinkedList s = new LinkedList();


s = (LinkedList) an.clone();
System.out.println(an);
System.out.println(s);

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("Iterating in backward direction from 2nd position");


while(itrb.hasPrevious())
System.out.println(itrb.previous());

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

 Area of a Circle = A = π×r2

 Circumference of a Circle = A = 2πr


Where, r = Radius of the Circle

 Surface Area of a Cube = S = 6a2


Where, a = Length of the sides of a Cube

 Curved surface area of a Cylinder  = 2πrh


 Total surface area of a Cylinder = 2πr(r + h)
 Volume of a Cylinder = V = πr2h
Where, r = Radius of the base of the Cylinder; h = Height of the Cylinder

 Curved surface area of a cone =  πrl


 Total surface area of a cone = πr(r+l) = πr[r+√(h 2+r2)]
 Volume of a Cone = V = ⅓×πr2h
Where, r = Radius of the base of the Cone, h = Height of the Cone

 Surface Area of a Sphere = S = 4πr2


 Volume of a Sphere = V = 4/3×πr3
Where, r = Radius of the Sphere
Quadrilateral:
In Euclidean geometry, a quadrilateral is a four-sided 2D figure whose sum of internal angles is 360°.
The word quadrilateral is derived from two Latin words ‘quadri’ and ‘latus’ meaning four and side
respectively. Therefore, identifying the properties of quadrilaterals is important when trying to
distinguish them from other polygons.
So, what are the properties of quadrilaterals? There are two properties of quadrilaterals:

 A quadrilateral should be closed shape with 4 sides


 All the internal angles of a quadrilateral sum up to 360°

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°

Different types of quadrilaterals

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

A rectangle has three properties:

 All the angles of a rectangle are 90°


 Opposite sides of a rectangle are equal and Parallel
 Diagonals of a rectangle bisect each other

Rectangle formula – Area and perimeter of a rectangle


If the length of the rectangle is L and breadth is B then,

1. Area of a rectangle = Length × Breadth or L × B


2. Perimeter of rectangle = 2 × (L + B)

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:

 All the angles of a square are 90°


 All sides of a square are equal and parallel to each other
 Diagonals bisect each other perpendicularly

Square formula – Area and perimeter of a square

If the side of a square is ‘a’ then,

1. Area of the square = a × a = a²


2. Perimeter of the square = 2 × (a + a) = 4a

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

A quadrilateral satisfying the below-mentioned properties will be classified as a parallelogram. A


parallelogram has four properties:

 Opposite angles are equal


 Opposite sides are equal and parallel
 Diagonals bisect each other
 Sum of any two adjacent angles is 180°

Parallelogram formulas – Area and perimeter of a 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

A rhombus is a quadrilateral which has the following four properties:

 Opposite angles are equal


 All sides are equal and, opposite sides are parallel to each other
 Diagonals bisect each other perpendicularly
 Sum of any two adjacent angles is 180°

Rhombus formulas – Area and perimeter of a rhombus

If the side of a rhombus is a then, perimeter of a rhombus = 4a


If the length of two diagonals of the rhombus is d1 and d2 then the area of a rhombus = ½ × d1 × d2

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

A trapezium is a quadrilateral in which the following one property:

 Only one pair of opposite sides are parallel to each other

Trapezium formulas – Area and perimeter of a trapezium

If the height of a trapezium is ‘h’ (as shown in the above diagram) then:

1. Perimeter of the trapezium= Sum of lengths of all the sides = AB + BC + CD + DA


2. Area of the trapezium = ½ × (Sum of lengths of parallel sides) × h = ½ × (AB + CD) × h

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

½ × d1 × ½ × (Sum


Area l×b a² l×h
d2 of parallel
sides) ×
height

2 × (l + Sum of all
Perimeter 4a 2 × (l + b) 4a
b) the sides

Find all prime numbers from an array:


import java.util.*;
class Imp
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n=in.nextInt();
int[] a = new int[n];
for (int i = 0; i < 5; i++)
{
a[i] = in.nextInt();
}
for (int i = 0; i < a.length; i++)
{
boolean isPrime = true;
if (a[i] == 1)
isPrime = false;
else
{
for (int j = 2; j <= a[i] / 2; j++)
{
if (a[i] % j == 0)
{
isPrime = false;
break;
}
}
}
if (isPrime)
System.out.print(array[i] + " ");
}
}
}
Ip= 5
34567
Op= 3 5 7

Print character frequency order occurrence.


import java.util.*;
class Gfg
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
Map<Character, Integer> d = new HashMap<Character, Integer>();
for(int i = 0; i < s.length(); i++)
{
if(d.containsKey(s.charAt(i)))
{
d.put(s.charAt(i), d.get(s.charAt(i)) + 1);
}
else
{
d.put(s.charAt(i), 1);
}
}
for(int i = 0; i < s.length(); i++)
{
if(d.get(s.charAt(i)) != 0)
{
System.out.print(s.charAt(i));
System.out.print(d.get(s.charAt(i)) + " ");
d.put(s.charAt(i), 0);
}
}
}
}
(or)
import java.util.*;
class Least
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String s=in.nextLine();
int cnt[]=new int[26];
for (int i = 0; i < s.length(); i++)
cnt[s.charAt(i) - 'a']++;
for (int i = 0; i < s.length(); i++)
if (cnt[s.charAt(i) - 'a'] != 0)
{
System.out.print(s.charAt(i));
System.out.print(cnt[s.charAt(i) - 'a'] + " ");
cnt[s.charAt(i) - 'a'] = 0;
}
}
}
(or)
import java.util.*;
public class Fre
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
int[] freq = new int[str.length()];
int i, j;
char string[] = str.toCharArray();
for(i = 0; i <str.length(); i++)
{
freq[i] = 1;
for(j = i+1; j <str.length(); j++)
{
if(string[i] == string[j])
{
freq[i]++;
string[j] = '0';
}
}
}
for(i = 0; i <freq.length; i++)
{
if(string[i] != ' ' && string[i] != '0')
System.out.print(string[i] + "" + freq[i] +" ");
}
}
}
Ip= library
Op= l1 i1 b1 r2 a1 y1

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);
}
}
}

Convert the nxn matrix into upper triangular matrix.


import java.util.*;
class Cut
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[][]=new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
for(int i=1;i<n;i++)
{
for(int j=0;j<i;j++)
{
if(a[i][j]!=0)
{
a[i][j]=0;
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(a[i][j]+ " ");
}
System.out.println();
}
}
}
Ip: 4
2 3 4 5678901234567
Op: 2 3 4 5
0 7 8 9
0 0 2 3
0 0 0 7

Boundary elements of a matrix:


import java.util.*;
class Cut
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
int n=sc.nextInt();
int a[][]=new int[m][n];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if (i == 0)
System.out.print(a[i][j] + " ");
else if (i == m - 1)
System.out.print(a[i][j] + " ");
else if (j == 0)
System.out.print(a[i][j] + " ");
else if (j == n - 1)
System.out.print(a[i][j] + " ");
else
System.out.print(" ");
}
System.out.println("");
}
}
}
Ip: 4
4
2345678901234567
Op: 2345
6 9
0 3
4567

Place 0 in all places except at the boundary places in nxn matrix:


import java.util.*;
class Cut
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
int n=sc.nextInt();
int a[][]=new int[m][n];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if (i == 0)
System.out.print(a[i][j] + " ");
else if (i == m - 1)
System.out.print(a[i][j] + " ");
else if (j == 0)
System.out.print(a[i][j] + " ");
else if (j == n - 1)
System.out.print(a[i][j] + " ");
else
System.out.print("0 ");
}
System.out.println("");
}
}
}
Ip: 4
4
2345678901234567
Op: 2345
6009
0003
4567
Print only non boundary and corner elements of an nxn matrix:
import java.util.*;
class Ccut
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
int n=sc.nextInt();
int a[][]=new int[m][n];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
boolean visible = (i % (m - 1) == 0) == (j % (n - 1) == 0);
if (visible)
{
System.out.printf(" %4d", a[i][j]);
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Ip: 4
4
2345678901234567
Op: 2 5
7 8
1 2
4 7

Sum of boundary elements in the matrix:


import java.util.*;
class Cut
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
int n=sc.nextInt();
int a[][]=new int[m][n];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
int sum=0;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (i == 0)
sum += a[i][j];
else if (i == m - 1)
sum += a[i][j];
else if (j == 0)
sum += a[i][j];
else if (j == n - 1)
sum += a[i][j];
}
}
System.out.println(sum);
}
}
Ip: 4
4
2345678901234567
Op: 54

Sum of non-boundary elements of a matrix:


import java.util.Scanner;
public class Cut
{
public static void main(String[] args)
{
int sum=0;
Scanner sc=new Scanner(System.in);
int r=sc.nextInt();
int c=sc.nextInt();
int a[][]=new int[r][c];
for(int i=0;i< r; i++)
{
for(int j=0;j< c;j++)
{
a[i][j]=sc.nextInt();
}
}
for(int i=1;i< r-1;i++)
{
for(int j=1;j< c-1;j++)
{
sum=sum+a[i][j];
}
}
System.out.println(sum);
}
}
Ip: 4
4
Op: 2345678901234567
18

Print non-boundary elements of the matrix:


import java.util.Scanner;
public class Cut
{
public static void main(String[] args)
{
int sum=0;
Scanner sc=new Scanner(System.in);
int r=sc.nextInt();
int c=sc.nextInt();
int a[][]=new int[r][c];
for(int i=0;i< r; i++)
{
for(int j=0;j< c;j++)
{
a[i][j]=sc.nextInt();
}
}
for(int i=1;i< r-1;i++)
{
for(int j=1;j< c-1;j++)
{
System.out.print(a[i][j]+ " ");
}
System.out.println();
}
}
}
Ip: 4
4
2345678901234567
Op: 78
12

Convert the boundary elements into 1 in the matrix:


import java.util.*;
class Cut
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
int n=sc.nextInt();
int a[][]=new int[m][n];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (i == 0)
System.out.print(1 + " ");
else if (i == m - 1)
System.out.print(1 + " ");
else if (j == 0)
System.out.print(1 + " ");
else if (j == n - 1)
System.out.print(1 + " ");
else
System.out.print(" ");
}
System.out.println("");
}
}
}
Ip: 4
4
2345678901234567
Op: 1111
1 1
1 1
1111

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:

Pointer is an important feature of C programming language. It is considered as the beauty of C


programming language making this language more powerful and robust.
A pointer is a special variable in C programming language which stores the memory address of other
variables of the same data type. As a pointer is variable, it is also created in some memory location.
Declaration of Pointer Variable

Like normal variables, pointer variables must be declared before using them. General syntax for

declaring pointer variable is:


data_type * pointer_name;

Here, data_type can be any valid C data types and pointer_name can be any valid C identifier.

Examples of Declaration of Pointer:


1. int *ptr; Here ptr is a pointer variable and it is read as a pointer to integer since it can point to
integer variables.
2. float *fptr; Here fptr is a pointer variable and it is read as a pointer to float since it can point to
float variables.
3. char *cp; Here cp is a pointer variable and it is read as a pointer to character since it can point
to character variables.

Referencing of Pointer (Initialization of Pointer)

Making a pointer variable to point other variables by providing address of that variable to the pointer

is known as referencing of pointer.


It is also known as initialization of pointers. For proper use of pointer, pointer variables must point to

some valid address and it is important to note that without referencing, pointer variables are

meaningless.

General syntax for referencing of pointer is:

pointer_variable = &normal_variable;

Here pointer_variable and normal_variable must be of the same data types.

Examples of Referencing of Pointer:

int a=5;
int *ptr;
ptr = &a;

Here pointer ptr got address of variable a so, pointer ptr is now pointing to variable a.

float val=5.5;
float *p;
p = &val;

Here pointer p got address of variable val so, pointer p is now pointing to variable val.

float x=30.4;
int *iptr;
iptr = &x;

is invalid!!! Because pointer iptr cannot store address of float variable.

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.

Examples of Dereferencing of Pointer:

int a=5;
int *ptr;
ptr = &a;
printf(“a = %d”, *ptr);

This prints a = 5 which is accessed through pointer.

float val=5.5;
float *p;
p = &val;
printf(“val = %f”, *p);

This prints val = 5.5 which is accessed through pointer. .

Volume Unit Conversion


1 milliliter 0.001 liter

1 centiliter 0.01 liter

1 deciliter 0.1 liter

1 decaliter 10 liters

1 hectoliter 100 liters

1 kiloliter 1000 liters

1 cubic inch 1.639 × 10 – 2 liters

1 gallon 3.785 liters

1 cubic foot 28.316 liters

Length Unit Conversion

1 millimeter 0.001 meter

1 centimeter 0.01 meter

1 decimeter 0.1 meter

1 decameter 10 meters

1 hectometer 100 meters

1 kilometer 1000 meters

1 inch 2.54 × 10−2 meters

1 foot 0.3048 meters

1 angstrom 1 x 10-10 meters

1 fermi 1 x 10-15 meters

1 light year 0.946 × 1016 meters

1 mile 1.609344 kms

Mass Conversion

1 milligram 0.001 gram

1 centigram 0.01 gram

1 decigram 0.1 gram

1 decagram 10 gram

1 hectogram 100 gram


1 kilogram 1000 grams

1 stone 6350.29 grams

1 pound 453.592 grams

1 ounce 28.3495 grams

Kilo  hecto  deca Unit deci centi milli

103 102 101 100 10-1 10-2 10–3

Sumit and Rohil:


It's a fine sunny afternoon today in California. Looking at the pleasant weather, Sumit is all ready to
go out and play with his friend Rohil. Unfortunately, Rohil is down with fever. Seeing that his friend is
ill, Sumit decides not to go out - instead play with Rohil inside the house. Sumit loves math, on the
contrary Rohil loves strings. Sumit decides to play a game that involves more of strings and less of
Maths so that Rohil could be at ease and have fun playing it.

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:

 Each name belongs to exactly one group.


 Names that belong to the same group are pairwise anagrams.
 The first character of all the names in the same group are equal.
 The last character of all the names in the same group are equal.
 The number of groups is minimum possible.

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

All names will consist of lowercase English alphabets(a-z).

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);
}
}

You might also like