0% found this document useful (0 votes)
8 views4 pages

Code

The document contains multiple Java code snippets demonstrating various algorithms and patterns. These include alternate sorting of an array, finding first occurrences of characters in a string, calculating the next greatest element in an array, and generating different numeric patterns and shapes. Each code snippet is accompanied by a main method that executes the respective functionality.

Uploaded by

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

Code

The document contains multiple Java code snippets demonstrating various algorithms and patterns. These include alternate sorting of an array, finding first occurrences of characters in a string, calculating the next greatest element in an array, and generating different numeric patterns and shapes. Each code snippet is accompanied by a main method that executes the respective functionality.

Uploaded by

nishantha3762
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

CODE

1 Alternate sorting:

import java.util.*;
class main{
static void alternatesort(int arr[] , int n ){
Arrays.sort(arr);
int i = 0;
int j = n-1;
while(i<j){
System.out.print(arr[j--] + " ");
System.out.print(arr[i++] + " ");

}
if (n % 2 !=0)
System.out.println(arr[i]);
}
public static void main(String[] args){
int arr[] = {1,9,2,4,7,3};
int n = arr.length;
alternatesort(arr,n);
}

2 First Occurences:

import java.util.*;
class Main{
public static String subString(String str,int startingIndex, int endingIndex){
String s = "";
for(int i=startingIndex ; i<=endingIndex;i++){
s += str.charAt(i) + "";
}
return s;

}
public static void main(String[] args){
String s1 = "ZOHOCORPORATION";
String s2 = "PORT";
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int n2 = s2.length();
for(int i=0; i<n2;i++){
int val = s1.indexOf(s2.charAt(i) + "");
if(val<=min)
min = val;
if(val>=max)
max = val;
}
System.out.println(min + " " + max);
System.out.println(subString(s1,min,max));

}
}

3 Greater On Right Side:


public class GreaterOnRightSide{
static void nextGreatest(int arr[] , int n ){
int max = -1;
int temp = 0;
for(int i=n-1;i>=0;i--){
temp = arr[i];
if(i == n-1){
arr[i] = -1;
max = Math.max(max,temp);
continue;
}
arr[i] = max;
max = Math.max(max,temp);
}
}

public static void main(String[] args){


int arr[] = {16,17,4,3,5,2};
int n = arr.length;
nextGreatest(arr,n);
for(int i = 0;i<n;i++){
System.out.print(arr[i] + " ");
}
}
}

4 Matrix Diagnol Sum:

public class MatrixDiagonalSum {


public static void main(String[] args) {
int row = 3, col = 3, upperTriangle = 0, lowerTriangle = 0;
int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (j <= col - (i + 1))
upperTriangle += arr[i][j];
if (j >= col - (i + 1))
// System.out.print(arr[i][j]+" ");
lowerTriangle += arr[i][j];
}
}
System.out.println(upperTriangle + " " + lowerTriangle);
System.out.println(upperTriangle > lowerTriangle ? upperTriangle :
lowerTriangle);
}
}

5 Number Staircase:

public class NumberStaircase {


public static void main(String[] args) {
int n = 5,count = 1,flag =1;
for (int i = 1; i <= n; i++) {
count=flag;
for (int j = 1; j <= n; j++) {
count = count>n? count-n:count;
System.out.print(count + " ");
count++;
}
flag++;
System.out.println();
}
}
}

6 NumbericRightAngleTrianglePattern:

import java.util.*;
public class NumbericRightAngleTrianglePattern {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n =5;
for(int i=1;i<=n;i++) {
for(int j=i;j<n;j++) {
System.out.print(" ");
}
for(int j=i;j>=1;j--) {
System.out.print(j+" ");
}
System.out.println();
}
}
}

7 Numeric Pattern:

public class Numericpattern{


public static void main(String[] args){
int n = 6, o = 1, e = 2;
for(int i = 0; i < n;i++){
for(int j = 0; j < n;j++){
System.out.print(" ");
}
for(int j = 1; j <=i; j++){
if(i%2 == 0){
System.out.print(e + " ");
e+=2;
}
else{
System.out.print(o + " ");
o+=2;
}
}
System.out.println();
}
}
}

8 XpatternWithWord:

public class XpatternWithWord {


public static void main(String[] args) {
String s = "NISHANT";
int n = s.length();
for(int i = 1;i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(i==j || j==((n+1)-i)?s.charAt(i-1): " ");
}
System.out.println();
}
}
}

9 SnakePattern:

public class SnakePattern {


public static void main(String[] args) {
int n =4, count =1;
for(int i = 1;i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(count + " ");
if(i%2 == 0 && j!= n)
count -=1;
else if(i%2!=0 && j!=n)
count +=1;
}
count +=n;
System.out.println();
}
}
}

You might also like