Maximize number from given integers using following operations
Last Updated :
09 Mar, 2023
Given positive integers N, A, B and C such that 1 ≤ C < N. The task is to maximize the value of B by performing the following operations:
- A can be incremented by 1 by decreasing 1 from N.
- Increase the value of B by B + A by subtracting C from N,
The above operations can be performed till N ≤ 0.
Examples:
Input: N = 6, A = 0, B = 0, C = 1
Output: 9
Explanation: First increase the value of A three times.
After this A become 3 and N = N - 3 that is N become 3.
Then increase the value of B by A that is B = B + A now B = 3,
and decrease the value of N by C that is N = N - C which is 2.
Repeat the previous step two times and finally B will become 9
and then N will become 0.
This is the maximum value that can be obtained by B
Input: N = 10, A = 0, B = 0, C = 2
Output: 12
Explanation: First increase the value of A four times.
After this A become 4 and N=N - 4 that is N become 6.
Then increase the value of B by A that is B = B + A now B = 4,
and decrease the value of N by C that is N = N - C which is 4.
Repeat the previous step two times and finally B will become 12
and N will become 0.
This is the maximum value that can be obtained by B.
Approach: To solve the problem follow the below idea:
The best time to check for each possible value of A, what can be the value of B. The maximum value among these values is the required answer.
It is always beneficial to keep the values of A in a manner such till N%C = 0, because that will result in no wastage of operations.
Follow the steps to solve the problem:
- First of all A has to be incremented to 1 because if B = B + A is performed, then B will always remain 0.
- Run a loop while N > 0.
- If (N%C = 0) then check the final value of B after performing all remaining operations of B = B + A.
- Update the maximum value of B.
- Else return B because further you can not find maximum value.
- Otherwise, just increase the value of A = A + 1 and decrease the value of N = N - 1.
- Return the maximum value of B.
Below is the implementation for the above approach:
C++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the maximum value of B
int maxB(int N, int A, int B, int C)
{
// Making A = 1
A = 1;
N -= 1;
// Doing process till N > 0
while (N > 0) {
// If we can increase the value of B
// constantly till N = 0
if (N % C == 0) {
// How many times we can increase B
int temp = N / C;
// Checking if we can get more
// value of B as previous
if (B < temp * A) {
B = temp * A;
A++;
N--;
}
else
return B;
}
else {
A++;
N--;
}
}
return B;
}
// Driver code
int main()
{
int N = 10, A = 0, B = 0, C = 2;
// Function call
cout << maxB(N, A, B, C) << endl;
return 0;
}
C
// C code to implement the approach
#include <stdio.h>
// Function to find the maximum value of B
int maxB(int N, int A, int B, int C)
{
// Making A = 1
A = 1;
N -= 1;
// Doing process till N > 0
while (N > 0) {
// If we can increase the value of B
// constantly till N = 0
if (N % C == 0) {
// How many times we can increase B
int temp = N / C;
// Checking if we can get more
// value of B as previous
if (B < temp * A) {
B = temp * A;
A++;
N--;
}
else
return B;
}
else {
A++;
N--;
}
}
return B;
}
// Driver Code
int main()
{
int N = 10, A = 0, B = 0, C = 2;
// Function call
int ans = maxB(N, A, B, C);
// Printing answer
printf("%d", ans);
return 0;
}
Java
// Java code to implement the approach
import java.io.*;
class GFG {
// Function to find the maximum value of B
static int maxB(int N, int A, int B, int C)
{
// Making A = 1
A = 1;
N -= 1;
// Doing process till N > 0
while (N > 0) {
// If we can increase the value of B
// constantly till N = 0
if (N % C == 0) {
// How many times we can increase B
int temp = N / C;
// Checking if we can get more
// value of B as previous
if (B < temp * A) {
B = temp * A;
A++;
N--;
}
else
return B;
}
else {
A++;
N--;
}
}
return B;
}
// Driver Code
public static void main(String[] args)
{
int N = 10, A = 0, B = 0, C = 2;
// Function call
int ans = maxB(N, A, B, C);
// Printing answer
System.out.println(ans);
}
}
Python3
# Python code to implement the approach
# Function to find the maximum value of B
def maxB(N, A, B, C):
# Making A = 1
A = 1
N -= 1
# Doing process till N > 0
while (N > 0):
# If we can increase the value of B
# constantly till N = 0
if (N % C == 0):
# How many times we can increase B
temp = N // C
# Checking if we can get more
# value of B as previous
if (B < temp * A):
B = temp * A
A += 1
N -= 1
else:
return B
else:
A += 1
N -= 1
return B
# Driver code
if __name__ == "__main__":
N = 10
A = 0
B = 0
C = 2
# Function call
print(maxB(N, A, B, C))
# This code is contributed by Rohit Pradhan
C#
// C# program of above approach.
using System;
public class GFG{
// Function to find the maximum value of B
static int maxB(int N, int A, int B, int C)
{
// Making A = 1
A = 1;
N -= 1;
// Doing process till N > 0
while (N > 0) {
// If we can increase the value of B
// constantly till N = 0
if (N % C == 0) {
// How many times we can increase B
int temp = N / C;
// Checking if we can get more
// value of B as previous
if (B < temp * A) {
B = temp * A;
A++;
N--;
}
else
return B;
}
else {
A++;
N--;
}
}
return B;
}
// Driver code
static public void Main ()
{
int N = 10, A = 0, B = 0, C = 2;
// Function call
int ans = maxB(N, A, B, C);
// Printing answer
Console.Write(ans);
}
}
// This code is contributed by sanjoy_62.
JavaScript
<script>
// Javascript code to implement the approach
// Function to find the maximum value of B
function maxB(N, A, B, C) {
// Making A = 1
A = 1;
N -= 1;
// Doing process till N > 0
while (N > 0){
// If we can increase the value of B
// constantly till N = 0
if (N % C == 0){
// How many times we can increase B
let temp = Math.floor(N /C);
// Checking if we can get more
// value of B as previous
if (B < temp * A){
B = temp * A;
A += 1;
N -= 1;
}
else{
return B;
}
}
else{
A += 1;
N -= 1;
}
}
return B;
}
// Driver code
let N = 10;
let A = 0;
let B = 0;
let C = 2;
// Function call
document.write(maxB(N, A, B, C));
// This code is contributed by AnkThon
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Maximize a given unsigned number by swapping bits at it's extreme positions. Given a number maximize it by swapping bits at its extreme positions i.e. at the first and last position, second and second last position, and so on. Examples: Input : 4 (0000...0100) Output : 536870912 (0010...0000) In the above example swapped 3rd and 3rd last bit to maximize the given unsigned nu
5 min read
Maximise minimum element possible in Array after performing given operations Given an array arr[] of size N. The task is to maximize the minimum value of the array after performing given operations. In an operation, value x can be chosen and A value 3 * x can be subtracted from the arr[i] element.A value x is added to arr[i-1]. andA value of 2 * x can be added to arr[i-2]. F
11 min read
Maximize the number of local maxima's with minimum operations Given an array A[] of size N (where N is even), Local maxima are array elements strictly greater in value than its adjacent elements i.e., A[i - 1] < A[i] > A[i + 1]. For all 2 ⤠i ⤠N - 1. The first and last element of the array can never be Local maxima. The task is to maximize the number of
15+ min read
Find maximum in an array without using Relational Operators Given an array A[] of non-negative integers, find the maximum in the array without using Relational Operator. Examples: Input : A[] = {2, 3, 1, 4, 5}Output : 5Input : A[] = {23, 17, 93}Output : 93We use repeated subtraction to find out the maximum. To find maximum between two numbers, we take a vari
9 min read
Maximize array elements upto given number Given an array of integers, a number and a maximum value, task is to compute the maximum value that can be obtained from the array elements. Every value on the array traversing from the beginning can be either added to or subtracted from the result obtained from previous index such that at any point
15+ min read