Maximize number from given integers using following operations
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 BInput: 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.
- If (N%C = 0) then check the final value of B after performing all remaining operations of B = B + A.
- 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> |
12
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...