0% found this document useful (0 votes)
15 views10 pages

Durgasoft DSA Notes PDF Guide

The document provides Java code examples for swapping diagonal elements in a matrix and performing left and right rotations on an array. It includes explanations of different methods for rotations, such as brute force and using a temporary variable. The document also contains sample outputs for various inputs demonstrating the functionality of the code.

Uploaded by

Rehan Hussain
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views10 pages

Durgasoft DSA Notes PDF Guide

The document provides Java code examples for swapping diagonal elements in a matrix and performing left and right rotations on an array. It includes explanations of different methods for rotations, such as brute force and using a temporary variable. The document also contains sample outputs for various inputs demonstrating the functionality of the code.

Uploaded by

Rehan Hussain
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DURGASOFT

main dia and opp dia

321
456
987

a[i][i]
a[i][n-i-1]

t=a[i][i]
a[i][i]=a[i][n-i-1]
a[i][n-i-1]=t;

Ex:
--
import [Link].*;

class Test
{
public static void main(String[] args)
{
Scanner obj = new Scanner([Link]);

[Link]("Enter row value:");


int row = [Link]();

[Link]("Enter col value:");


int col = [Link]();

int a[][] = new int[row][col];

int i,j,t;

[Link]("Enter matrix elements:");

for(i=0;i<row;i++){
for(j=0;j<col;j++){
a[i][j] = [Link]();
}
}

[Link]("Before swaping...");
for(i=0;i<row;i++){
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
171  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
for(j=0;j<col;j++){
[Link](a[i][j]+" ");
}
[Link]();
}
for(i=0;i<row;i++){
t=a[i][i];
a[i][i]=a[i][row-i-1];
a[i][row-i-1]=t;
}
[Link]("After swaping...");
for(i=0;i<row;i++){
for(j=0;j<col;j++){
[Link](a[i][j]+" ");
}
[Link]();
}
}
}

output:
-------
C:\prakashclasses>javac [Link]

C:\prakashclasses>java Test
Enter row value:
3
Enter col value:
3
Enter matrix elements:
123
456
789
Before swaping...
123
456
789
After swaping...
321
456
987

DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,


172  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
Array Rotations:
~~~~~~~~~~~~~~~~
Ex:
---
[1, 2, 3, 4, 5] ---> Left Rotations

[2, 3, 4, 5, 1] ---> 1
[3, 4, 5, 1, 2] ---> 2
[4, 5, 1, 2, 3] ---> 3
[5, 1, 2, 3, 4] ---> 4
[1, 2, 3, 4, 5] ---> 5

[1, 2, 3, 4, 5] ---> Right Rotations

[5, 1, 2, 3, 4] ---> 1
[4, 5, 1, 2, 3] ---> 2
[3, 4, 5, 1, 2] ---> 3
[2, 3, 4, 5, 1] ---> 4

we can perform these rotations in the following ways

1) Brute Force
--------------
Rotate all the elements by one position towards left/right direction for 'r' rotations.

n----> array size


a----> array
r----> number of rotations

Note:
-----
r=r%n
n=5, r=1 -----> r=0 -------------------> r=1%5=1
n=5, r=2 -----> r=0,1 -----------------> r=2%5=2
n=5, r=3 -----> r=0,1,2 ---------------> r=3%5=3
n=5, r=4 -----> r=0,1,2,3 -------------> r=4%5=4
n=5, r=5 -----> loop wn't execute -----> r=5%5=0
n=5, r=6 -----> r=0 -------> r=6%5=1
Ex:
---
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
173  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
import [Link].*;

class Demo
{
static int[] rotateLeft(int a[],int r)
{
int temp,prev,i,j;
for(i=0;i<r;i++)
{
prev=a[0];
for(j=[Link]-1;j>=0;j--){
temp=a[j];
a[j]=prev;
prev=temp;
}
}
return a;
}
}

class Test
{
public static void main(String[] args)
{
Scanner obj = new Scanner([Link]);

int a[] = {1, 2, 3, 4, 5};

[Link]("Enter number of rotations(r):");


int r = [Link]();

[Link]("Before Rotation==>"+[Link](a));
a=[Link](a,r);
[Link]("After Rotation ==>"+[Link](a));
}
}

output:
-------
C:\prakashclasses>javac [Link]

C:\prakashclasses>java Test
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
174  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
Enter number of rotations(r):
1
Before Rotation==>[1, 2, 3, 4, 5]
After Rotation ==>[2, 3, 4, 5, 1]

C:\prakashclasses>java Test
Enter number of rotations(r):
2
Before Rotation==>[1, 2, 3, 4, 5]
After Rotation ==>[3, 4, 5, 1, 2]

C:\prakashclasses>java Test
Enter number of rotations(r):
3
Before Rotation==>[1, 2, 3, 4, 5]
After Rotation ==>[4, 5, 1, 2, 3]

C:\prakashclasses>java Test
Enter number of rotations(r):
4
Before Rotation==>[1, 2, 3, 4, 5]
After Rotation ==>[5, 1, 2, 3, 4]

C:\prakashclasses>java Test
Enter number of rotations(r):
5
Before Rotation==>[1, 2, 3, 4, 5]
After Rotation ==>[1, 2, 3, 4, 5]

C:\prakashclasses>java Test
Enter number of rotations(r):
6
Before Rotation==>[1, 2, 3, 4, 5]
After Rotation ==>[2, 3, 4, 5, 1]

Ex;
---
import [Link].*;

class Demo
{
static int[] rotateLeft(int a[],int r)
{
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
175  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
int temp,prev,i,j;
for(i=0;i<r;i++)
{
prev=a[0];
for(j=[Link]-1;j>=0;j--){
temp=a[j];
a[j]=prev;
prev=temp;
}
}
return a;
}
static int[] rotateRight(int a[],int r)
{
int temp,prev,i,j;
for(i=0;i<r;i++)
{
prev=a[[Link]-1];
for(j=0;j<[Link];j++){
temp=a[j];
a[j]=prev;
prev=temp;
}
}
return a;
}
}

class Test
{
public static void main(String[] args)
{
Scanner obj = new Scanner([Link]);

int a[] = {1, 2, 3, 4, 5};

[Link]("Enter number of rotations(r):");


int r = [Link]();

[Link]("Before Rotation==>"+[Link](a));
a=[Link](a,r);
[Link]("After Rotation ==>"+[Link](a));
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
176  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
}
}

output:
-------
C:\prakashclasses>javac [Link]

C:\prakashclasses>java Test
Enter number of rotations(r):
1
Before Rotation==>[1, 2, 3, 4, 5]
After Rotation ==>[5, 1, 2, 3, 4]

C:\prakashclasses>java Test
Enter number of rotations(r):
2
Before Rotation==>[1, 2, 3, 4, 5]
After Rotation ==>[4, 5, 1, 2, 3]

C:\prakashclasses>java Test
Enter number of rotations(r):
3
Before Rotation==>[1, 2, 3, 4, 5]
After Rotation ==>[3, 4, 5, 1, 2]

C:\prakashclasses>java Test
Enter number of rotations(r):
4
Before Rotation==>[1, 2, 3, 4, 5]
After Rotation ==>[2, 3, 4, 5, 1]

C:\prakashclasses>java Test
Enter number of rotations(r):
5
Before Rotation==>[1, 2, 3, 4, 5]
After Rotation ==>[1, 2, 3, 4, 5]

2) by using 'temp' variable


---------------------------
Ex:
---
import [Link].*;
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
177  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
class Demo
{
static int[] rotateLeft_Temp(int a[],int r)
{
r=r%[Link];

int temp, i, j;

for(i=0;i<r;i++)
{
temp=a[0];
for(j=0;j<[Link]-1;j++){
a[j]=a[j+1];
}
a[[Link]-1]=temp;
}

return a;
}
}

class Test
{
public static void main(String[] args)
{
Scanner obj = new Scanner([Link]);

int a[] = {1, 2, 3, 4, 5};

[Link]("Enter number of rotations(r):");


int r = [Link]();

[Link]("Before Rotation==>"+[Link](a));
a=Demo.rotateLeft_Temp(a,r);
[Link]("After Rotation ==>"+[Link](a));
}
}

output:
-------
C:\prakashclasses>javac [Link]

DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,


178  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
C:\prakashclasses>java Test
Enter number of rotations(r):
1
Before Rotation==>[1, 2, 3, 4, 5]
After Rotation ==>[2, 3, 4, 5, 1]

C:\prakashclasses>java Test
Enter number of rotations(r):
2
Before Rotation==>[1, 2, 3, 4, 5]
After Rotation ==>[3, 4, 5, 1, 2]

C:\prakashclasses>java Test
Enter number of rotations(r):
3
Before Rotation==>[1, 2, 3, 4, 5]
After Rotation ==>[4, 5, 1, 2, 3]

C:\prakashclasses>java Test
Enter number of rotations(r):
4
Before Rotation==>[1, 2, 3, 4, 5]
After Rotation ==>[5, 1, 2, 3, 4]

C:\prakashclasses>java Test
Enter number of rotations(r):
5
Before Rotation==>[1, 2, 3, 4, 5]
After Rotation ==>[1, 2, 3, 4, 5]

Ex:
---
import [Link].*;

class Demo
{
static int[] rotateRight_Temp(int a[],int r)
{
r=r%[Link];

int temp, i, j;

DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,


179  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
for(i=0;i<r;i++)
{
temp=a[[Link]-1];
for(j=[Link]-1;j>0;j--){
a[j]=a[j-1];
}
a[0]=temp;
}

return a;
}
}

class Test
{
public static void main(String[] args)
{
Scanner obj = new Scanner([Link]);

int a[] = {1, 2, 3, 4, 5};

[Link]("Enter number of rotations(r):");


int r = [Link]();

[Link]("Before Rotation==>"+[Link](a));
a=Demo.rotateRight_Temp(a,r);
[Link]("After Rotation ==>"+[Link](a));
}
}

output:
-------
C:\prakashclasses>javac [Link]

C:\prakashclasses>java Test
Enter number of rotations(r):
1
Before Rotation==>[1, 2, 3, 4, 5]
After Rotation ==>[5, 1, 2, 3, 4]

C:\prakashclasses>java Test
Enter number of rotations(r):
2
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
180  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]

You might also like