[ad_1]
Given an integer N, discover a sequence of N integers such that the sum of all integers within the sequence is the same as the sum of any two adjoining integers within the sequence.
Examples:
Enter: N = 4
Output: 1 -1 1 -1
Clarification: Whole sum of the 4 integers is 0 and the sum of any two adjoining integers can be 0.Enter: N = 5
Output: 1 -2 1 -2 1
Clarification: Whole sum of the 4 integers is -1 and the sum of any two adjoining integers can be -1.
Method: To resolve the issue observe the under thought:
- For even n, we are able to use the array [-1, 1, -1, 1, …, -1, 1] as an answer. This array has a size of n, and the sum of any two adjoining components is 0, in addition to the sum of the entire array.
- For odd n, we are able to assemble an array s of size n utilizing two mounted values a and b, such that si-1 = si+1 for i = 2, 3, …n-1. Particularly, we are able to set s1 = a and s2 = b, after which create the array s = [a, b, a, b, …, a, b, a]. We are able to then resolve for ‘a’ and ‘b’ utilizing the truth that the sum of any two adjoining components and the sum of the entire array have to be equal.
- If we let ok be a optimistic integer such that n = 2k+1, then we are able to discover values for ‘a’ and ‘b’ that fulfill the circumstances. Particularly, we are able to set a = k-1 and b = -k, which produces the array [k-1, -k, k-1, -k, …, k-1, -k, k-1]. This array has a size of n, and the sum of any two adjoining components is k-1-k = -1, in addition to the sum of the entire array being ok(k-1)-(k-1)ok = 0.
- Nonetheless, there isn’t any answer for n = 3, as a = 0 and b = 0 don’t fulfill the circumstances. Generally, the array we constructed won’t work if a or b is the same as 0. Due to this fact, we should have ok ≥ 2 to make sure that each a and b are nonzero.
- General, this method reveals that if there’s a answer for even n, then there’s a answer for odd n better than or equal to five.
Comply with the steps to resolve the issue:
- Initialize an integer variable ‘num’ with the worth N/2.
- If N is even print 1, -1, ………, 1, -1
- If N is odd :
- iterate by the sequence of integers from 1 to N.
- If i is odd, print -num, in any other case print num-1.
Under is the implementation for the above method:
C++
// C++ code for the above method: #embrace <bits/stdc++.h> utilizing namespace std; // Perform to resolve the issue void resolve(int n) { // If n is odd if (n % 2) { // Calculate the quantity to be // added and subtracted // alternatively int num = n / 2; // Iterate by the sequence // and print the values for (int i = 0; i < n; i++) { if (i % 2) // If i is odd, print // the unfavorable quantity cout << -num << " "; // In any other case, print the // optimistic quantity else cout << num - 1 << " "; } // Print a newline character // on the finish cout << endl; } // If n is even else { // Iterate by the sequence // and print the values for (int i = 0; i < n; i++) { // If i is odd, print -1 if (i % 2) cout << -1 << " "; // In any other case, print 1 else cout << 1 << " "; } // Print a newline character // on the finish cout << endl; } } // Driver perform int foremost() { int n = 9; // Name the resolve perform with n = 9 resolve(9); }
Output
3 -4 3 -4 3 -4 3 -4 3
Time Complexity: O(N)
Auxiliary Area: O(1)
[ad_2]