Pergunta de entrevista da empresa NVIDIA

3 Program to rearrange the array so that 'n' element in an array should be shifted to the end without impacting the sequence of non 'n' number

Resposta da entrevista

Sigiloso

13 de mai. de 2021

input arr[] = {4,0,2,0,0,6,1,0} output = {4,2,6,1,0,0,0,0} int[] array = { 4,0,2,0,0,6,1,0 }; int nonZeroIndex = 0; //the number of non-zero elements we've seen so far for (int i = 0; i < array.Length; i++) { //skip all zeroes if (array[i] == 0) continue; //put this element to the index we're tracking array[nonZeroIndex] = array[i]; nonZeroIndex++; } //fill the end of the array with zeroes. for (int i = nonZeroIndex; i < array.Length; i++) array[i] = 0; //Console.WriteLine(array); Console.ReadKey();

2