TCS Off Campus coding question solved

Given list of integers, compute the running total based on the following conditions:

  1. Add even-numbered indexes to the total
  2. substract odd-numbered indexes from the total
  3. Index 0 is considered as even index.

Question:

This question was asked in one of the off campus coding rounds.the permitted languages are java, python,java script etc. The question is based on loops, arrays and if else conditions.

Given list of integers, compute the running total based on the following conditions:

Add even-numbered indexes to the total
substract odd-numbered indexes from the total
Index 0 is considered as even index.

tcs off campus coding question

Algorithm to solve

  • Input the array from user
  • initialize a variable say total to store the sum of the elements of array
  • use for loop for iterating through elements of the array
  • Inside for loop write one condition if index values is equal to zero OR if index value Divide by 2 is zero (index value %2==0), then add the element at that index with total.
  • Else substract the value of that index from total
  • That’s it you just solved one coding question

JAVA CODE

package arrayPrograms;
import java.util.*;

class Demo {

	public static void main(String[] args) {
		
		
		///....... CODED BY VRASHIKESH PATIL......///
		
		
		int[] array1= {100,100};
		int total=0;
		
		for(int i=0;i<array1.length;i++) {
			
			if(i==0 || i%2==0) {
				
				total=total+array1[i];
			}
			else {
				total=total-array1[i];
				
			}
		}
		System.out.println("The final output is :" + total);
		
		
	}

}

Watch complete explanation for above code in our channel

Similar off campus coding questions

Reducing Dishes coding Complete solution

Count number of palindrome words in given sentance

Saddle points in 2D array off campus problem solved

Hacker Earth Coding Question with Solution

3 thoughts on “TCS Off Campus coding question solved”

  1. Pingback: Java code to print duplicate characters from string - Is It Actually

  2. Pingback: Important Difference between array list and linked list - Is It Actually

  3. Pingback: Important differences between Fixed capacitor & variable capacitor - Is It Actually

Leave a Comment

Your email address will not be published. Required fields are marked *