Find maximum value of Indices of Array that satisfy the given conditions
Last Updated :
26 Dec, 2022
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++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
static void calculateIndices(int N)
{
// Variable to store difference at
// each iteration of while Loop
long Z = 0;
// Counter for calculating
// power of 2
long Pow_counter = 1;
// Variable K to store sum of
// elements of X[]
long K = 0;
// Variable K to store sum of
// elements of X[]
long L = 0;
// Variable to store Max value of
// index at which Z >= 0
long A = 0;
// Variable to store Max value of
// index at which Z is maximum in
// all iterations of while loop
long B = 0;
// max variable to store maximum
// value of Z
long max = LONG_MIN;
// While loop to execute
// till Z >= 0
while (Z >= 0) {
// Updating value of K
K += N;
// Updating value of L
L += pow(2, Pow_counter - 1);
// Incrementing A
A++;
// Updating value of Z
Z = K - L;
// If Z is maxed till now
if (Z > max) {
// Update max variable as Z
max = Z;
// Update B as max_counter
B = Pow_counter;
}
// Incrementing variable
// Pow_counter, if Z is
// non-negative(Z >= 0)
if (Z >= 0)
Pow_counter++;
}
// Printing value of Z
cout << (A - 1) << " " << B << endl;
}
// Driver code
int main()
{
long N = 6;
calculateIndices(N);
return 0;
}
// This code is contributed by Tapesh(tapeshdua420)
Java
// Java code for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static void calculateIndices(long N)
{
// Variable to store difference at
// each iteration of while Loop
long Z = 0;
// Counter for calculating
// power of 2
long Pow_counter = 1;
// Variable K to store sum of
// elements of X[]
long K = 0;
// Variable K to store sum of
// elements of X[]
long L = 0;
// Variable to store Max value of
// index at which Z >= 0
long A = 0;
// Variable to store Max value of
// index at which Z is maximum in
// all iterations of while loop
long B = 0;
// max variable to store maximum
// value of Z
long max = Long.MIN_VALUE;
// While loop to execute
// till Z >= 0
while (Z >= 0) {
// Updating value of K
K += N;
// Updating value of L
L += Math.pow(2, Pow_counter - 1);
// Incrementing A
A++;
// Updating value of Z
Z = K - L;
// If Z is maxed till now
if (Z > max) {
// Update max variable as Z
max = Z;
// Update B as max_counter
B = Pow_counter;
}
// Incrementing variable
// Pow_counter, if Z is
// non-negative(Z >= 0)
if (Z >= 0)
Pow_counter++;
}
// Printing value of Z
System.out.println((A - 1) + " " + B);
}
// Driver code
public static void main(String args[])
{
long N = 6;
calculateIndices(N);
}
}
Python3
# Python code for the above approach
import math
# Function to calculate indices
def calculateIndices(N):
# Variable to store difference at
# each iteration of while Loop
Z = 0
# Counter for calculating
# power of 2
Pow_counter = 1
# Variable K to store sum of
# elements of X[]
K = 0
# Variable K to store sum of
# elements of X[]
L = 0
# Variable to store Max value of
# index at which Z >= 0
A = 0
# Variable to store Max value of
# index at which Z is maximum in
# all iterations of while loop
B = 0
# max variable to store maximum
# value of Z
max = -math.inf
# While loop to execute
# till Z >= 0
while (Z >= 0):
# Updating value of K
K += N
# Updating value of L
L += pow(2, Pow_counter - 1)
# Incrementing A
A += 1
# Updating value of Z
Z = K - L
# If Z is maxed till now
if (Z > max):
# Update max variable as Z
max = Z
# Update B as max_counter
B = Pow_counter
# Incrementing variable
# Pow_counter, if Z is
# non-negative(Z >= 0)
if (Z >= 0):
Pow_counter += 1
# Printing value of Z
print(A - 1, B)
# Driver code
N = 6
calculateIndices(N)
# This code is contributed by Tapesh(tapeshdua420)
C#
// C# code for the above approach
using System;
public class GFG{
static void calculateIndices(long N)
{
// Variable to store difference at
// each iteration of while Loop
long Z = 0;
// Counter for calculating
// power of 2
long Pow_counter = 1;
// Variable K to store sum of
// elements of X[]
long K = 0;
// Variable K to store sum of
// elements of X[]
long L = 0;
// Variable to store Max value of
// index at which Z >= 0
long A = 0;
// Variable to store Max value of
// index at which Z is maximum in
// all iterations of while loop
long B = 0;
// max variable to store maximum
// value of Z
long max = Int64.MinValue;
// While loop to execute
// till Z >= 0
while (Z >= 0) {
// Updating value of K
K += N;
// Updating value of L
L += (long)Math.Pow(2, Pow_counter - 1);
// Incrementing A
A++;
// Updating value of Z
Z = K - L;
// If Z is maxed till now
if (Z > max) {
// Update max variable as Z
max = Z;
// Update B as max_counter
B = Pow_counter;
}
// Incrementing variable
// Pow_counter, if Z is
// non-negative(Z >= 0)
if (Z >= 0)
Pow_counter++;
}
// Printing value of Z
Console.WriteLine((A - 1) + " " + B);
}
static public void Main (){
// Code
long N = 6;
calculateIndices(N);
}
}
// This code is contributed by lokeshmvs21.
JavaScript
<script>
// JavaScript code for the above approach
function calculateIndices(N) {
// Variable to store difference at
// each iteration of while Loop
var Z = 0;
// Counter for calculating
// power of 2
var Pow_counter = 1;
// Variable K to store sum of
// elements of X[]
var K = 0;
// Variable K to store sum of
// elements of X[]
var L = 0;
// Variable to store Max value of
// index at which Z >= 0
var A = 0;
// Variable to store Max value of
// index at which Z is maximum in
// all iterations of while loop
var B = 0;
// max variable to store maximum
// value of Z
var max = -Infinity;
// While loop to execute
// till Z >= 0
while (Z >= 0) {
// Updating value of K
K += N;
// Updating value of L
L += Math.pow(2, Pow_counter - 1);
// Incrementing A
A += 1;
// Updating value of Z
Z = K - L;
// If Z is maxed till now
if (Z > max) {
// Update max variable as Z
max = Z;
// Update B as max_counter
B = Pow_counter;
}
// Incrementing variable
// Pow_counter, if Z is
// non-negative(Z >= 0)
if (Z >= 0) {
Pow_counter += 1;
}
}
// Printing value of Z
console.log(A - 1, B);
}
// Driver code
var N = 6;
calculateIndices(N);
// This code is contributed by Tapesh(tapeshdua420)
</script>
Time Complexity: O(A)
Auxiliary Space: O(1)
Similar Reads
Count of triplets in an array that satisfy the given conditions Given an array arr[] of N elements, the task is to find the count of triplets (arr[i], arr[j], arr[k]) such that (arr[i] + arr[j] + arr[k] = L) and (L % arr[i] = L % arr[j] = L % arr[k] = 0.Examples: Input: arr[] = {2, 4, 5, 6, 7} Output: 1 Only possible triplet is {2, 4, 6}Input: arr[] = {4, 4, 4,
13 min read
Smallest index in the given array that satisfies the given condition Given an array arr[] of size N and an integer K, the task is to find the smallest index in the array such that: floor(arr[i] / 1) + floor(arr[i + 1] / 2) + floor(arr[i + 2] / 3) + ..... + floor(arr[n - 1] / n - i ) ? K If no such index is found then print -1. Examples: Input: arr[] = {6, 5, 4, 2}, K
6 min read
Check if there exist 4 indices in the array satisfying the given condition Given an array A[] of N positive integers and 3 integers X, Y, and Z, the task is to check if there exist 4 indices (say p, q, r, s) such that the following conditions are satisfied: 0 < p < q < r < s < NSum of the subarray from A[p] to A[q - 1] is XSum of the subarray from A[q] to A[
8 min read
Minimum increment/decrement operations required on Array to satisfy given conditions Given an array arr[] of size N, the task is to find the minimum number of increment or decrement operations required at any index i such that for each i (1 ? i < N) if the sum of elements at index from 1 to i is positive then the sum of elements from 1 to i + 1 must be negative or vice versa. Not
11 min read
Count of elements that are Kth powers of their indices in given Array Given an array arr[] with N non-negative integers, the task is to find the number of elements that are Kth powers of their indices, where K is a non-negative number. arr[i] = iK Example: Input: arr = [1, 1, 4, 3, 16, 125, 1], K = 0Output: 3Explanation: 3 elements are Kth powers of their indices:00 i
9 min read