Dado un valor impar N , la tarea es encontrar el valor máximo de (a+b) (donde a y b son números enteros) tal que (a 2 – b 2 = N)
Ejemplos:
Input: N = 1 Output: 1 Since a*a - b*b = 1 The maximum value occurs when a = 1 and b = 0. Thus, a + b = 1. Input: N = 3 Output: 3 Since a*a - b*b = 3 The maximum value occurs when a = 2 and b = 1. Thus, a + b = 3.
Acercarse:
- Dado
a*a - b*b = N => (a+b)*(a-b) = N => (a+b) = N/(a-b)
- Ahora, para que la ecuación anterior sea cierta
We know that |a - b| ≥ 1 Therefore, a + b ≤ N
- Ahora para maximizar el valor de (a + b),
Maximising a + b ≤ N => a + b = N
- Por lo tanto, N es el valor máximo de (a + b) cuando necesitamos maximizar el valor de (a+b) tal que (a*ab*b = N).
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to maximize the value // of (a+b) such that (a*a-b*b = N) #include <bits/stdc++.h> using namespace std; // Function to maximize the value // of (a+b) such that (a*a-b*b = n) int maxValue(int n) { return n; } // Driver code int main() { int n = 1; cout << maxValue(n); return 0; }
Java
// Java program to maximize the value // of (a+b) such that (a*a-b*b = N) class GFG { // Function to maximize the value // of (a+b) such that (a*a-b*b = n) static int maxValue(int n) { return n; } // Driver code public static void main(String[] args) { int n = 1; System.out.print(maxValue(n)); } } // This code is contributed by 29AjayKumar
Python3
# Python3 program to maximize the value # of (a+b) such that (a*a-b*b = N) # Function to maximize the value # of (a+b) such that (a*a-b*b = n) def maxValue(n) : return n; # Driver code if __name__ == "__main__" : n = 1; print(maxValue(n)); # This code is contributed by AnkitRai01
C#
// C# program to maximize the value // of (a+b) such that (a*a-b*b = N) using System; class GFG { // Function to maximize the value // of (a+b) such that (a*a-b*b = n) static int maxValue(int n) { return n; } // Driver code public static void Main(String[] args) { int n = 1; Console.Write(maxValue(n)); } } // This code is contributed by PrinciRaj1992
Javascript
<script> // Javascript program to maximize the value // of (a+b) such that (a*a-b*b = N) // Function to maximize the value // of (a+b) such that (a*a-b*b = n) function maxValue(n) { return n; } // Driver code var n = 1; document.write(maxValue(n)); </script>
Producción:
1
Complejidad del tiempo: