You are given an array a[] of n values as a[1], a[2]…a[n] which are part of n-equations in n-variables where equations are as:
Eqn1 => X2 + X3 +…..+Xn = a1
Eqn2 => X1 + X3 +…..+Xn = a2
Eqni => X1 + X2 +…+Xi-1 + Xi+1..+Xn = ai
Eqnn => X1 + X2 +…..+Xn-1 = an
As you have n-equations in n-variables find the value of all variables(X1, X2…Xn).
Examples :
Input : a[] = {4, 4, 4, 4, 4}
Output : X1 = 1
X2 = 1
X3 = 1
X4 = 1
X5 = 1
Input : a[] = {2, 5, 6, 4, 8}
Output : X1 = 4.25
X2 = 1.25
X3 = 0.25
X4 = 2.25
X5 = -1.75
Approach:
Let, X1+X2+X3+….Xn= SUM
Using value SUM we have our equations as
SUM - X1 = a1 -----(1) SUM - X2 = a2 -----(2) SUM - Xi = ai -----(i) SUM - Xn = an -------(n) --------------------------------------------------------------- Now, if we add all these equation we will have an equation as : n*SUM -(X1+X2+...Xn) = a1 + a2 + ...an n*SUM - SUM = a1 + a2 + ...an SUM = (a1+a2+...an)/(n-1) Calculate SUM from above equation. putting value of SUM in (i), (ii).... we have X1 = SUM - a1 X2 = SUM - a2
Solution :
X1 = SUM - a1 X2 = SUM - a2 Xi = SUM - ai Xn = SUM - an
C++
// CPP program to find n-variables #include <bits/stdc++.h> using namespace std; // function to print n-variable values void findVar(int a[], int n) { // calculate value of array SUM float SUM = 0; for (int i = 0; i < n; i++) SUM += a[i]; // Every variable contributes n-1 // times to sum. So dividing by // n-1 to get sum of all. SUM /= (n - 1); // print the values of n-variables for (int i = 0; i < n; i++) cout << "X" << (i + 1) << " = " << SUM - a[i] << endl; } // driver program int main() { int a[] = { 2, 5, 6, 4, 8 }; int n = sizeof(a) / sizeof(a[0]); findVar(a, n); return 0; } |
Java
// Java program to // find n-variables import java.io.*; class GFG { // function to print // n-variable values static void findVar(int []a, int n) { // calculate value+ // of array SUM float SUM = 0; for (int i = 0; i < n; i++) SUM += a[i]; // Every variable contributes // n-1 times to sum. So dividing // by n-1 to get sum of all. SUM /= (n - 1); // print the values // of n-variables for (int i = 0; i < n; i++) System.out.print("X" + (i + 1) + " = " + (SUM - a[i]) + "\n"); } // Driver Code public static void main(String args[]) { int []a = new int[]{2, 5, 6, 4, 8}; int n = a.length; findVar(a, n); } } // This code is contributed by // Manish Shaw(manishshaw1) |
Python3
# Python3 program to # find n-variables # function to print # n-variable values def findVar(a, n): # calculate value of # array SUM SUM = 0; for i in range(n): SUM += a[i]; # Every variable contributes # n-1 times to sum. So # dividing by n-1 to get sum # of all. SUM = SUM / (n - 1); # print the values # of n-variables for i in range(n): print("X" , (i + 1), " = ", SUM - a[i]); # Driver Code a = [2, 5, 6, 4, 8]; n = len(a); findVar(a, n); # This code is contributed # by mits |
C#
// C# program to find n-variables using System; using System.Linq; using System.Collections.Generic; class GFG { // function to print // n-variable values static void findVar(int []a, int n) { // calculate value+ // of array SUM float SUM = 0; for (int i = 0; i < n; i++) SUM += a[i]; // Every variable contributes // n-1 times to sum. So dividing // by n-1 to get sum of all. SUM /= (n - 1); // print the values // of n-variables for (int i = 0; i < n; i++) Console.Write("X" + (i + 1) + " = " + (SUM - a[i]) + "\n"); } // Driver Code static void Main() { int []a = { 2, 5, 6, 4, 8 }; int n = a.Length; findVar(a, n); } } // This code is contributed by // Manish Shaw(manishshaw1) |
PHP
<?php // PHP program to // find n-variables // function to print // n-variable values function findVar($a, $n) { // calculate value of // array SUM $SUM = 0; for ($i = 0; $i < $n; $i++) $SUM += $a[$i]; // Every variable contributes n-1 // times to sum. So dividing by // n-1 to get sum of all. $SUM /= ($n - 1); // print the values // of n-variables for ( $i = 0; $i < $n; $i++) echo "\nX" , ($i + 1) , " = " , $SUM - $a[$i]; } // Driver Code $a = array(2, 5, 6, 4, 8); $n = count($a); findVar($a, $n); // This code is contributed by anuj_67. ?> |
X1 = 4.25 X2 = 1.25 X3 = 0.25 X4 = 2.25 X5 = -1.75
Time Complexity : O(n)
Auxiliary Space : O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find the repeating and the missing number using two equations
- Find the values of X and Y in the Given Equations
- Find 'N' number of solutions with the given inequality equations
- Find if two given Quadratic equations have common roots or not
- Find n positive integers that satisfy the given equations
- Program to find root of an equations using secant method
- Find the missing value from the given equation a + b = c
- Find the Missing Number
- Find the Missing Point of Parallelogram
- Find the missing integer in an array if mean is given
- Find the missing elements from 1 to M in given N ranges | Set-2
- Find the smallest missing number
- Find missing elements from an Array
- Find the one missing number in range
- Find the repeating and the missing | Added 3 new methods
- Find first k natural numbers missing in given array
- Find temperature of missing days using given sum and average
- Find the last two missing digits of the given phone number
- Find the missing number in Geometric Progression
- Find four missing numbers in an array containing elements from 1 to N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

