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
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
4 min read
Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read