ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2050E - Three Strings
Language: java

Solution 1 (runtime: 374 ms):
import java.util.*;

public class Solution {
    static int dp[][];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();

        while(t-->0){
            String a=sc.next();
            String b=sc.next();
            String c=sc.next();
            dp=new int[a.length()+1][b.length()+1];

            for(int i=0;i<dp.length;i++){
                Arrays.fill(dp[i],-1);
            }
            System.out.println(solve(a,b,c,0,0,0));

        }
    }
    public static int solve(String a,String b,String c,int i,int j,int k){
        if(i==a.length() || j==b.length()){
            return calc(a,b,c,i,j,k);
        }
        if(dp[i][j]!=-1) return dp[i][j];
        if(c.charAt(k)==a.charAt(i) && c.charAt(k)==b.charAt(j)){
            return dp[i][j]=Math.min(solve(a,b,c,i+1,j,k+1),solve(a,b,c,i,j+1,k+1));

        }
        else if(c.charAt(k)==a.charAt(i) ){
            return dp[i][j]=Math.min(solve(a,b,c,i+1,j,k+1),1+solve(a,b,c,i,j+1,k+1));
        }
        else if(c.charAt(k)==b.charAt(j)){
            return dp[i][j]=Math.min(solve(a,b,c,i,j+1,k+1),1+solve(a,b,c,i+1,j,k+1));
        }
        else return dp[i][j]=1+Math.min(solve(a,b,c,i+1,j,k+1),solve(a,b,c,i,j+1,k+1));
    }

    public static int calc(String a,String b,String c,int i,int j,int k){
        int count=0;
        if(i==a.length()){
            while(j<b.length()){
                if(b.charAt(j)!=c.charAt(k)){
                    count++;
                }
                k++;
                j++;
            }

        }
        else{
            while(i<a.length()){
                if(a.charAt(i)!=c.charAt(k)){
                    count++;
                }
                i++;
                k++;
            }
        }
        return count;
    }
}

Solution 2 (runtime: 234 ms):
import java.util.Scanner;
public class Main{
	private static int solve(String a, String b, String c, int ai, int bi, int ci, int[][] dp) {
		if (ai==a.length()) {
			int diff = 0;
			for(int i=bi;i<b.length();i++) {
				diff += (b.charAt(i)==c.charAt(ci++)?0:1);
			}
			return diff;
		}
		if (bi==b.length()) {
			int diff = 0;
			for(int i=ai;i<a.length();i++) {
				diff += (a.charAt(i)==c.charAt(ci++)?0:1);
			}
			return diff;
		}
		if (dp[ai][bi]!=-1) {
			return dp[ai][bi];
		}
		// take from a
		int takeFromA = solve(a, b, c, ai+1, bi, ci+1,dp) + (a.charAt(ai)==c.charAt(ci)?0:1);
		int takeFromB = solve(a, b, c, ai, bi+1, ci+1,dp) + (b.charAt(bi)==c.charAt(ci)?0:1);
		return dp[ai][bi] = Math.min(takeFromA, takeFromB);
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while (t-->0) {
			String a,b,c;
			a = sc.next();
			b = sc.next();
			c = sc.next();
			int dp[][] = new int[a.length()][b.length()];
			for (int i=0;i<dp.length;i++) {
				for(int j=0;j<dp[i].length;j++) {
					dp[i][j] = -1;
				}
			}
			System.out.println(solve(a,b,c,0,0,0,dp));
		}
	}
}
Problem: 2060D - Subtract Min Sort
Language: java

Solution 1 (runtime: 484 ms):
import java.util.*;
public class Fi {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for(int o=0;o<t;o++){
            int n = sc.nextInt();
            int result = 1;
            int arr[] = new int[n];
            for(int i=0;i<n;i++){
                arr[i] = sc.nextInt();
                if(i>0 && arr[i-1]>arr[i]){
                    result = 0;
                }else if(i>0 && arr[i-1]<=arr[i]){
                    arr[i] -=arr[i-1];
                    arr[i-1] = 0;
                }
            }
            if(result==0){
                System.out.println("NO");
            }else{
                System.out.println("YES");
            }
          
        }
    }
}
//4
//1 4 3 2

Solution 2 (runtime: 671 ms):
import java.util.*;
 
public class bit {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            int n = sc.nextInt();
            int arr[] = new int[n];
            for(int i=0;i<n;i++){
                arr[i]=sc.nextInt();
            }
            String ans = "YES";
            if(arr[0]>arr[1]){
                System.out.println("NO");
                continue;
            }
            for(int i=0;i<n-1;i++){
                if(arr[i]>arr[i+1]){
                    ans = "NO";
                    break;
                }else{
                    arr[i+1]-=arr[i];
                    arr[i]-=arr[i];
                }
            }
            System.out.println(ans);
            
        } 
    }
}
Problem: 2050D - Digital string maximization
Language: java

Solution 1 (runtime: 296 ms):


import java.util.*;

import java.io.*;



public class DigitalStringMax {



public static void main(String args[]) throws Exception {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

PrintWriter pw = new PrintWriter(System.out);



int tt = Integer.parseInt(br.readLine());

while(tt-- > 0) {



char ch[] = br.readLine().toCharArray();



int n = ch.length;

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

int max = -1;

int ind = -1;

for(int j = i;j < Math.min(n,i+9); j++){



if(max < (ch[j] - (j - i))){

max = (ch[j] - (j - i));

ind = j;

}



}



ch[ind] = (char)(ch[ind]  - (ind - i ));

for(int j = ind; j > i; j--){

char temp = ch[j];



ch[j] = ch[j-1];

ch[j-1] = temp;



}

}



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

pw.print(ch[i]);

}

pw.println();



}

pw.flush();

}

}



Solution 2 (runtime: 609 ms):
import java.math.*;

import java.util.*;



public class Main {

static int N = (int)2e5 + 5;

static int[] a = new int[N];

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int T = sc.nextInt();

while(T-- > 0){

String s = sc.next();

int len = s.length();

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

a[i] = (int)s.charAt(i - 1) - (int)'0';

}

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

int x = a[i], idx = i;

int mx = x, id = -1;

for(int j = i + 1;j <= len && j <= i + 8;j++){

if(a[j] - (j - i) > mx){

mx = a[j] - (j - i);

idx = j;

}

}

for(int j = idx - 1;j >= i;j--){

int t = a[j];

a[j] = a[j + 1] - 1;

a[j + 1] = t;

}

}

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

System.out.print(a[i]);

}

System.out.println();

}

sc.close();

}

}
Problem: 2060D - Subtract Min Sort
Language: java

Solution 1 (runtime: 639 ms):
import java.util.*;

public class Main {
    public static void main(String[] args) {
      Scanner s=new Scanner(System.in);
      int t=s.nextInt();
      while(t-->0){
        int n=s.nextInt();
        int arr[]=new int[n];
        for(int i=0;i<n;i++){
          arr[i]=s.nextInt();
        }
        for(int i=1;i<n;i++){
          int a=Math.min(arr[i],arr[i-1]);
          arr[i-1]-=a;
          arr[i]-=a;
        }
        boolean ans=true;
        for(int i=0;i<n-1;i++){
          if(arr[i]!=0){
            ans=false;
            break;
          }
        }
        if(ans){
          System.out.println("YES");
        }else{
          System.out.println("NO");
        }
      }
  }
}

Solution 2 (runtime: 655 ms):
import java.util.*;
import java.lang.*;
import java.math.BigInteger;
import java.io.*;
public class cf{
    // public static long gcd(long a,long b){
    //     while(a!=0 && b!=0){
    //         if(a<b){
    //             b=b%a;
    //         }else{
    //             a=a%b;
    //         }
    //     }
    //     if(a!=0){
    //         return a;
    //     }else{
    //         return b;
    //     }
    // }  
    // public static long lcm(long a,long b){
    //     return (a/gcd(a,b))*b;
    // }

    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int tsts=sc.nextInt();
        for(int t=0;t<tsts;t++){
            int n=sc.nextInt();
            // int k=sc.nextInt();
            // int a4=sc.nextInt();
            // int a5=sc.nextInt();
            // int r2=sc.nextInt();
            // StringBuilder s=new StringBuilder(sc.next());
            // String a=sc.next();
            // String b=sc.next();
            // String c=sc.next();
            int[] a=new int[n];
            for(int i=0;i<n;i++){
                a[i]=sc.nextInt();
            }
            // int[] b=new int[n];
            // for(int i=0;i<n;i++){
            //     b[i]=sc.nextInt();
            // }
            // long n=sc.nextLong();
            // long k=sc.nextLong();
            // long r=sc.nextInt();
            // long c=sc.nextInt();
            for(int i=0;i<n-1;i++){
                int temp=Math.min(a[i],a[i+1]);
                a[i]-=temp;
                a[i+1]-=temp;
            }
            int[] b=new int[n];
            for(int i=0;i<n;i++){
                b[i]=a[i];
            }
            Arrays.sort(b);
            boolean flag=true;
            for(int i=0;i<n;i++){
                if(a[i]!=b[i]){
                    flag=false;
                    break;
                }
            }
            if(flag) System.out.println("Yes");
            else System.out.println("No");
        }    
    }
}
Problem: 2050C - Uninteresting Number
Language: java

