Techno Blender
Digitally Yours.

Find the longest Substring of a given String S

0 26


import java.io.*;

import java.util.*;

  

public class GFG {

  

    

    

    public static void maxLength(int lis[], int n)

    {

        long or = 0;

  

        

        

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

            or |= lis[i];

        }

  

        int[] count = new int[32];

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

            for (int j = 0; j < 32; j++) {

                if ((lis[i] & (1L << j)) != 0)

                    count[j]++;

            }

        }

  

        long ans = 0, l = 0, tempOr = 0;

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

            tempOr |= lis[r];

  

            boolean flag = true;

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

                if ((lis[r] & (1L << i)) != 0) {

                    count[i]--;

                    if (count[i] == 0)

                        flag = false;

                }

            }

  

            

            if (!flag) {

                for (; l <= r;) {

                    boolean flag1 = true;

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

                        if ((lis[(int)l] & (1L << i))

                            != 0) {

                            count[i]++;

                        }

                        if (count[i] == 0)

                            flag1 = false;

                    }

                    l++;

                    if (flag1)

                        break;

                }

            }

  

            

            

            if (tempOr == or) {

  

                ans = Math.max(ans, r - l + 1);

            }

        }

  

        

        if (ans != 0)

            System.out.println(ans);

        else

            System.out.println(-1);

    }

  

    

    public static void main(String[] args)

    {

        String S = "2347";

        int N = S.length();

        int[] A = new int[N];

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

            A[i] = S.charAt(i) - '0';

        }

  

        

        maxLength(A, N);

    }

}


import java.io.*;

import java.util.*;

  

public class GFG {

  

    

    

    public static void maxLength(int lis[], int n)

    {

        long or = 0;

  

        

        

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

            or |= lis[i];

        }

  

        int[] count = new int[32];

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

            for (int j = 0; j < 32; j++) {

                if ((lis[i] & (1L << j)) != 0)

                    count[j]++;

            }

        }

  

        long ans = 0, l = 0, tempOr = 0;

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

            tempOr |= lis[r];

  

            boolean flag = true;

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

                if ((lis[r] & (1L << i)) != 0) {

                    count[i]--;

                    if (count[i] == 0)

                        flag = false;

                }

            }

  

            

            if (!flag) {

                for (; l <= r;) {

                    boolean flag1 = true;

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

                        if ((lis[(int)l] & (1L << i))

                            != 0) {

                            count[i]++;

                        }

                        if (count[i] == 0)

                            flag1 = false;

                    }

                    l++;

                    if (flag1)

                        break;

                }

            }

  

            

            

            if (tempOr == or) {

  

                ans = Math.max(ans, r - l + 1);

            }

        }

  

        

        if (ans != 0)

            System.out.println(ans);

        else

            System.out.println(-1);

    }

  

    

    public static void main(String[] args)

    {

        String S = "2347";

        int N = S.length();

        int[] A = new int[N];

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

            A[i] = S.charAt(i) - '0';

        }

  

        

        maxLength(A, N);

    }

}

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment