codeparrot/codecomplex huggingface dataset:
// Problem: 1141_F2. Same Sum Blocks (Hard)
// Time complexity: quadratic
import java.util.*;
import java.math.*;
public class Main{
	static HashMap<Integer,ArrayList<seg>> ma;
	static class seg{
		seg(int a,int b){
			l=a;r=b;
		}
		int l,r;
	}
	static int n,sum,dex;
	static int arr[]=new int[1600];
	public static void main(String argas[]){
		Scanner cin=new Scanner(System.in);
		ma=new HashMap();
		n=cin.nextInt();
		for(int i=1;i<=n;i++){
			arr[i]=cin.nextInt();
			sum=0;
			for(int j=i;j>0;j--){
				sum+=arr[j];
				if(ma.containsKey(sum)) ma.get(sum).add(new seg(j,i));
				else {
					ma.put(sum, new ArrayList<seg>());
					ma.get(sum).add(new seg(j,i));
				}
			}
		}
		int ans=0,te;
		ArrayList<seg> best=new ArrayList(),now,temp;
		Iterator it=ma.entrySet().iterator();
		while(it.hasNext()){
			now=new ArrayList();
			te=0;
			Map.Entry entry=(Map.Entry) it.next();
			temp=(ArrayList<seg>) entry.getValue();
			dex=0;
			for(int i=0;i<temp.size();i++){
				if(temp.get(i).l>dex){
					dex=temp.get(i).r;
					te++;
					now.add(new seg(temp.get(i).l,temp.get(i).r));
				}
			}
			if(te>ans){
				ans=te;
				best=now;
			}
		}
		System.out.println(ans);
		for(int i=0;i<best.size();i++){
			System.out.println(best.get(i).l+" "+best.get(i).r);
		}
	}
}
// Problem: 1141_F2. Same Sum Blocks (Hard)
// Time complexity: quadratic
//package com.example.programming;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Test {
    static class Pair {
        int f,s;
        public Pair(int x, int y) {f = x; s = y;}
    }
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        String[] s = br.readLine().split(" ");
        int[] arr = new int[n];
        for(int i = 0; i<n; i++) arr[i] = Integer.parseInt(s[i]);

        HashMap<Integer, ArrayList<Pair>> map = new HashMap<>();
        for(int i = 0; i<n; i++) {
            int sum = 0;
            for(int j = i; j>=0; j--) {
                sum += arr[j];
                ArrayList<Pair> list = map.get(sum);
                if(list == null) {
                    list = new ArrayList<>();
                    map.put(sum, list);
                }
                list.add(new Pair(j, i));
            }
        }

        Iterator it = map.entrySet().iterator();
        ArrayList<Pair> ans = new ArrayList<>();
        for(;it.hasNext();){
            Map.Entry<Integer, ArrayList<Pair>> entry = (Map.Entry<Integer, ArrayList<Pair>>)it.next();
            ArrayList<Pair> list = entry.getValue();
            ArrayList<Pair> pre = new ArrayList<>();
            int r = -1;
            for(Pair p : list) {
                if(p.f > r) {
                    pre.add(p);
                    r = p.s;
                }
            }
            if(ans.size()<pre.size()) ans = pre;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(ans.size()).append('\n');
        for(Pair p : ans) {
            sb.append(p.f+1).append(' ').append(p.s+1).append('\n');
        }
        System.out.print(sb);
    }

}
// Problem: 1141_F2. Same Sum Blocks (Hard)
// Time complexity: quadratic
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Main {
	
	private static class Interval implements Comparable<Interval> {
		public int start , end;
		public Interval(int start , int end) {
			this.start = start;
			this.end = end;			
		}
		@Override
		public int compareTo(Interval interval) {
			return this.end - interval.end;			
		}		
	}
	
	private static int getTotal(List<Interval> list) {
		int ans = 0 , i , n = list.size();
		for (i = 0;i < n;i ++) {
			int end = list.get(i).end;
			while (i < n && list.get(i).start <= end) {
				i ++;				
			}
			i --;
			ans ++;
		}		
		return ans;
	}
	
	private static void solve(List<Interval> list) {
		List<int[]> ans = new ArrayList<>();
		int i , n = list.size();
		for (i = 0;i < n;i ++) {
			int start = list.get(i).start , end = list.get(i).end;
			while (i < n && list.get(i).start <= end) {
				i ++;				
			}
			i --;
			ans.add(new int[] {start , end});
		}
		System.out.println(ans.size());
		for (int[] array : ans) {
			System.out.println(array[0] + " " + array[1]);
		}
	}
	
	private static long[] a = new long[2000];	
	
	public static void main(String[] args) {																	
	
		Scanner scan = new Scanner(System.in);				

		Map<Long , List<Interval>> map = new HashMap<>();		
		int i , j , n = scan.nextInt() , max = 0;
		long ans = 0;
		for (i = 1;i <= n;i ++) {
			a[i] = scan.nextLong();
		}
		for (i = 1;i <= n;i ++) {
			long sum = 0;
			for (j = i;j <= n;j ++) {				
				sum += a[j];
				if (!map.containsKey(sum)) {
					map.put(sum , new ArrayList<>());
				}
				map.get(sum).add(new Interval(i , j));				
			}
		}
		for (List<Interval> list : map.values()) {
			Collections.sort(list);			
		}		
		for (Map.Entry<Long , List<Interval>> entry : map.entrySet()) {
			int total = getTotal(entry.getValue());
			if (total > max) {
				max = total;
				ans = entry.getKey();				
			}
		}
		solve(map.get(ans));		
	
	}
    
}
// Problem: 1141_F2. Same Sum Blocks (Hard)
// Time complexity: quadratic
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

