Wednesday, 4 May 2022

Matrix rotation

Problem statement:

   Rotate the matrix 90 degree in anti clock wise

Input:

1 2 3

4 5 6

7 8 9

Output:

3 6 9

2 5 8

1 4 7


Solution:

        public void matrixRotation() {

int[][] array = { { 1, 2, 3}, { 4, 5, 6},{7,8,9} };

int rowCount = array.length;

int colCount = array[0].length;

for (int i = colCount - 1; i >= 0; i--) {

for (int j = 0; j < rowCount; j++) {

System.out.print(array[j][i] + " ");

}

System.out.println();

}

}

Check vowels in String

 

Problem statement: 

      Please check whether vowels present in the given String or not

Sample input1: india

Sample output1: Vowels present in the String

Sample input2: bbc

Sample output2: Vowels not present in the string

     

Solution:

        public void checkVowelsInString() {

System.out.println("checkVowelsInString:");

System.out.println("-----------------------");

String input="sssss";

if(input.matches(".*[aeiou].*")) {

System.out.println("Vowels present in string");

}

else {

System.out.println("Vowels not present in string");

}

System.out.println("==============================================\n\n");

}


Output: Vowels not present in the string


Linked list implementation with Java

 Linked list:


Implement linked list and do the following operations,
  • Adding new element in front
  • Adding new element at the end
  • Adding new element in the middle
  • Find data from the list
  • Delete element from the list   
  •  Print all the elements in the list


public class LinkedList {

Node head;

Node tail;

class Node{

Node next;

int data;

Node(int d){

data=d;

}

}

public void addDataAtFrond(int d) {

Node node=new Node(d);

Node tmp=head;

if(tmp==null) {

head=node;

}

else {

node.next=head;

head=node;

}

}

public void addDataAtEnd(int d) {

Node node=new Node(d);

Node tmp=tail;

if(tmp==null) {

head=node;

tail=node;

}

else {

tmp.next=node;

tail=node;

}

}

public void findElement(int d) {

Node tmp=head;

while(tmp!=null) {

if(tmp.data==d) {

System.out.println(d+" is available in list");

return;

}

tmp=tmp.next;

}

System.out.println(d+" is not available in list");

}

public void deleteElement(int d) {

Node tmp=head;

Node tmpprev=null;

while(tmp!=null) {

if(tmp.data==d) {

tmpprev.next=tmp.next;

}

tmpprev=tmp;

tmp=tmp.next;

}

}

public void addDataAtMiddle(int d,int v) {

Node node=new Node(v);

Node tmp=head;

if(tmp==null) {

head=node;

tail=node;

}

else {

while(tmp!=null) {

if(tmp.data==d) {

Node tmpNext=tmp.next;

tmp.next=node;

node.next=tmpNext;

break;

}

tmp=tmp.next;

}

}

}

public void print() {

Node temp=head;

while(temp!=null) {

System.out.println(temp.data);

temp=temp.next;

}

}


public static void main(String[] args) {

LinkedList obj=new LinkedList();

obj.addDataAtEnd(10);

obj.addDataAtEnd(20);

obj.addDataAtEnd(30);

obj.addDataAtMiddle(20,25);

obj.print();

System.out.println("==============================");

obj.findElement(20);

obj.deleteElement(30);

System.out.println("==============================");

obj.print();

System.out.println("==============================");

obj.findElement(20);

}


}

Tuesday, 3 May 2022

Check whether given strings are anagram with each other

Problem statement: We have two strings  "abc" & "bac" and verify whether both string have same alphabets in any order


Sample input1:

"abc" & "bac"

Output1: 

Anagram


Sample input2:

"abc" & "aba"

Output2: 

Not Anagram

public void checkAnagram() {

  System.out.println("checkAnagram:");

  System.out.println("-------------");

  String input1="aba",input2="baa";

  Map<Character, Integer> map1=new HashMap<Character, Integer>();

  Map<Character, Integer> map2=new HashMap<Character, Integer>();

  if(input1.length()==input2.length()) {

  for(int i=0;i<input1.length();i++) {

if(!map1.containsKey(input1.charAt(i))) {

map1.put(input1.charAt(i), 1);

}

else {

map1.put(input1.charAt(i),map1.get(input1.charAt(i))+1);

}

if(!map2.containsKey(input2.charAt(i))) {

map2.put(input2.charAt(i), 1);

}

else {

map2.put(input2.charAt(i), map2.get(input2.charAt(i))+1);

}

  }

  if(map1.equals(map2)) {

System.out.println("Anagram");

  }

  else {

  System.out.println("Not Anagram");

  }

 }

  else {

  System.out.println("Not Anagram");

  }

}


Output: 

Anagram

Top Engineering colleges in Tamilnadu

 Top Engineering colleges in Tamilnadu


IITChennai
Anna University Guindi CampusChennai
NITTrichy
SASTRAThanjavur
SSNChennai
AMRITACoimbatore
SRMChennai
SATYABHAMAChennai
VITVellore
HINDUSTANChennai
MITChennai
KALASALINGAMMadurai
KumaraguruCoimbatore
THIYAGARAJARMadurai
Coimbatore Institute of TechnologyCoimbatore
Sri Krishna College of EngineeringCoimbatore
Mepco schlenk engineeringSivakasi
Kovilpatti national engineering collegeKovilpatti
Sri Sairam Engineering collegesChennai
Pannari AmmanCoimbatore
Velammal Engineering collegeChennai
KarunyaCoimbatore
KARPAGAM COLLEGE OF ENGINEERINGCoimbatore
Kongu Engineering collegeErode
Karpagam College of EngineeringCoimbatore

Rearrange the numbers

 Rearrange the numbers

Problem statement: We have an integer array {9,-5,7,0,-2,-1,8,-7} and rearrange this array by moving all positive numbers in left side and negative numbers in right side with numbers in given order


Sample input:

{9,-5,7,0,-2,-1,8,-7}

Output: 

{9,7,0,8,-5,-2,-1,-7}

        public void reArrangIntegerArray() {

   int[] array = { 9, -5, 7, 0, -2, -1, 8, -7 };

   for (int i = 0; i < array.length; i++) {

for (int j = 1; j < array.length; j++) {

    if (array[j - 1] < 0 

                       && array[j] >= 0) {

int temp = array[j - 1];

array[j - 1] = array[j];

array[j] = temp;

}

    }

}


for (int a : array) {

System.out.println(a);

}

}


Output: 

{9,7,0,8,-5,-2,-1,-7}

Fibonacci Series

 

Fibonacci Series


        public void fibonacciNumberSeries() {

//0,1,1,2,3,5,8,13,21,34,55,89

int n=100;

int f0=0,f1=1,f2=0;

List<Integer> listFibonacciSeries=new ArrayList<Integer>();

listFibonacciSeries.add(f0);

listFibonacciSeries.add(f1);

while(f2<n) {

    f2=f0+f1;

    f0=f1;

    f1=f2;

    if(f2<100) {

   listFibonacciSeries.add(f2);

}

                }

System.out.println(listFibonacciSeries);

}


Output:0,1,1,2,3,5,8,13,21,34,55,89

Switch case in Java

 Problem statement: Return the capital of a state based on input state          public String getCapital(String state){               switch...