C Programming – HackerRank Solution | Sum except that element |

Write a Modular C Programming code to solve Sum except for that element, Find the sum of all elements of an array except that element at index i

Find the sum of all elements of an array except that element at index i, and store the answer in a new array.

Example:

arr[]={10,20,30,40,50};

output_array={140,130,120,110,100}

Explanation: Sum of all elements except at index 0 is 140 (20+30+40+50), therefore store 140 at index 0.

Sum of all elements except at index 1 is 130 (10+30+40+50), therefore store 130 at index 1.

Sum of all elements except at index 2 is 120 (10+20+40+50), therefore store 140 at index 2.

Sum of all elements except at index 3 is 110 (10+20+30+50), therefore store 140 at index 3.

Sum of all elements except at index 4 is 100 (10+20+30+40), therefore store 140 at index 4.

Input Format

First-line contains a number of test cases. the second line contains N, the number of array elements the Third line contains N array elements separated by space

Constraints

1<=N<=10^5

Output Format

Output the new array for each test case in a new line.

Sample Input 0

2
4
1 2 3 4
5
10 20 30 40 50

Sample Output 0

9 8 7 6
140 130 120 110 100

Explanation 0

The first line in input is 2, represents number of test cases. The first array is 1 2 3 4 Sum of array element except at index 0 is 9 Sum of array element except at index 1 is 8 Sum of array element except at index 2 is 7 Sum of array element except at index 3 is 6

Refer : C Programming HackerRank all solutions for Loops | Arrays | strings

 

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

void  read(int a[30],int k)
{
     int t,i,n;
    for(t=0;t<k;t++) { scanf("%d",&n); if(n>0&&n<100000)
        {
     for(i=0;i<n;i++)
     {
         scanf("%d",&a[i]);
     }
       sum(a,n);
        printf("\n");
        }
        else 
            printf("Invalid\n");
    }
     
}
void sum(int a[30],int n)
{
 int i,t=0,j,sum=0;
            for(i=0;i<n;i++)
            {
                for(j=0;j<n;j++)
                {
                    if(i!=j)
                    {
                        sum=sum+a[j];
                    }
                }
                printf("%d ",sum);
                sum=0;
            }
}
int main() {
int a[30],k;
    scanf("%d",&k);
    read(a,k);
}

OUTPUT

Congratulations, you passed the sample test case.

Click the Submit Code button to run your code against all the test cases.

Input (stdin)

2
4
1 2 3 4
5
10 20 30 40 50
Your Output (stdout)

9 8 7 6 
140 130 120 110 100 
Expected Output

9 8 7 6
140 130 120 110 100

 

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University







Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!