1)
first non repeating character
Malayalam
y
programming
p
class HelloWorld {
public static void main(String[] args) {
String str = "MalayalaM";
char a = '\0';
for (char c : str.toCharArray()) {
if (str.indexOf(c) == str.lastIndexOf(c)) {
a = c;
break;
}
}
System.out.println(a);
}
}
2)
1
##
333
####
55555
class HelloWorld {
public static void main(String[] args) {
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
if(i%2!=0)
{
System.out.print(i);
}
else
{
System.out.print("#");
}
}
System.out.println();
}
}
}
3)
find two elements of array for given target sum
class HelloWorld {
public static void main(String[] args) {
int[] a = {2, 11, 7, 15};
int target = 9;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] + a[j] == target) {
System.out.println(a[i] + ", " + a[j]);
}
}
}
}
}
4)
find union of two arrays
int a[]={1,2,3,4,5};
int b[]={3,2,6,7,8};
output
[1, 2, 3, 4, 5, 6, 7, 8]
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
int a[]={1,2,3,4,5};
int b[]={3,2,6,7,8};
HashSet<Integer> s=new HashSet<Integer>();
for(int i=0;i<5;i++)
{
s.add(a[i]);
}
for(int i=0;i<5;i++)
{
s.add(b[i]);
}
System.out.println(s);
}
}
5)
find intersection of two arrays
int a[]={1,2,3,4,5};
int b[]={3,2,6,7,8};
output
{2,3}
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
int a[]={1,2,3,4,5};
int b[]={3,2,6,7,8};
for(int i=0;i<5;i++)
{
for(int j=0;j<=i;j++)
{
if(a[i]==b[j])
{
System.out.println(a[i]);
}
}
}
}
}
6)Check if a String is a Palindrome
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
String a="mam";
StringBuilder b = new StringBuilder(a);
b.reverse();
if(a.equals(b.toString()))
{
System.out.println("polindrome");
}
else
{
System.out.println("not polindrome");
}
}
}
7)remove duplicate in array
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
String a="sabari";
char c[]=a.toCharArray();
LinkedHashSet<Character> d=new LinkedHashSet<Character>();
for(char c1:c)
{
d.add(c1);
}
StringBuilder sb = new StringBuilder();
for (Character c2 : d) {
sb.append(c2);
}
System.out.println(sb);
}
}
8)
Given an array A[] and a number x, check for pair in A[] with sum as x.
Input
{2, 11, 7,3,6, 15}
output
2, 7
3, 6
total count 2
9)
first occurrence of a string in another string
a = "Hello, how are you?";
b = "how";
First occurrence of how is at index: 7
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
String a = "Hello, how are you?";
String b = "how";
int index = a.indexOf(b);
if (index != -1) {
System.out.println("First occurrence of "+ b +" is at index: " + index);
} else {
System.out.println(" not found.");
}
}
}
10)Shuffle array
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
int a[]={1,2,3,4,5,6};
ArrayList<Integer> al=new ArrayList<Integer>();
for(int i=0;i<a.length;i++)
{
al.add(a[i]);
}
Collections.shuffle(al);
System.out.println(al);
}
}
11)Number of occurrence
X = 2
Arr[] = {1, 1, 2, 2, 2, 2, 3}
Output: 4
Explanation: 2 occurs 4
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
int a[] = {1, 1, 2, 2, 2, 2, 3};
int x=2,count=0;
for(int i=0;i<a.length;i++)
{
if(a[i]==x)
{
count++;
}
}
System.out.println(x+" occurs "+count);
}
}
Input:
use abc;
CREATE TABLE Worker (
WORKER_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
FIRST_NAME CHAR(25),
LAST_NAME CHAR(25),
SALARY INT(15),
JOINING_DATE DATETIME,
DEPARTMENT CHAR(25)
);
INSERT INTO Worker
(WORKER_ID, FIRST_NAME, LAST_NAME, SALARY, JOINING_DATE, DEPARTMENT) VALUES
(001, 'Monika', 'Arora', 100000, '21-02-20 09.00.00', 'HR'),
(002, 'Niharika', 'Verma', 80000, '21-06-11 09.00.00', 'Admin'),
(003, 'Vishal', 'Singhal', 300000, '21-02-20 09.00.00', 'HR'),
(004, 'Amitabh', 'Singh', 500000, '21-02-20 09.00.00', 'Admin'),
(005, 'Vivek', 'Bhati', 500000, '21-06-11 09.00.00', 'Admin'),
(006, 'Vipul', 'Diwan', 200000, '21-06-11 09.00.00', 'Account'),
(007, 'Satish', 'Kumar', 75000, '21-01-20 09.00.00', 'Account'),
(008, 'Geetika', 'Chauhan', 90000, '21-04-11 09.00.00', 'Admin');
Q. Write a SQL query to find the 3th highest salary from employee table?
SELECT DISTINCT salary
FROM worker
ORDER BY salary DESC
LIMIT 1 OFFSET 2;
Q. SQL queries
-- 2> Write a SQL query to find top n records?
-- Example: finding top 5 records from employee table
SELECT *
FROM worker
ORDER BY salary DESC
LIMIT 5;
-- 3> Write a SQL query to find the count of employees working in department 'Admin'
select count(*) from worker where department='Admin'
-- 4> Write a SQL query to fetch department wise count employees sorted by department count in desc order.
SELECT department, COUNT(*) AS empcount
FROM worker
GROUP BY department
ORDER BY empcount DESC;
-- 5> Write a SQL query to find only odd rows from employee table
SELECT *
FROM worker
WHERE worker_id % 2 <> 0;
6> Write a SQL query to show the last record from a table.
SELECT *
FROM worker
order by worker_id desc limit 1;
-- 7> Write a SQL query to show the first record from a table.
SELECT *
FROM worker
limit 1;
-- 8> Write a SQL query to get last five records from a employee table.
SELECT *
FROM worker
order by worker_id desc limit 5;
-- 9> Write a SQL query to find employees having the highest salary in each department.
select * from worker where salary in (select max(salary) from worker group by department )
-- 10> Write a SQL query to fetch departments along with the total salaries paid for each of them.
select department,sum(salary) from worker group by department
-- 11> write a SQL query to find employees with same salary
SELECT e1.*
FROM worker e1
JOIN worker e2 ON e1.salary = e2.salary
WHERE e1.worker_id!=e2.worker_id;
No comments:
Post a Comment