Vous êtes sur la page 1sur 6

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);
}
}

SherlockArray:

package sherlockarray;

import java.util.Scanner;

public class SherlockArray {


public static void main(String[] args)
{
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");
}

EqualStacks:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package equalstack;

import java.util.Scanner;

public class EqualStack {

public static void main(String[] args)


{
Scanner sc=new Scanner(System.in);
int n1=sc.nextInt();
int n2=sc.nextInt();
int n3=sc.nextInt();
int[] sum=new int[3];
boolean flag=false;
int top1=0,top2=0,top3=0;
int[][] ar={new int[n1], new int[n2],new int[n3]};
for(int i=0;i<ar.length;i++)
{
for(int j=0;j<ar[i].length;j++)
{
ar[i][j]=sc.nextInt();
sum[i]+=ar[i][j];
}
}
while(!flag)
{
if(sum[0]>sum[1]&&sum[0]>sum[2])
{
sum[0]-=ar[0][top1++];
}
if(sum[1]>sum[2]&&sum[1]>sum[0])
{
sum[1]-=ar[1][top2++];
}
if(sum[2]>sum[0]&&sum[2]>sum[1])
{
sum[2]-=ar[2][top3++];
}
if(sum[0]==sum[1]&&sum[1]==sum[2])
flag=true;
}
System.out.println(sum[0]);
}
}

Balance brackets:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
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");
}
}

Save Prisoner:

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);
}
}

}
Balanced Brackets

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
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");
}
}
}

Vous aimerez peut-être aussi