Solution 1 (runtime: 296 ms):
import java.util.*;
import java.io.*;

public class Main {
    static Scanner f;
    static PrintWriter out;

    public static void main(String[] args) throws Exception {
        // f = new Scanner(new File("input.txt"));
        // out = new PrintWriter(new File("output.txt"));
        f = new Scanner(System.in);
        out = new PrintWriter(System.out);

        int t = f.nextInt();
        f.nextLine();
        while(t-- > 0) {
            solve();
        }

        out.close();
    }


    public static void solve() {
        String s = f.nextLine();
        int n = s.length();
        int sum = 0;
        int count2 = 0;
        int count3 = 0;
        for(int i = 0; i < n; i++) {
            sum += s.charAt(i)-'0';
            if(s.charAt(i) == '2') {
                count2++;
            } else if(s.charAt(i) == '3') {
                count3++;
            }
        }

        for(int i = 0; i <= Math.min(50, count2); i++) {
            for(int j = 0; j <= Math.min(50, count3); j++) {
                if((sum+2*i+6*j)%9 == 0) {
                    out.println("YES");
                    return;
                }
            }
        }
        out.println("NO");
    }


}

Solution 2 (runtime: 249 ms):
//package com.aakash.Contest.C991;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class C {
    static BufferedReader br = new BufferedReader(
            new InputStreamReader(System.in));

    public static void main(String[] args) throws IOException {

        StringTokenizer s1
                = new StringTokenizer(br.readLine());
        int t = Integer.parseInt(s1.nextToken());
        while (t-- > 0) {
            StringBuilder sb=new StringBuilder(br.readLine());
            int n = sb.length();
            boolean is=solve(sb,n);
            if(is) System.out.println("YES");
            else System.out.println("NO");
        }

    }

    static boolean solve(StringBuilder sb,int n) {
        int two=0,three=0;
        int sum=0;
        for(int i=0;i<n;i++) {
            int curr=sb.charAt(i)-'0';
            if(curr==2) two++;
            if(curr==3) three++;
            sum+=curr;
            sum%=9;
        }
        if((two>=5&&three>=5) ||sum==0) return true;
        sum=9-sum;
        for(int x=0;x<=two;x++) {
            for(int y=0;y<=three;y++) {
                long curr=x*2;
                curr+=y*6;
                if(curr%9==sum) return true;
            }
        }
        return false;
    }
}
Problem: 2062C - Cirno and Operations
Language: java

Solution 1 (runtime: 187 ms):
// "static void main" must be defined in a public class.
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner Sc = new Scanner(System.in);
        int t = Sc.nextInt();
        Sc.nextLine();
        for(int i=0; i<t; i++) {
            int n = Sc.nextInt();
            long[] arr = new long[n];
            for(int j=0; j<n; j++) {
                arr[j] = Sc.nextInt();
            }
            Solution s1 = new Solution();
            System.out.println(s1.maxSum(arr));
        }
    }
}

class Solution {
    public long maxSum(long[] arr) {
        if(arr.length == 1)
            return arr[0];
        int n = arr.length;
        long sum = 0;
        for(int i=0; i<n; i++) {
            sum += arr[i];
        }
        while(n > 1) {
            long temp = 0;
            for(int i=0; i<n-1; i++) {
                arr[i] = arr[i] - arr[i+1];
                temp = temp + arr[i];
            }
            sum = Math.max(sum, Math.abs(temp));
            n--;
        }
        return Math.max(sum, Math.abs(arr[0]));
    }
}


Solution 2 (runtime: 234 ms):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;

    public static String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    public static int nextLong() throws IOException {
        return Integer.parseInt(next());
    }

    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    public static void main(String[] args) throws IOException {
        int t = nextInt();
        while (t-- > 0) {
            run();
        }
    }

    public static void run() throws IOException {
        int n = nextInt();
        long[] arr = new long[n];
        int new_len = n;
        for(int i = 0;i<n;i++){
            arr[i] = nextLong();
        }
        long min = Long.MIN_VALUE;
        for(int i = 0;i<n;i++){
            long sum = 0;
            for(int j=0;j<new_len;j++){
                sum+=arr[j];
            }
            if(i==0){
                min = Math.max(min,sum);
            }
            else{
                min = Math.max(min,Math.max(sum,-sum));
            }
            for(int j = 0;j<n-1;j++){
                arr[j] = arr[j+1]-arr[j];
            }
            new_len--;
        }
        System.out.println(min);
    }
}
Problem: 2050D - Digital string maximization
Language: java

Solution 1 (runtime: 281 ms):
import java.io.*;
import java.util.*;

public class DigitalStringMaximization {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tokenizer = new StringTokenizer(in.readLine());

        int t = Integer.parseInt(tokenizer.nextToken());

        while (t-- > 0) {
            tokenizer = new StringTokenizer(in.readLine());
            char[] c = tokenizer.nextToken().toCharArray();

            for (int i = c.length - 1; i > 0; i--) {
                if (c[i] > c[i - 1] + 1) {
                    operation(c, i);
                    i += 9;
                    if (i > c.length) {
                        i = c.length;
                    }
                }
            }

            System.out.println(new String(c));
        }
    }

//    public static int findLast(char[] c) {
//        for (int i = c.length - 1; i > 0; i--) {
//            if (c[i] > c[i - 1] + 1) {
//                return i + 1;
//            }
//        }
//        return c.length;
//    }

    public static void operation(char[] c, int i) {
        int cur = c[i];
        int left = c[i - 1];
        c[i - 1] = (char) (cur - 1);
        c[i] = (char) left;
    }
}

Solution 2 (runtime: 296 ms):
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        sc.nextLine(); 
        while (t-- > 0) {
            String s = sc.nextLine();
            char[] arr = s.toCharArray();
            int swapcount = 1;
            int ans = 0;
            while (swapcount != 0) {
                int sw = 0;
                for (int i = 1; i < arr.length; i++) {
                    char a = (char) (arr[i] - 1);
                    char b = arr[i - 1];
                    if ((arr[i] != '1' || arr[i] != '0') && a - b > 0) {
                        if (arr[i] > arr[i - 1]) {
                            swap(i - 1, i, arr);
                            sw++;
                        }
                    }
                }

                if (sw == 0) {
                    break;
                }
            }
            System.out.println(new String(arr));
        }
    }

    public static void swap(int i, int j, char[] arr) {
        arr[j] = (char) (arr[j] - 1);
        char t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }
}
Problem: 2057C - Trip to the Olympiad
Language: java

Solution 1 (runtime: 374 ms):
import java.util.*;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt(); // number of test cases
        // int t = 1;
        while (t-- > 0) {
            long l = sc.nextLong();
            long r = sc.nextLong();

            long ans = 0;
            long a = 0;
            long b = 0;
            // long c = 0;
            for(int i = 30;i>=0;i--) {
                long and1 = r & (1 << i);
                long and2 = l & (1 << i);

                if(and1 == and2) {
                    if(and1 >= 1) ans = ans + (1 << i);
                } else {
                    a = ans + (1 << i);
                    b = a - 1;
                    break;
                }
            }
            long c = r;
            if(a == r) {
                c = l;
            }
            System.out.println(a + " " + b + " " + c);
        }
        
    }
}



Solution 2 (runtime: 358 ms):
import java.util.*;
import java.lang.*;
import java.math.BigInteger;
import java.io.*;
public class cf{
    // public static long gcd(long a,long b){
    //     while(a!=0 && b!=0){
    //         if(a<b){
    //             b=b%a;
    //         }else{
    //             a=a%b;
    //         }
    //     }
    //     if(a!=0){
    //         return a;
    //     }else{
    //         return b;
    //     }
    // }  
    // public static long lcm(long a,long b){
    //     return (a/gcd(a,b))*b;
    // }

    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int tsts=sc.nextInt();
        for(int t=0;t<tsts;t++){
            int l=sc.nextInt();
            int r=sc.nextInt();
            // int a=sc.nextInt();
            // int b=sc.nextInt();
            // int r2=sc.nextInt();
            // StringBuilder s=new StringBuilder(sc.next());
            // String a=sc.next();
            // String b=sc.next();
            // String c=sc.next();

            // int[] a=new int[n];
            // for(int i=0;i<n;i++){
            //     a[i]=sc.nextInt();
            // }
            // int[] b=new int[n];
            // for(int i=0;i<n;i++){
            //     b[i]=sc.nextInt();
            // }
            // long x=sc.nextLong();
            // long m=sc.nextLong();
            // long r=sc.nextInt();
            // long c=sc.nextInt();
            int n=l^r;
            int i=0;
            while(n>0){
                n=n>>1;
                i++;
            }
            i--;
            int a=l|((1<<i)-1),b=a+1,c=(a==l)?r:l;
            System.out.println(a+" "+b+" "+c);
        }    
    }
}
Problem: 2050E - Three Strings
Language: java

Solution 1 (runtime: 374 ms):
import java.util.*;

public class Solution {
    static int dp[][];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();

        while(t-->0){
            String a=sc.next();
            String b=sc.next();
            String c=sc.next();
            dp=new int[a.length()+1][b.length()+1];

            for(int i=0;i<dp.length;i++){
                Arrays.fill(dp[i],-1);
            }
            System.out.println(solve(a,b,c,0,0,0));

        }
    }
    public static int solve(String a,String b,String c,int i,int j,int k){
        if(i==a.length() || j==b.length()){
            return calc(a,b,c,i,j,k);
        }
        if(dp[i][j]!=-1) return dp[i][j];
        if(c.charAt(k)==a.charAt(i) && c.charAt(k)==b.charAt(j)){
            return dp[i][j]=Math.min(solve(a,b,c,i+1,j,k+1),solve(a,b,c,i,j+1,k+1));

        }
        else if(c.charAt(k)==a.charAt(i) ){
            return dp[i][j]=Math.min(solve(a,b,c,i+1,j,k+1),1+solve(a,b,c,i,j+1,k+1));
        }
        else if(c.charAt(k)==b.charAt(j)){
            return dp[i][j]=Math.min(solve(a,b,c,i,j+1,k+1),1+solve(a,b,c,i+1,j,k+1));
        }
        else return dp[i][j]=1+Math.min(solve(a,b,c,i+1,j,k+1),solve(a,b,c,i,j+1,k+1));
    }

    public static int calc(String a,String b,String c,int i,int j,int k){
        int count=0;
        if(i==a.length()){
            while(j<b.length()){
                if(b.charAt(j)!=c.charAt(k)){
                    count++;
                }
                k++;
                j++;
            }

        }
        else{
            while(i<a.length()){
                if(a.charAt(i)!=c.charAt(k)){
                    count++;
                }
                i++;
                k++;
            }
        }
        return count;
    }
}

Solution 2 (runtime: 343 ms):
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while (t-->0){
           String a=sc.next();
           String b=sc.next();
           String c=sc.next();
           int[][] f=new int[a.length()+1][b.length()+1];
            for (int i = 0; i < a.length(); i++) {
                f[i+1][0]=f[i][0]+(a.charAt(i)==c.charAt(i)?0:1);
            }
            for (int i = 0; i < b.length(); i++) {
                f[0][i+1]=f[0][i]+(b.charAt(i)==c.charAt(i)?0:1);
            }
            f[0][0]=0;
            for (int i = 1; i <= a.length(); i++) {
                for (int j = 1; j <= b.length(); j++) {
                    f[i][j]=Integer.MAX_VALUE;
                    f[i][j]=Math.min(f[i][j],f[i-1][j]+(a.charAt(i-1)==c.charAt(i+j-1)? 0 : 1));
                    f[i][j]=Math.min(f[i][j],f[i][j-1]+(b.charAt(j-1)==c.charAt(i+j-1)? 0 : 1));
                }
            }
            System.out.println(f[a.length()][b.length()]);
        }
    }
}