public class F11141 {
	static class Solver {

		ArrayList<int[]> ranges[];
		HashMap<Long, Integer> hm = new HashMap<>();

		int id(long s) {
			if (!hm.containsKey(s))
				hm.put(s, hm.size());
			return hm.get(s);
		}
		
		// max disjoint range set in ranges[r]
		int[] memo;
		int go(int r) {
			memo[N] = 0;
			int last = N;
			for(int[] a : ranges[r]) {
				while(a[0] < last) {
					memo[last - 1] = memo[last];
					last--;
				}
				memo[a[0]] = Math.max(memo[a[0]], Math.max(memo[a[0]], 1 + memo[a[1] + 1]));
				last = a[0];
			}
			while(0 < last) {
				memo[last - 1] = memo[last];
				last--;
			}
			return memo[0];
		}

		ArrayDeque<int[]> ans = new ArrayDeque<>();
		
		void go2(int r) {
			memo[N] = 0;
			int last = N;
			int minAt[] = new int[N], oo = 987654321;
			Arrays.fill(minAt, oo);
			for(int[] a : ranges[r]) {
				minAt[a[0]] = Math.min(minAt[a[0]], a[1] - a[0]);
				while(a[0] < last) {
					memo[last - 1] = memo[last];
					last--;
				}
				memo[a[0]] = Math.max(memo[a[0]], Math.max(memo[a[0]], 1 + memo[a[1] + 1]));
				last = a[0];
			}
			while(0 < last) {
				memo[last - 1] = memo[last];
				last--;
			}
			int k = 0;
			for(; k < N;) {
				if(minAt[k] == oo || memo[k] != 1 + memo[k + minAt[k] + 1]) k++;
				else {
					ans.push(new int[] {k, k + minAt[k]});
					k += minAt[k] + 1;
				}
			}
		}
		
		@SuppressWarnings("unchecked")
		Solver() {
			ranges = new ArrayList[2250001];
			for (int i = 0; i < ranges.length; i++)
				ranges[i] = new ArrayList<>();
		}

		int N, LID;
		long[] a;

		void solve(Scanner s, PrintWriter out) {
			N = s.nextInt();
			a = new long[N + 1];
			for (int i = 1; i <= N; i++)
				a[i] = s.nextLong() + a[i - 1];
			for (int i = N; i >= 1; i--)
				for (int j = i; j <= N; j++) {
					int x = id(a[j] - a[i - 1]);
					ranges[x].add(new int[] { i - 1, j - 1 });
				}
			
			int best = 0, bid = -1;
			memo = new int[N + 1];
			Arrays.sort(ranges, (a, b) -> b.size() - a.size());
			for(int i = 0; i < ranges.length; i++, LID++) {
				if(ranges[i].size() <= best) break;
				int ans = go(i);
				if(ans > best) {
					best = ans;
					bid = i;
				}
			}
			
			// backtrack on bid
			out.println(best);
			go2(bid);
						
			while(!ans.isEmpty()) {
				int[] c = ans.pop();
				out.println(++c[0] + " " + ++c[1]);
			}
			
		}

	}

	public static void main(String[] args) {

		Scanner s = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		Solver solver = new Solver();
		solver.solve(s, out);

		out.close();

	}
}
// Problem: 1141_F2. Same Sum Blocks (Hard)
// Time complexity: quadratic
import java.io.*;
import java.util.*;
public class Main
{
	HashMap<Integer,Pair> map;
	int n,a[];
	private void solve()throws IOException
	{
		n=nextInt();
		a=new int[n+1];
		for(int i=1;i<=n;i++)
			a[i]=nextInt();
		map=new HashMap<>();
		for(int i=1;i<=n;i++)
		{
			int sum=0;
			for(int j=i;j>=1;j--)
			{
				sum+=a[j];
				if(!map.containsKey(sum))
					map.put(sum,new Pair(i,1));
				else
				{
					Pair p=map.get(sum);
					if(p.pos<j)
						map.put(sum,new Pair(i,p.cnt+1));
				}
			}
		}
		int sum=0,ans=0;
		for(int i:map.keySet())
			if(map.get(i).cnt>ans)
			{
				ans=map.get(i).cnt;
				sum=i;
			}
		out.println(ans);
		ArrayList<String> list=new ArrayList<>();
		for(int i=1,prev=0;i<=n;i++)
		{
			int s=0;
			for(int j=i;j>=1;j--)
			{
				s+=a[j];
				if(s==sum && j>prev)
				{
					list.add(j+" "+i);
					prev=i;
				}
			}
		}
		for(String s:list)
			out.println(s);
	}
	class Pair{
	    int pos,cnt;
	    Pair(int a,int b){
	        pos=a;
	        cnt=b;
	    }
	}
	 
	///////////////////////////////////////////////////////////

	public void run()throws IOException
	{
		br=new BufferedReader(new InputStreamReader(System.in));
		st=null;
		out=new PrintWriter(System.out);

		solve();
		
		br.close();
		out.close();
	}
	public static void main(String args[])throws IOException{
		new Main().run();
	}
	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;
	String nextToken()throws IOException{
		while(st==null || !st.hasMoreTokens())
		st=new StringTokenizer(br.readLine());
		return st.nextToken();
	}
	String nextLine()throws IOException{
		return br.readLine();
	}
	int nextInt()throws IOException{
		return Integer.parseInt(nextToken());
	}
	long nextLong()throws IOException{
		return Long.parseLong(nextToken());
	}
	double nextDouble()throws IOException{
		return Double.parseDouble(nextToken());
	}
}
// Problem: 1141_F2. Same Sum Blocks (Hard)
// Time complexity: quadratic
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class SolutionArch2 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.valueOf(scanner.nextLine());
        String s = scanner.nextLine();
        int[] arr = Arrays.stream(s.split(" "))
                .mapToInt(Integer::valueOf)
                .toArray();
        int[] prefixSum = new int[n + 1];
        for (int i = 0; i < n; i++) {
            prefixSum[i + 1] = prefixSum[i] + arr[i];
        }
        Map<Integer, List<int[]>> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
                int subarraySum = prefixSum[j + 1] - prefixSum[i];
                map.putIfAbsent(subarraySum, new ArrayList<>());
                int l = i + 1, r = j + 1;
                map.get(subarraySum).add(new int[]{l, r});
            }
        }
        List<int[]> resultPairs = new ArrayList<>();
        for (Map.Entry<Integer, List<int[]>> e : map.entrySet()) {
            List<int[]> result = new ArrayList<>();
            int[] curr = new int[2];
            List<int[]> pairs = e.getValue();
            Collections.sort(pairs, Comparator.<int[]>comparingInt(a -> a[1]));
            for (int[] next : pairs) {
                if (next[0] > curr[1]) {
                    result.add(next);
                    curr = next;
                }
            }
            if (resultPairs.size() < result.size()) {
                resultPairs = result;
            }
        }
        printResult(resultPairs);
    }

    private static void printResult(List<int[]> list) {
        StringBuilder sb = new StringBuilder();
        sb.append(list.size()).append("\n");
        for (int[] pair : list) {
            sb.append(pair[0]).append(" ").append(pair[1]).append("\n");
        }
        System.out.print(sb);
    }
}
// Problem: 1141_F2. Same Sum Blocks (Hard)
// Time complexity: quadratic
import java.io.*;
import java.util.*;
import java.math.*;
import java.awt.Point;

public final class Solution {
    BufferedReader br;
    StringTokenizer st;
    
    {
        st = null;
        br = new BufferedReader(new InputStreamReader(System.in));
    }
    
    public static void main(String[] args) throws Exception {
        new Solution().run();
    }
    
