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

Java Solutions

The document contains code for 6 Java programs that solve different problems: 1) SavePrisoner finds the position of a prisoner after some steps. 2) HowManyCanSee counts how many buildings each person can see from their position in a line of buildings. 3) SherlockArray checks if an array is balanced by summing halves. 4) BraveArray finds the length of the longest subarray with ascending numbers. 5) BeautifulWords checks if a word only contains vowels in alternating order. 6) BalancedBrackets checks if brackets in a string are balanced using a stack.

Uploaded by

arunchinnathambi
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Java Solutions

The document contains code for 6 Java programs that solve different problems: 1) SavePrisoner finds the position of a prisoner after some steps. 2) HowManyCanSee counts how many buildings each person can see from their position in a line of buildings. 3) SherlockArray checks if an array is balanced by summing halves. 4) BraveArray finds the length of the longest subarray with ascending numbers. 5) BeautifulWords checks if a word only contains vowels in alternating order. 6) BalancedBrackets checks if brackets in a string are balanced using a stack.

Uploaded by

arunchinnathambi
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

SavePrisoner:

package savePrisoner;
import java.util.*;
public class SavePrisoner {

public static void main(String[] args)


{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int np=sc.nextInt();
int ns=sc.nextInt();
int start=sc.nextInt();
int pos=0;
if(ns>np)
{
pos=ns%np+start-1;
}
else
{
pos=ns+start-1;
}
System.out.println(pos);
}
}
}

HowManyCanSee

package howmanycansee;

/**
*
* @author 101874
*/
import java.util.*;
public class HowManyCanSee {

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int ar[]=new int[n];
int see[] =new int[n];
for(int i=0;i<n;i++)
ar[i]=sc.nextInt();
int crt=0,cnt=0;
for(int i=0;i<n;i++)
{
crt=ar[i];
cnt=1;
for(int j=i-1;j>0;j--)
{
if(crt>ar[j])
cnt++;
else
{
//if (ar[j]>ar[j-1])
// cnt++;
break;
}
}
see[i]=cnt;
}
for(int i=0;i<n;i++)
System.out.println(see[i]);
}

SherlockArray:

package sherlockarray;

import java.util.Scanner;

public class SherlockArray {

public static void main(String[] args)


{
// TODO code application logic here
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
boolean flag=false;
int[] ar=new int[n];
for(int i=0;i<n;i++)
ar[i]=sc.nextInt();
int lsum=0,rsum=0;
for(int i=1;i<n-1;i++)
{
lsum=0;
rsum=0;
for(int j=0;j<i;j++)
lsum+=ar[j];
for(int k=i+1;k<n;k++)
rsum+=ar[k];
if(lsum==rsum)
{
flag=true;
break;
}

}
if(flag)
System.out.println("Yes");
else
System.out.println("No");
}

}
BraveArray:

package bravearray;
import java.util.*;
public class BraveArray
{
public static void main(String[] arg)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int flag=-1;
int[] ar=new int[n];
for(int i=0;i<n;i++)
ar[i]=sc.nextInt();
int cnt=0,tcnt=0;

for(int i=0;i<n-1;i++)
{
if(ar[i]<ar[i+1])
{
if(flag==1)
{
tcnt++;
}
else if(flag==0 | flag==-1)
{
flag=1;
cnt=cnt<tcnt?tcnt:cnt;
tcnt=2;
}
}
else
{
if(flag==1 |flag ==-1)
{
flag=0;
cnt=cnt<tcnt?tcnt:cnt;
tcnt=2;
}
else
tcnt++;
}
}

System.out.println(cnt);
}
}

BeautifulWords:

package beautifulwords;
import java.util.*;
public class BeautifulWords {

public static void main(String[] args) {


// TODO code application logic here

Scanner sc =new Scanner(System.in);


String s=sc.nextLine().toLowerCase();
int len=s.length();
boolean flag=true;
for(int i=0;i<len-1;i++)
{
if((s.charAt(i)!=s.charAt(i+1)))
{
if(isVowel(s.charAt(i))&&isVowel(s.charAt(i+1))){
flag=false;
break;}
}
else
{
flag=false;
break;
}
}
if(flag)
System.out.println("Beautiful Word");
else
System.out.println("Not a Beautiful Word");
}
private static boolean isVowel(char c)
{
boolean flag=false;
if(c=='a'|c=='e'|c=='i'|c=='o'|c=='u')
flag=true;
return flag;
}

BalancedBrackets;

package balancedbrackets;

/**
*
* @author 101874
*/
import java.util.*;
public class BalancedBrackets {

public static void main(String[] args)


{
Scanner sc=new Scanner(System.in);
int t=Integer.parseInt(sc.nextLine());
while(t-->0){
char[] s=sc.nextLine().toCharArray();
char[] stack=new char[s.length];
int sTop=-1;
boolean flag=true;
for(int i=0;i<s.length;i++)
{
if(s[i]=='{'|s[i]=='('|s[i]=='[')
{
stack[++sTop]=s[i];
}
else
{
if(stack[sTop]=='{'&&s[i]=='}')
{
stack[sTop--]='\0';
}
else if(stack[sTop]=='('&&s[i]==')')
{
stack[sTop--]='\0';
}
else if(stack[sTop]=='['&&s[i]==']')
{
stack[sTop--]='\0';
}
else
{
flag =false;
break;
}
}
}
if(flag)
System.out.println("Yes");
else
System.out.println("No");
}
}

You might also like