Thread: Arrays interview Question

Forum : Java SE FAQ?s and Interview Questions Java SE   07-3-2018 11:32:24 AM
User Image
Shivaksk

Newbie

Joined: Jun 25, 2017

Points: 100

Threads: 2

Posts: 3

Arrays interview Question

| Quote Date : Mar 7, 2018    Views:482    

Write a program for below scenario
i have an array with elements i.e
int[] i = {1,2,3,4}
print the output like
at the first position(1) should be replace with sum of remaining elements with in the array
at second position(2) also should be replace with sum of remaining elements with in the array. and so on

Output should be
{9,8,7,6}.

Comments

User Image
TEJESH

Newbie

Joined : Oct 14, 2018

Points : 100

Threads: 0

Posts: 1

Re: Arrays interview Question

Reply Date : Nov 7, 2018

import java.util.Scanner;
public class ArrayAdd{
public static void main(String[] args){
int i=0,j=0;
int sum=0;
int inputArr[];
int outputArr[];
int n=0;
Scanner scan=null;

scan=new Scanner(System.in);
//taking no of input
System.out.println("enter no of input");
n=scan.nextInt();
//assigning array size
inputArr=new int[n];
outputArr=new int[n];
//taking input
System.out.println("enter numbers");
for(i=0;i<n;i++)
inputArr[i]=scan.nextInt();
//computing result
for(i=0;i<n;i++){
for(j=0;j<n;j++)
{
if(i!=j)
sum+=inputArr[j];
}
outputArr[i]=sum;
sum=0;
  }
  //displaying result
  System.out.println("output");
  for(i=0;i<n;i++)
  System.out.println(outputArr[i]);
}//main
}//class

User Image
Hemanth18

Newbie

Joined : Sep 7, 2018

Points : 100

Threads: 3

Posts: 8

Re: Arrays interview Question

Reply Date : Nov 5, 2018


class Sumreplace
{
    public static void main(String[] args)
    {
        //System.out.println("Hello World!");
        int m=0;
        int j=0;
        int[] v={1,2,3,4};
       
        int[] b=new int[4];
        for(m=0;m<v.length;m++)
        {
            //System.out.println(v[m]);
        }
           
           
           
        b[0]=v[1]+v[2]+v[3];
        //System.out.println(b[0]);//test index of new added array
        b[1]=v[0]+v[2]+v[3];
        b[2]=v[0]+v[1]+v[3];
        b[3]=v[0]+v[1]+v[2];

        for(j=0;j<b.length;j++)
        {
        System.out.println(b[j]);
        }
    }
}


output: 9,8 7 6




User Image
Baadshah

Newbie

Joined : Oct 3, 2017

Points : 100

Threads: 0

Posts: 1

Re: Arrays interview Question

Reply Date : Mar 8, 2018

package com.ds.sjg;
    public class MyTest {
        public static void main(String[] args) {
        int arrA[] = {1,2,3,4};
        int sumOfAll = 0;
        for( int i = 0; i < arrA.length; i++)
            sumOfAll + = arrA[i];
        for(int i = 0; i < arrA.length; i++) {
            arrA[i] = sumOfAll - arrA[i];
            System.out.print(arrA[i] + " ");
        }
    }
}

User Image
Aruna986

Newbie

Joined : Jun 25, 2017

Points : 100

Threads: 1

Posts: 14

Re: Arrays interview Question

Reply Date : Mar 8, 2018

  
public class SumReplacement {
public static void main(String args[]) {
int[] a = { 1, 2, 3, 4 };
int[] b = new int[4];
int temp = 0;
int counter = 0;
int k = 0;
for (int j = 0; j < a.length; j++) {
for (int i = 0; i < a.length; i++) {
int c = a[i];
temp = temp + c;
}
temp = temp - a[j];
b[k] = temp;
temp = 0;
k++;
}
for (int s = 0; s < b.length; s++) {
System.out.print(b[s]+" ");
}
}?

Load More

No More Comments