    void run() throws Exception {
        int n = ni();
        int[] a = new int[n];
        for(int i=0; i<n; i++) a[i] = ni();
        for(int i=1; i<n; i++) a[i] += a[i-1];
        
        HashMap<Integer, List<Point>> map = new HashMap<>();
        for(int i=0; i<n; i++) {
            for(int j=i; j<n; j++) {
                int sum = getSum(a, i, j);
                if(!map.containsKey(sum)) map.put(sum, new ArrayList<>());
                map.get(sum).add(new Point(i+1, j+1));
            }
        }
        
        for(int key : map.keySet()) {
            Collections.sort(map.get(key), new Comparator<Point>() {
                @Override
                public int compare(Point p1, Point p2) {
                    if(p1.x == p2.x) return p1.y - p2.y;
                    return p1.x - p2.x;
                }
            });
        }
        
        Stack<Point> stack = new Stack<>();
        for(int key : map.keySet()) {
            Stack<Point> st = getPairs(map.get(key));
            if(st.size() > stack.size()) stack = st;
        }
        
        pl(stack.size());
        while(!stack.isEmpty()) {
            Point p = stack.pop();
            pl(p.x + " " + p.y);
        }
    }
    
    Stack<Point> getPairs(List<Point> list) {
        Stack<Point> stack = new Stack<>();
        stack.push(list.get(0));
        for(int i=1; i<list.size(); i++) {
            Point p = list.get(i);
            if(p.x >= stack.peek().x && p.x <= stack.peek().y) {
                Point p2 = stack.pop();
                if(p2.y < p.y) {
                    stack.push(p2);
                } else {
                    stack.push(p );
                }
            } else {
                stack.push(p);
            }
        }
        return stack;
    }
    
    int getSum(int[] a, int l, int r) {
        return (l == 0) ? a[r] : a[r] - a[l-1];
    }
    
    class Node {
        HashSet<Integer> adj;
        public Node() {
            adj = new HashSet<>();
        }
    }
    
    // I/O
    String nt() throws Exception {
        if(st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine(), " ");
        }
        return st.nextToken();
    }
    
    int ni() throws Exception {
        return Integer.parseInt(nt());
    }
    
    long nl() throws Exception {
        return Long.parseLong(nt());
    }
    
    void p(Object o) {
        System.out.print(o);
    }
    
    void pl(Object o) {
        System.out.println(o);
    }
}
// Problem: 1141_F2. Same Sum Blocks (Hard)
// Time complexity: quadratic
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.*;

public class P1141F {

	static BufferedReader br;
	static BufferedWriter bw;
	static StringTokenizer st;

	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		bw = new BufferedWriter(new OutputStreamWriter(System.out));

		int n = Integer.parseInt(br.readLine());
		int[] a = new int[n];
		int[][] b = new int[n][n];
		HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();
		
		st = new StringTokenizer(br.readLine());
		for(int i = 0; i < n; ++i) {
			a[i] = Integer.parseInt(st.nextToken());
			
			int r = i;
			b[r][r] = a[r];
			if (!map.containsKey(b[r][r]))
				map.put(b[r][r], new ArrayList<Integer>());
			for(int l = 0; l < r; ++l) {
				b[l][r] = b[l][r-1] + a[r];
				if (!map.containsKey(b[l][r]))
					map.put(b[l][r], new ArrayList<Integer>());
			}
		}

		
		for(int r = 0; r < n; ++r) {
			for(int l = 0; l <= r; ++l) {
				int sum = b[l][r];
				ArrayList<Integer> intervals = map.get(sum);
				int last_r = -1;
				if(!intervals.isEmpty())
					last_r = intervals.get(intervals.size()-1);
				
				if(l > last_r) {
					intervals.add(l);
					intervals.add(r);
				}
				
			}
		}
		
		//int best_sum = -1;
		ArrayList<Integer> best_intervals = new ArrayList<Integer>();
		
		for(Map.Entry<Integer, ArrayList<Integer>> entry : map.entrySet()) {
			//int sum = entry.getKey();
			ArrayList<Integer> intervals = entry.getValue();
			
			if(intervals.size() > best_intervals.size()) {
				best_intervals = intervals;
			}
		}
		
		bw.write(best_intervals.size()/2 + "\n");
		for(int i = 0; i < best_intervals.size(); i += 2) {
			bw.write((best_intervals.get(i)+1) + " " + (best_intervals.get(i+1)+1) + "\n");
		}


