Tuesday, December 6, 2022
HomeSoftware DevelopmentDiscover most worth of Indices of Array that fulfill the given circumstances

Discover most worth of Indices of Array that fulfill the given circumstances


Given an integer N (N ≥ 5) Then assume you might have two infinite arrays X and Y the place X[] is an array of ingredient N and every ingredient of Y[] is 2i the place i is the index of the array, the duty is to search out two indices let’s say A and B that are the most worth of the index at which the prefix sum in X[] is at the very least the identical because the prefix sum of Y[] and the index worth at which the X[i] – Y[i] is most respectively, The place The worth of B must be lower than or equal to A. Formally, B ≤ A.

Examples:

Enter: N = 7
Output: A = 5, B = 3
Rationalization: Y[] = {1, 2, 4, 8….., 2i – n}, X[] = {7, 7, 7, 7, …..7}
The sum of X[] until index 5 is: 35 
The sum of Y[] until index 5 is: 31
5 is the utmost worth of index at which, The sum of components of X[] is strictly larger than or equal to Y[]. Most attainable distinction of sum(Xi – Yi) is at index 3, Which is 14. Due to this fact, A = 5 and B = 3.

Enter: N = 6
Output: A = 4, B = 3
Rationalization: It may be verified that 4 is most attainable worth of index at which sum of components of X[] is strictly larger than or equal to Y[] and max distinction is at index 3. Due to this fact, A = 4 and B = 3.

Method: Implement the thought under to resolve the issue:

Take two lengthy knowledge kind integers shall we say Okay and L to retailer sum of X and Y respectively, run some time loop until the sum distinction is bigger than or equal to zero

Observe the steps to resolve the issue:

  • Take two lengthy knowledge kind variables let’s say Okay and L to retailer the sum of X[] and Y[] respectively until index i, different two integers A and B for output as mentioned in the issue assertion. 
  • Take one other variable Z, and initialize it to 0, Which can use to retailer the distinction at index i as X[i] – Y[i]. Run a Whereas loop until Z ≥ 0, and observe the under steps contained in the loop :
    • Replace the worth of Okay and L.
    • Replace the worth of Z as Xi – Yi.
    • Increment A.
    • If Z is the utmost present worth of the index at i, Then replace B as i.
  • Print the worth of A and B.  

Beneath is the code to implement the above strategy:

Java

  

import java.io.*;

import java.lang.*;

import java.util.*;

  

class GFG {

  

    static void calculateIndices(int N)

    {

        

        

        lengthy Z = 0;

  

        

        

        lengthy Pow_counter = 1;

  

        

        

        lengthy Okay = 0;

  

        

        

        lengthy L = 0;

  

        

        

        lengthy A = 0;

  

        

        

        

        lengthy B = 0;

  

        

        

        lengthy max = Lengthy.MIN_VALUE;

  

        

        

        whereas (Z >= 0) {

  

            

            Okay += N;

  

            

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

  

            

            A++;

  

            

            Z = Okay - L;

  

            

            if (Z > max) {

  

                

                max = Z;

  

                

                B = Pow_counter;

            }

  

            

            

            

            if (Z >= 0)

                Pow_counter++;

        }

  

        

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

    }

  

    

    public static void predominant(String args[])

    {

        lengthy N = 6;

        calculateIndices(N);

    }

}

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments