Core Java Programs for Class XII A
Core Java Programs for Class XII A
COMPUTER
PROJECTTOPIC:
Core Java Programs on -
Object Passing
Arrays (SDA & DDA)
Strings
Inheritance
Stack & Queue
4 STRINGS 52-63
5 INHERITENCE 64-85
Data Members:
Member Functions:
2. void getval() →
4. void display() →
o Print x
o Print y
import [Link].*;
class op1{
int x,y;
double dis;
op1() {
x=0;y=0;dis=0.0;
}
void getval() {
Scanner in=new Scanner ([Link]);
x=[Link](); y=[Link]();
}
void calradius(op1 obj1,op1 obj2) {
dis=[Link]([Link]((obj1.x-obj2.x),2)+[Link]((obj1.y-
obj2.y),2));
[Link]("DISTANCE BETWEEN POINTS");
[Link](dis);
}
void display() {
[Link](x);
[Link](y);
}
public static void main(String[] args) {
op1 ox=new op1();
[Link]("enter x&y of 1st point");
[Link]();
op1 oy=new op1();
[Link]("enter x&y of 2nd point");
[Link]();
[Link](ox,oy);
}
}
OUTPUT :
Question 2:
Main Class: op2
Data Members:
Member Functions:
1. op2(String name, int maths, int physics, int chemistry) → parameterized constructor
3. void display() →
[Link].*;
publicclass op2 {
String name;
intmaths, physics, chemistry;
op2(String name, int maths, int physics, int chemistry) {
[Link] = name;
[Link] = maths;
[Link] = physics;
[Link] = chemistry;
}
static double calculateAverage(op2 m) {
double average = ([Link] + [Link] + [Link]) / 3.0;
return average;
}
void display() {
[Link]("%s Average: %.2f\n", name,
calculateAverage(this));
}
public static void main(String[] args) {
Scanner in=new Scanner([Link]);
[Link]("name of 1st person: ");
String a=[Link]();
[Link]("marks in physics , chemistry and maths: ");
int p=[Link]();
int c=[Link]();
int m=[Link]();
op2 student1 = new op2(a, m,p,c);
[Link]("name of 2nd person: ");
String a1=[Link]();
[Link]("marks in physics , chemistry and maths: ");
int p1=[Link]();
int c1=[Link]();
int m1=[Link]();
op2 student2 = new op2(a1, m1,p1,c1);
[Link]("Student 1 Average: " +
calculateAverage(student1));
[Link]("Student 2 Average: " +
calculateAverage(student2));
[Link]("\nUsing display method:");
[Link]();
[Link]();
}
}
OUTPUT :
Question 3:
Main Class: op3
Data Members:
Member Functions:
o Return salary
import [Link].*;
class op3 {
String empId;
double salary;
public op3(String empId, double salary) {
[Link] = empId;
[Link] = salary;
}
public double getSalary() {
return salary;
}
publicString toString() {
return "ID = " + empId + ", Salary = " + salary;
}
publicstatic op3 compareSalary(op3 e1, op3 e2) {
if([Link]() > [Link]()) {
return e1;
}else {
return e2;
}
}
publicstatic void main(String[] args) {
Scanner in=new Scanner([Link]);
[Link]("1st employee name and salary"); String
a=[Link](); intb=[Link](); op3emp1 = new op3(a,b);
[Link]("2nd employee name and salary");
String c=[Link](); intd=[Link](); op3emp2 = new op3(c,
d);
[Link]("op3 1: " + emp1);
[Link]("op3 2: " + emp2);
op3higherPaid = compareSalary(emp1, emp2);
[Link]("op3 with higher salary: " + higherPaid);
}
}
OUTPUT :
Question4 :
Main Class: op4
Data Members:
Member Functions:
o Return balance
o If amount > 0:
balance += amount
o [Link](amount)
o If true→call [Link](amount)
o Returntrue
o Else→return false
import [Link].*;
class op4 {
String accountNumber;
double balance;
op4(String accountNumber, double balance) {
[Link] = accountNumber;
[Link] = balance;
}
public double getBalance() {
return balance;
} public boolean withdraw(double amount)
{ if (amount > 0 && amount <= balance) {
balance -= amount;
return true;
}
return false;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public String toString() {
return accountNumber + ": " + balance;
}
public static boolean transferFunds(op4 source, op4 destination,
double amount) {
if ([Link](amount)) {
[Link](amount);
return true;
}
return false;
}
public static void main(String[] args) {
Scanner in=new Scanner([Link]);
[Link]("name and balance of sender");
String a=[Link]();
int ba=[Link]();
op4 account1 = new op4(a, ba);
[Link]("name and balance of reciever");
String a1=[Link]();
int ba1=[Link]();
op4 account2 = new op4(a1, ba1);
[Link]("Initial Balances:");
[Link]("Account " + account1);
[Link]("Account " + account2);
[Link]("amount to transfer");
double transferAmount = [Link]();
boolean transferSuccess = transferFunds(account1, account2,
transferAmount);
[Link]("Transfer " + transferAmount + " from A123 to
B456: " +
(transferSuccess ? "Successful" : "Failed"));
[Link]("Updated Balances:");
[Link]("Account " + account1);
[Link]("Account " + account2);
}
}
OUTPUT :
Question5 :
Class Name: op5
Data Members:
int m → Marks in Mathematics
int s → Marks in Science (Physics/Chemistry – as per input prompt)
int e → Marks in English (or third subject – as per input)
Member Functions:
ALGORITHM : Step 1: Start the program. Step 2: Define class op5 with
integer variables m, s, e. Step 3: Create constructor to set m, s, e using this
keyword. Step 4: Define getPercentage method to return (m + s + e)
divided by 3.0. Step 5: Define static calculateClassAverage method taking
op5 array. Step 6: Initialize sum to 0. Step 7: Loop through each student in
array. Step 8: Add student's getPercentage to sum. Step 9: Return sum
divided by array length. Step 10: In main, create Scanner object. Step 11:
Print input prompt for marks. Step 12: For first student, print label and
read three marks into a, b, c. Step 13: For second student, print label and
read into a1, b1, c1. Step 14: For third student, print label and read into a2,
b2, c2. Step 15: For fourth student, print label and read into a3, b3, c3.
Step 16: Create op5 array with four student objects using read marks.
Step 17: Loop from i=0 to 3.
Step 18: Print student number and their percentage.
Step 19: Call calculateClassAverage on array and print result.
Step 20: End the program.
SOURCE CODE :
import [Link].*;
public class op5 {
int m, s, e;
op5(int m, int s, int e) {
this.m = m;
this.s = s;
this.e = e;
}
double getPercentage() {
return (m + s + e) / 3.0;
}
static double calculateClassAverage(op5[] arr) {
double sum = 0;
for (int i = 0; i < [Link]; i++) {
sum += arr[i].getPercentage();}
VARIABLE DESCRIPTION
[Link]. DATA TYPE VARIABLE DESCRIPTION
1. Int m Store physics marks
2. Int s Store chemistry marks
3. Int e Store maths marks
4. Double sum To store sum
5. Int i For loop
6. Int a,a1,a2 To store phy marks
7. Int b,b1,b2 To store chem marks
8. Int c,c1,c2 To store maths marks
OUTPUT :
Question 1: Write a Java program to accept n words in a Single Dimension
Array, and for each word, display the frequency of each uppercase letter in
it.
import [Link].*;
class sda1 {
public static void main(String[] args) {
Scanner in = new Scanner([Link]);
[Link]("Enter size of array");
int n = [Link]();
[Link]();
String[] a = new String[n];
[Link]("Enter words");
for (int i = 0; i < n; i++) {
a[i] = [Link]().toUpperCase().trim();
}
for (int i = 0; i < [Link]; i++) {
String word = a[i];
if ([Link]())
continue;
[Link]("Word: " + word);
int[] freq = new int[26];
for (int j = 0; j < [Link](); j++) {
char c = [Link](j);
if (c >= 'A' && c <= 'Z') freq[c - 'A']++;
}
[Link]("Letter\tFrequency");
for (int p = 0; p < 26; p++) {
if (freq[p] > 0)
[Link]((char)(p + 'A') + "\t" + freq[p]);
}
[Link]();} }}
OUTPUT :
Question2:Givena1Darray of n integers, rearrange it so that all even
numberscomebeforeallodd numbers. Preserve the relative order of even
and oddelements.
import [Link].*;
class sda2 {
publicstaticvoidmain(String[] args) {
Scannerin=newScanner([Link]);
[Link]("Enter size of array");
int n = [Link]();
int[] a = new int[n];
a[i] = [Link]();
int[]result=newint[n];
int idx = 0;
for(inti=0;i<n;i++) {
if(a[i]%2==0)result[idx++] = a[i];
for(inti=0;i<n;i++) {
if(a[i]%2!=0)result[idx++] = a[i];
OUTPUT :
Question 3: Write a Java program to input an array of integers, sort it
using bubble sort, and search for a target element using binary search.
import [Link].*;
class sda3 {
public static void main(String[] args) {
Scanner in=new Scanner([Link]);
[Link]("Enter size of array");
int n=[Link]();
int a[]=new int[n];
[Link]("Enter element of array");
for(int i=0;i<n;i++)
{
a[i]=[Link]();
}
[Link]("Enter element of search");
int target = [Link]();
for (int i = 0; i < n - 1; i++) {
}
return -1; }
}
OUTPUT :
Question 4 : WriteaJavaprogram to input an array of integers and
printthefrequencyofeachelement using a visited array.
import [Link].*;
class sda4
{
publicstaticvoidmain(String[] args) {
Scannerin=newScanner([Link]);
[Link]("Entersize of array");
int n=[Link]();
int a[]=new int[n];
[Link]("Enterelement of array");
for(int i=0;i<n;i++)
{
a[i]=[Link]();
}
boolean[]visited=newboolean[n];
for (int i = 0; i < n; i++) {
if (!visited[i]) {
int count = 1;
for(intj=i+1;j<n;j++) {
if (a[i] == a[j]) {
visited[j] = true;
count++;
}
}
[Link]("Element " + a[i] + " occurs " + count + "
times");
}
}
}
}
OUTPUT :
Question 5 : Write a Java program to input array size, elements, and a
target sum; print all pairs of elements that sum to the target.
ALGORITHM:
SOURCE CODE :
import [Link].*;
class sda5
int n=[Link]();
for(int i=0;i<n;i++)
a[i]=[Link]();
[Link]("target to achieve");
int nn = [Link];
VARIABLE DESCRIPTION
[Link]. DATA TYPE VARIABLE DESCRIPTION
1. Int n To store array size
2. Int a[] To create an array of size n
3. Int target To store target value
4. Int nn To store array length
5. Int i,j For loop
OUTPUT :
Question1 : Write a Java program to input size of square matrix, read
elements, print original matrix, transpose it, and print transposed matrix.
import [Link].*;
class dda1
{
for(intj=0;j< n; j++) {
if(i==j||i+ j == n- 1) {
sum+=a[i][j];
[Link](a[i][j] + "\t");
}
else {
[Link]("\t");
}
}
[Link]();
}
[Link]("Sumofthediagonal elements = " + sum);
intb[]=newint[(n-2)*(n-2)];
int k = 0;
for(inti=1;i<n-1;i++){
for(intj=1;j<n-1;j++){
b[k++] = a[i][j];
}
}
for(inti=0;i<k-1;i++){
for(intj=0;j<k-i-1;j++) {
if (b[j] > b[j + 1]) {
int t = b[j];
b[j] = b[j+1];
b[j+1] = t;
}}}
k=0;
for(inti=1;i<n-1;i++){
for(intj=1;j<n-1;j++){
a[i][j] = b[k++];
}
}
for (int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
[Link](a[i][j] + "\t");
}
[Link]();
}
}
}
} OUTPUT : Enter sizeofthe
matrix 4 Enter elements in
the matrix: 8 9 6 3 4 5 6 7 8
2145
6 7 8 ORIGINAL
MATRIX
8 9 6 3
4 5 6 7
8 2 1 4
56 7 8
DIAGONAL ELEMENTS
8 3
5 6
2 1
5 8
Sum of the diagonal elements = 38
8 9 6 3
4 1 2 7
8 5 6 4
5 6 7 8
Question3 : Write a Java program to input a square matrix (n×n), display it,
convert it to 1D array, sort the 1D array in ascending order using bubble
sort, and display sorted array.
import [Link].*;
public class dda3 {
public static void main(String[] args) {
Scanner in=new Scanner([Link]);
[Link]("enter size nxn");
int n=[Link]();
int a[][]=new int[n][n];
[Link]("enter element in array");
for (int i = 0; i < n; i++) {
for(intj=0;j<[Link]-i-1;j++) {
if (array1D[j] > array1D[j+1]) {
int t = array1D[j];
array1D[j] = array1D[j+1];
array1D[j+1] = t;
}
}
}
for (int k = 0; k < [Link]; k++) {
[Link](array1D[k] + " ");
}
}
}
OUTPUT :
Question4 : Write a Java program to input a square matrix (n×n), display it,
rotate it 90° clockwise in-place, and display the rotated matrix.
import [Link].*;
class dda4 {
public static void main(String[] args)
{
Scanner in=new Scanner([Link]);
[Link]("enter size nxn");
int n=[Link]();
int matrix[][]=new int[n][n];
[Link]("enter element in array");
for (int i = 0; i < n; i++) {
SOURCE CODE :
import [Link].*;
class dda5 {
int n=[Link]();
matrix1[i][j]=[Link]();
matrix2[i][j]=[Link]();
}
[Link]("Matrix 1:");
for(inti=0;i<[Link]; i++) {
[Link]();
[Link]("Matrix 2:");
for(inti=0;i<[Link]; i++) {
[Link]();
int[][]result=multiplyMatrices(matrix1, matrix2);
[Link]("Result matrix:");
for(inti=0;i<[Link]; i++) {
[Link]();
}
}
introws1=[Link];
intcols1=matrix1[0].length;
intcols2=matrix2[0].length; int[]
[]result=newint[rows1][cols2];
for(inti=0;i<rows1; i++) {
for(intj=0;j<cols2; j++) {
result[i][j] = 0;
return result; }}
VARIABLE DESCRIPTION
[Link]. DATA TYPE VARIABLE DESCRIPTION
1. Int n To store array size
2. Int matrix1[][] To create first array
3. Int i,j For loop
4. Int matrix2[][] To create second array
5. Int To store resultant array
result[][]
6. Int Torows1
store no. of 1st matrix row
7. Int Tocols1
store no. of 1st matrix column
8. Int Tocols2
store no. of 2nd matrix column
OUTPUT :
enter size nxn 3 FOR
MATRIX 1 : enter
element in array 8 7 4
5 6 3 2 4 5 FOR
MATRIX 2 : enter
element in array 9 8 7
65432
1 Matrix 1: 8 7
4 5 6 3 2 4 5
Matrix 2: 9 8 7
6 5 4 3 2 1
Result matrix:
126 107 88 90
76 62 57 46 35
Question1 : Write a Java program to input two words, check if they are
anagrams (ignoring case and spaces), and print the result.
import [Link].*;
public class s1
{
}
public static boolean areAnagrams(String s1, String s2) {
s1 = [Link]("\\s", "").toLowerCase();
s2 = [Link]("\\s", "").toLowerCase();
if ([Link]() != [Link]())
return false;
count[[Link](i) - 'a']++;
}
for(inti= 0; i < [Link](); i++) {
count[[Link](i) - 'a']--;
return false;
returntrue;
OUTPUT :
QUESTION2 : Write a program to accept a sentence which may be
terminated by either '.', '?' or '!' only. The words may be separated by more
than one blank space and are in UPPER CASE.
Perform the following tasks:
public class s2
{
int x=0;
[Link]("Enter a sentence");
s1=[Link]().trim();
s=[Link]();
ln=[Link]();
ch=[Link](ln-1);
if(ch=='.'||ch=='?'||ch=='!'){
String ss=[Link](0,ln-1);
a[i]=[Link]();
for(i=0;i<c;i++) {
l=='O'||l=='U'||l=='I'))
{[Link](w+" ");
x++;}
else
el+=w+" ";
}
[Link](el+".");
[Link]("NUMBER OF WORDS BEGINNING AND
ENDINGWITHA VOWEL = " + x); }
else
[Link]("Invalid Input"); } }
OUTPUT :
Question3 : Write a Java program to input a string and count the number of
letters, digits, and special characters.
import [Link].*;
public class s3 {
publicstaticvoidmain(String[] args) {
Scannersc=new Scanner([Link]);
[Link]("Enter a string: ");
Stringinput=[Link]();
intletters=0,digits = 0, special = 0;
for(inti=0;i<[Link](); i++) {
charch=[Link](i);
if([Link](ch)) {
letters++;
}elseif([Link](ch)) {
digits++;
} else {
special++;
}
}
import [Link].*;
public class s4 {
publicstaticvoidmain(String[] args) {
Scannersc=new Scanner([Link]);
[Link]("Enter first string: ");
Stringstr1=[Link]();
[Link]("Enter second string: ");
Stringstr2=[Link]();
if([Link]()!= [Link]()) {
[Link]("No, not a rotation.");
}
else {
Stringcombined = str1 + str1;
if([Link](str2)) {
[Link]("Yes, second string is a rotation of first.");
} else {
[Link]("No, not a rotation."); } } }}
OUTPUT :
Question5 : Write a Java program to input two strings and check if they are
isomorphic (one-to-one character mapping).
ALGORITHM :
Step 7: In areIsomorphic.
Step 9: Create two HashMaps: map1 (char in s1 → s2), map2 (char in s2 → s1).
import [Link].*;
public class s5 {
publicstaticbooleanareIsomorphic(String s1, String s2) {
if([Link]()!=[Link]()) {
return false;
}
//forward:s1[i]→s2[i]
//backward:s2[i]→s1[i]
char[] fwd = new char[256]; //stores the image of each char in s1
char[] bwd = new char[256]; //stores the pre-image of each char in s2
for(inti=0;i<[Link](); i++) {
charc1=[Link](i);
charc2=[Link](i);
}
//alreadyseen→must map to the same char
elseif(fwd[c1]!=c2) {
return false;
}
if(bwd[c2] == 0) {
bwd[c2] = c1; }
else if (bwd[c2] != c1) {
return false; } } return true;}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first string: ");
String s1 = [Link]();
[Link]("Enter second string: ");
String s2 = [Link]();
if(areIsomorphic(s1, s2))
[Link]("Yes, the strings are isomorphic.");
else
[Link]("No, the strings are not isomorphic.");
}
}
VARIABLE DESCRIPTION
[Link]. DATA TYPE VARIABLE DESCRIPTION
1. String s1 To store 1st word
2. String s2 To store 2nd word
3. Char fwd[] To store image of each character
4. Char bwd[] To store pre image of each character
5. Char c1 To extract and store each value of s1
6. Char c2 To extract and store each value of s2
7. Int i For loop
OUTPUT :
QUESTION 1:
Base Class: Employee (Parent Class)
Data Members:
Member Functions:
Data Members:
Member Functions:
import [Link].*;
class Employee {
String name[];
int salary[];
int size;
Employee(int n) {
size = n;
name=newString[size];
salary=newint[size];
void input() {
Scannersc=newScanner([Link]);
for(inti=0;i<size; i++) {
name[i]=[Link]();
salary[i]=[Link]();
void displayAll() {
[Link]("Name\tSalary");
for(inti=0;i<size; i++) {
}
}
int ind;
Bonus(int n) {
super(n);
ind=0;
voidcalcBonus() {
[Link]("Bonus (20%):");
voidfindHighest() {
intmax = salary[0];
ind = i;
[Link]();
[Link]();
[Link]();
[Link](); }}
OUTPUT :
QUESTION2 :
Base Class: Student (Parent Class)
Data Members:
Member Functions:
1. Student(String n, int a, int b, int c) → parameterized constructor to initialize name and three subject
marks
2. void display() → displays name and marks in format:
Data Members:
Member Functions:
o Call [Link]()
o Call check()
o Print Result: PASS or Result: FAIL
import [Link].*;
class Student {
String name;
name = n;
m1 = a;
m2 = b;
m3 = c;
void display() {
import [Link].*;
String status;
super(n, a, b, c);
status = "FAIL";
}
voidcheck() {
status = "PASS";
voiddisplay() {
[Link]();
check();
Result(a,b,c,d);
String a1=[Link]();
[Link]("enter phy,chem,maths marks");
int b1=[Link]();
int c1=[Link]();
int d1=[Link]();
[Link]();
[Link]("---");
[Link]();
OUTPUT :
QUESTION 3:
Base Class: Product (Parent Class)
Data Members:
Member Functions:
Data Members:
Member Functions:
String pname;
pname = p;
qty = q;
rate = r;
void display() {
[Link]("Qty: " + qty + ", Rate: " + rate + ", Amt: " + amt);
}}
import [Link].*;
int per;
super(p, q, r);
per = d;
void applyDiscount() {
rate= rate - (rate * per / 100);
voiddisplay() {
[Link]();
applyDiscount();
[Link]("Qty: " + qty + ", New Rate: " + rate + ", New Amt:
" + amt);
String n=[Link]();
inta1=[Link]();
inta2=[Link]();
inta3=[Link]();
Data Members:
Member Functions:
1. void accept() →
2. void show() →
Data Members:
Member Functions:
1. void reverse() →
import [Link].*;
class Word {
String str;
void accept() {
Scannersc=new Scanner([Link]);
str = [Link]();
void show() {
classReverseWordextends Word {
void reverse() {
rev+=[Link](i);
void show() {
[Link]();
reverse();
[Link]();
[Link]();
OUTPUT :
QUESTION 5:
Base Class: Book (Parent Class)
Data Members:
Member Functions:
Data Members:
Member Functions:
title (String)
copies (int)
price (int)
Assign title = t
Assign copies = c
Assign price = p
issueCopies (int)
Call super(t, c, p)
Assign issueCopies = ic
o If true:
copies = copies - issueCopies
Print: issueCopies + " copies issued successfully."
o If false:
Print: "Not enough copies!"
Step 10: Define display() method (overridden)
SOURCE CODE :
import [Link].*;
classBook {
String title;
intcopies, price;
title = t;
copies = c;
price = p;
void display() {
}
}
[Link].*;
intissueCopies;
super(t, c, p);
issueCopies = ic;
voidissueBook() {
copies -= issueCopies;
}else {
voiddisplay() {
[Link]();
issueBook();
String n=[Link]();
int a1=[Link]();
int a2=[Link]();
int a3=[Link]();
[Link]();
}
VARIABLE DESCRIPTION
[Link]. DATA TYPE VARIABLE DESCRIPTION
1. String To store
title book title
2. int Tocopies
store no. of copies
3. int To store
price book price
4. String To storet book title
5. int To storec no. of copies
6. int To store
p book price
7. int To store
ic no. of issued copies
8. int issuecopies To store no. of issued copies
9. String n To accept book title
10. int a1 To accept no. Of copies
11. int a2 To accept book price
12. int a3 To accept no. Of issue book
OUTPUT :
QUESTION 1:
Main Class: sq1
Data Members:
o Finds next greater element to the right for each element in array
o Uses stack to track candidates
o Returns result array where:
res[i] = next greater element to right of a[i]
If no greater element → -1
import [Link].*;
n - 1; i >= 0; i--) {
top--;
stk[++top] = a[i];
}
return res;
[Link]("Enter 1 to start");
if([Link]() != 1) {
return;
String y = "yes";
String s = [Link]();
while ([Link](y)) {
[Link]("Enter 4 numbers");
nums[i] = [Link]();
s= [Link]();
}
[Link]("Thank you");
OUTPUT :
QUESTION2 :
Main Class: sq2
Data Members:
Member Functions:
import [Link].*;
publicclass sq2 {
char[] arr;
inttop;
intcapacity;
sq2(int size) {
capacity = size;
top = -1;
[Link]("Enterastring: ");
Stringinput=[Link]().toLowerCase().replaceAll("\\s+", "");
char ch = [Link](i);
if([Link]<[Link] - 1) {
[Link]++;
[Link][[Link]]=ch;
if([Link]<[Link] - 1) {
[Link]++;
[Link][[Link]]=ch; } }
int queueFront = 0;
charfromStack=[Link][[Link]--];
charfromQueue=[Link][queueFront++];
if(fromStack!=fromQueue) {
issq2 = false;
break; }}
[Link](); }}
OUTPUT :
QUESTION3 :
Main Class: sq3
import [Link].*;
int n = [Link];
if ([Link]([Link](0))) {
stk[++top] = num;
que[++rear] = num;
} else {
switch (token) {
case "+" -> stk[++top] = a + b;
que[++rear] = stk[top];
[Link]();
}}
OUTPUT :
QUESTION4 :
Main Class: sq4
import [Link].*;
int n = [Link]();
[Link]("Enter prices:");
que[++rear] = price[i];
[Link]();
OUTPUT :
QUESTION5 :
Main Class: sq5
ALGORITHM :
Step 7: Read n.
Step 8: Read k.
Step 21: While top >= 0 and a[stk[top]] <= a[i]: top--.
SOURCE CODE :
import [Link].*;
[Link]("Enter n, k: ");
int[]stk=newint[n],que=newint[n];
[Link]("Enter array:");
a[i] = [Link]();
que[++rear] = a[i];
stk[++top] = i;
if (stk[front] == i - k) front++;
if(i>=k-1)res[i-k+1]=a[stk[front]];
[Link]("Max:"+[Link](res));
[Link]("Windowelements: ");
front = rear - k + 2;
[Link]();
}
VARIABLE DESCRIPTION
[Link]. DATA TYPE VARIABLE DESCRIPTION
1. int n Totalnumberofelementsinthe array
2. int k Size of the sliding window
3. int a[] Input array of n integers
4. int res[] Output:maximumofeverywindow of
5. size k
int que[] Queuethatstoresactualarrayelements
6. int stk[] Monotonicstack(holdsindices)
7. int top Stack pointer (-1 = empty)
8. int front Frontindexofdeque(oldestuseful
9. int rear index)
10. Rearindexofqueue(-1=empty)
int i
For loop
OUTPUT :
THE
END