Find maximum value of Indices of Array that satisfy the given conditions
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:
C++
#include <bits/stdc++.h>
using namespace std;
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;
while (Z >= 0) {
K += N;
L += pow (2, Pow_counter - 1);
A++;
Z = K - L;
if (Z > max) {
max = Z;
B = Pow_counter;
}
if (Z >= 0)
Pow_counter++;
}
cout << (A - 1) << " " << B << endl;
}
int main()
{
long N = 6;
calculateIndices(N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static void calculateIndices( long 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);
}
}
|
Python3
import math
def calculateIndices(N):
Z = 0
Pow_counter = 1
K = 0
L = 0
A = 0
B = 0
max = - math.inf
while (Z > = 0 ):
K + = N
L + = pow ( 2 , Pow_counter - 1 )
A + = 1
Z = K - L
if (Z > max ):
max = Z
B = Pow_counter
if (Z > = 0 ):
Pow_counter + = 1
print (A - 1 , B)
N = 6
calculateIndices(N)
|
C#
using System;
public class GFG{
static void calculateIndices( long N)
{
long Z = 0;
long Pow_counter = 1;
long K = 0;
long L = 0;
long A = 0;
long B = 0;
long max = Int64.MinValue;
while (Z >= 0) {
K += N;
L += ( long )Math.Pow(2, Pow_counter - 1);
A++;
Z = K - L;
if (Z > max) {
max = Z;
B = Pow_counter;
}
if (Z >= 0)
Pow_counter++;
}
Console.WriteLine((A - 1) + " " + B);
}
static public void Main (){
long N = 6;
calculateIndices(N);
}
}
|
Javascript
<script>
function calculateIndices(N) {
var Z = 0;
var Pow_counter = 1;
var K = 0;
var L = 0;
var A = 0;
var B = 0;
var max = -Infinity;
while (Z >= 0) {
K += N;
L += Math.pow(2, Pow_counter - 1);
A += 1;
Z = K - L;
if (Z > max) {
max = Z;
B = Pow_counter;
}
if (Z >= 0) {
Pow_counter += 1;
}
}
console.log(A - 1, B);
}
var N = 6;
calculateIndices(N);
</script>
|
Time Complexity: O(A)
Auxiliary Space: O(1)
Last Updated :
26 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...