		br.close();
		bw.close();
	}
}
// Problem: 1141_F2. Same Sum Blocks (Hard)
// Time complexity: quadratic
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];

        arr[0] = sc.nextInt();

        for (int i = 1; i < n; i++) {
            arr[i] = arr[i - 1] + sc.nextInt();
        }

        HashMap<Integer, List<Pair>> map = new HashMap<>();

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

            if (map.containsKey(arr[i])) map.get(arr[i]).add(new Pair(0, i));
            else {
                List<Pair> l = new ArrayList<>();
                l.add(new Pair(0, i));
                map.put(arr[i], l);
            }

            for (int j = 1; j <= i; j++) {
                int ss = arr[i] - arr[j - 1];
                if (map.containsKey(ss)) map.get(ss).add(new Pair(j, i));
                else {
                    List<Pair> l = new ArrayList<>();
                    l.add(new Pair(j, i));
                    map.put(ss, l);
                }
            }
        }

        List<Pair> el = null;

        for (List<Pair> value : map.values()) {
            value.sort(Comparator.comparingInt(Pair::getStart));
            ArrayList<Pair> ps = new ArrayList<>();
            Pair last = value.get(0);

            for (int i = 1; i < value.size(); i++) {
                if (last.getEnd() < value.get(i).getStart()) {
                    ps.add(last);
                    last = value.get(i);
                }
                else if (last.getEnd() > value.get(i).getEnd()) last = value.get(i);
            }

            ps.add(last);

            if (el == null) el = ps;
            else if (ps.size() > el.size()) el = ps;
        }

        System.out.println(el.size());

        for (Pair pair : el) {
            System.out.println((pair.getStart() + 1) + " " + (pair.getEnd() + 1));
        }

    }
}

class Pair {
    private final int start;
    private final int end;

    public int getStart() {
        return start;
    }

    public int getEnd() {
        return end;
    }

    public Pair(int start, int end) {
        this.start = start;
        this.end = end;
    }
}
// Problem: 1141_F2. Same Sum Blocks (Hard)
// Time complexity: quadratic
import java.io.*;
import java.util.*;

public class Soln {
	
	public static int[] io(int n) {
    	int[] d = new int[n];
    	for (int i=0;i<n;i++) d[i] = f.nextInt();
    	return d;
	}
	public static int binlog( int bits ){
        int log = 0;
        if( ( bits & 0xffff0000 ) != 0 ) { bits >>>= 16; log = 16; }
        if( bits >= 256 ) { bits >>>= 8; log += 8; }
        if( bits >= 16  ) { bits >>>= 4; log += 4; }
        if( bits >= 4   ) { bits >>>= 2; log += 2; }
        return log + ( bits >>> 1 );
    }
	static class FastReader { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastReader() 
        { 
            br = new BufferedReader(new
                     InputStreamReader(System.in)); 
        } 
  
        String next() 
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 
  
        int nextInt() 
        { 
            return Integer.parseInt(next()); 
        } 
  
        long nextLong() 
        { 
            return Long.parseLong(next()); 
        } 
  
        double nextDouble() 
        { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() 
        { 
            String str = ""; 
            try
            { 
                str = br.readLine(); 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            return str; 
        } 
	}
	public static final long mod = (long)Math.pow(10, 9) + 7;
	static FastReader f=new FastReader();
	
	public static void main (String[] args) throws IOException{
		int n = f.nextInt();
		int[] a = io(n);
		HashMap<Integer,ArrayList<ivl>> hm = new HashMap<>();
		for (int i=0;i<n;i++) {
			int sum = 0;
			for (int j=i;j<n;j++) {
				sum+=a[j];
				if (hm.get(sum)==null) hm.put(sum,new ArrayList<ivl>());
				hm.get(sum).add(new ivl(i,j));
			}
		}
		HashSet<ivl> hs = new HashSet<ivl>();
		
		for (ArrayList<ivl> arr : hm.values()) {
			Collections.sort(arr,new comp());
			HashSet<ivl> temp = new HashSet<ivl>();
			temp.add(arr.get(0));
			int lastr = arr.get(0).r;
			int num = 1;
			for (ivl curr:arr) {
				if (curr.l>lastr) {
					lastr = curr.r;
					num++;
					temp.add(curr);
				}
			}
			if (temp.size()>hs.size()) hs = temp;
		}
		
		System.out.println(hs.size());
		for (ivl curr:hs) {
			System.out.println((curr.l+1)+" "+(curr.r+1));
		}
    }
	
	static class ivl{
		int l,r;
		ivl(int l,int r){
			this.l=l;this.r=r;
		}
	}
	
	static class comp implements Comparator<ivl>{
		public int compare(ivl a,ivl b) {
			if (a.r - b.r == 0) return a.l-b.l;
			return a.r-b.r;
		}
	}
}