Techno Blender
Digitally Yours.

Find maximum value of Indices of Array that satisfy the given conditions

0 28


Given an integer N (N ≥ 5) Then assume you have two infinite arrays X and Y where X[] is an array of element N and each element of Y[] is 2i where i is the index of the array, the task is to find two indices let’s say A and B which are the maximum value of the index at which the prefix sum in X[] is at least the same as the prefix sum of Y[] and the index value at which the X[i] – Y[i] is maximum respectively, Where The value of B should be less than or equal to A. Formally, B ≤ A.

Examples:

Input: N = 7
Output: A = 5, B = 3
Explanation: Y[] = {1, 2, 4, 8….., 2i – n}, X[] = {7, 7, 7, 7, …..7}
The sum of X[] till index 5 is: 35 
The sum of Y[] till index 5 is: 31
5 is the maximum value of index at which, The sum of elements of X[] is strictly greater than or equal to Y[]. Maximum possible difference of sum(Xi – Yi) is at index 3, Which is 14. Therefore, A = 5 and B = 3.

Input: N = 6
Output: A = 4, B = 3
Explanation: It can be verified that 4 is maximum possible value of index at which sum of elements of X[] is strictly greater than or equal to Y[] and max difference is at index 3. Therefore, A = 4 and B = 3.

Approach: Implement the idea below to solve the problem:

Take two long data type integers lets say K and L to store sum of X and Y respectively, run a while loop till the sum difference is greater than or equal to zero

Follow the steps to solve the problem:

  • Take two long data type variables let’s say K and L to store the sum of X[] and Y[] respectively till index i, other two integers A and B for output as discussed in the problem statement. 
  • Take another variable Z, and initialize it to 0, Which will use to store the difference at index i as X[i] – Y[i]. Run a While loop till Z ≥ 0, and follow the below steps inside the loop :
    • Update the value of K and L.
    • Update the value of Z as Xi – Yi.
    • Increment A.
    • If Z is the maximum current value of the index at i, Then update B as i.
  • Print the value of A and B.  

Below is the code to implement the above approach:

Java

  

import java.io.*;

import java.lang.*;

import java.util.*;

  

class GFG {

  

    static void calculateIndices(int N)

    {

        

        

        long Z = 0;

  

        

        

        long Pow_counter = 1;

  

        

        

        long K = 0;

  

        

        

        long L = 0;

  

        

        

        long A = 0;

  

        

        

        

        long B = 0;

  

        

        

        long max = Long.MIN_VALUE;

  

        

        

        while (Z >= 0) {

  

            

            K += N;

  

            

            L += Math.pow(2, Pow_counter - 1);

  

            

            A++;

  

            

            Z = K - L;

  

            

            if (Z > max) {

  

                

                max = Z;

  

                

                B = Pow_counter;

            }

  

            

            

            

            if (Z >= 0)

                Pow_counter++;

        }

  

        

        System.out.println((A - 1) + " " + B);

    }

  

    

    public static void main(String args[])

    {

        long N = 6;

        calculateIndices(N);

    }

}

Time Complexity: O(A)
Auxiliary Space: O(1)  


Given an integer N (N ≥ 5) Then assume you have two infinite arrays X and Y where X[] is an array of element N and each element of Y[] is 2i where i is the index of the array, the task is to find two indices let’s say A and B which are the maximum value of the index at which the prefix sum in X[] is at least the same as the prefix sum of Y[] and the index value at which the X[i] – Y[i] is maximum respectively, Where The value of B should be less than or equal to A. Formally, B ≤ A.

Examples:

Input: N = 7
Output: A = 5, B = 3
Explanation: Y[] = {1, 2, 4, 8….., 2i – n}, X[] = {7, 7, 7, 7, …..7}
The sum of X[] till index 5 is: 35 
The sum of Y[] till index 5 is: 31
5 is the maximum value of index at which, The sum of elements of X[] is strictly greater than or equal to Y[]. Maximum possible difference of sum(Xi – Yi) is at index 3, Which is 14. Therefore, A = 5 and B = 3.

Input: N = 6
Output: A = 4, B = 3
Explanation: It can be verified that 4 is maximum possible value of index at which sum of elements of X[] is strictly greater than or equal to Y[] and max difference is at index 3. Therefore, A = 4 and B = 3.

Approach: Implement the idea below to solve the problem:

Take two long data type integers lets say K and L to store sum of X and Y respectively, run a while loop till the sum difference is greater than or equal to zero

Follow the steps to solve the problem:

  • Take two long data type variables let’s say K and L to store the sum of X[] and Y[] respectively till index i, other two integers A and B for output as discussed in the problem statement. 
  • Take another variable Z, and initialize it to 0, Which will use to store the difference at index i as X[i] – Y[i]. Run a While loop till Z ≥ 0, and follow the below steps inside the loop :
    • Update the value of K and L.
    • Update the value of Z as Xi – Yi.
    • Increment A.
    • If Z is the maximum current value of the index at i, Then update B as i.
  • Print the value of A and B.  

Below is the code to implement the above approach:

Java

  

import java.io.*;

import java.lang.*;

import java.util.*;

  

class GFG {

  

    static void calculateIndices(int N)

    {

        

        

        long Z = 0;

  

        

        

        long Pow_counter = 1;

  

        

        

        long K = 0;

  

        

        

        long L = 0;

  

        

        

        long A = 0;

  

        

        

        

        long B = 0;

  

        

        

        long max = Long.MIN_VALUE;

  

        

        

        while (Z >= 0) {

  

            

            K += N;

  

            

            L += Math.pow(2, Pow_counter - 1);

  

            

            A++;

  

            

            Z = K - L;

  

            

            if (Z > max) {

  

                

                max = Z;

  

                

                B = Pow_counter;

            }

  

            

            

            

            if (Z >= 0)

                Pow_counter++;

        }

  

        

        System.out.println((A - 1) + " " + B);

    }

  

    

    public static void main(String args[])

    {

        long N = 6;

        calculateIndices(N);

    }

}

Time Complexity: O(A)
Auxiliary Space: O(1)  

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