0% found this document useful (0 votes)
2 views

Adv Comp Coding - 2

The document contains multiple Java programs that implement various algorithms and data structures, including sorting, finding the minimum in an array, calculating stock spans, segregating even and odd numbers, solving the Tower of Hanoi problem, and identifying a celebrity in a group. Each program reads input from the user, processes the data, and outputs the result. The code snippets demonstrate fundamental programming concepts and problem-solving techniques.
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)
2 views

Adv Comp Coding - 2

The document contains multiple Java programs that implement various algorithms and data structures, including sorting, finding the minimum in an array, calculating stock spans, segregating even and odd numbers, solving the Tower of Hanoi problem, and identifying a celebrity in a group. Each program reads input from the user, processes the data, and outputs the result. The code snippets demonstrate fundamental programming concepts and problem-solving techniques.
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
You are on page 1/ 5

Tab 1

Sort the Bitonic / Merge Sort / Sort without space


import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n = sc.nextInt();
int arr[]=new int[n];
for(int i =0;i<n;i++){
arr[i]=sc.nextInt();
}
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
}

Minimum Stack
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
int min=findMin(arr);
System.out.print(min);
}
public static int findMin(int[] arr){
int min=arr[0];
for(int i=1;i<arr.length;i++){
if(arr[i]<min){
min=arr[i];
}
}
return min;
}
}
Stock Span
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];

for (int i = 0; i < n; i++) {


arr[i] = sc.nextInt();
}

int[] span = new int[n];


span[0] = 1;

for (int i = 1; i < n; i++) {


span[i] = 1;
while (i - span[i] >= 0 && arr[i] >= arr[i - span[i]]) {
span[i]++;
}
}

System.out.println(Arrays.toString(span));
}
}

Segregate even odd

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n =sc.nextInt();
int[] arr = new int[n];
for (int i=0; i<n;i++){
arr[i]=sc.nextInt();
}
int[] result = new int[arr.length];
int index = 0;
for (int num : arr) {
if (num % 2 == 0) {
result[index++] = num;
}
}
for (int num : arr) {
if (num % 2 != 0) {
result[index++] = num;
}
}
System.out.println(Arrays.toString(result));
}
}

Hanoi Tower
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner s1 = new Scanner(System.in);
System.out.print("Enter the number of disks: ");
int n = s1.nextInt();
towerOfHanoi(n, 'A', 'C', 'B');
}

public static void towerOfHanoi(int n, char source, char destination, char auxiliary) {
if (n == 1) {
System.out.println("Move disk 1 from " + source + " to " + destination);
return;
}
towerOfHanoi(n - 1, source, auxiliary, destination);
System.out.println("Move disk " + n + " from " + source + " to " + destination);
towerOfHanoi(n - 1, auxiliary, destination, source);
}
}
Celebrity Problem
import java.util.*;

class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[][] M = new int[N][N];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
M[i][j] = sc.nextInt();

int celebrity = findCelebrity(M, N);


System.out.println(celebrity != -1 ? "Celebrity id: " + celebrity : "No Celebrity");
}

static int findCelebrity(int[][] M, int n) {


int candidate = 0;
for (int i = 1; i < n; i++)
if (M[candidate][i] == 1) candidate = i;

for (int i = 0; i < n; i++)


if (i != candidate && (M[candidate][i] == 1 || M[i][candidate] == 0))
return -1;
return candidate;
}
}

You might also like