Transform the array  | Practice | GeeksforGeeks

Given an array arr[] of size N containing integers, zero is considered an invalid number, and rest all other numbers are valid. If two nearest valid numbers are equal then double the value of the first one and make the second number as 0. At last move all the valid numbers on the left.
Example 1:
Input: N = 12
arr[] = {2, 4, 5, 0, 0, 5, 4, 8, 6, 0, 
                                 6, 8}
Output:  2 4 10 4 8 12 8 0 0 0 0 0
Explanation: After performing above 
given operation we get array as,
2 4 10 0 0 0 4 8 12 0 0 8, then shifting
all zero's to the right, we get resultant
array as - 2 4 10 4 8 12 8 0 0 0 0 0 

Example 2:
Input: N = 2, arr[] = {0, 0}
Output: 0 0
Explanation: All elements in the array 
are invalid .
 
Your Task:
You don't need to read input or print anything. Complete the function valid() that takes array arr[] and integer N as input parameters and returns the resultant array.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 ≤ N ≤ 105
 
0
chessnoobdj4 months ago
C++
vector<int> valid(int arr[],int n)
	{
	    int i = 0;
	    while(i < n-1){
	        if(arr[i] && arr[i] == arr[i+1]){
	            arr[i]  *= 2;
	            arr[i+1] = 0;
	            int j = i+1;
	            while(j<n && !arr[j])
	                j += 1;
	            if(j<n)
	                swap(arr[i+1], arr[j]); 
	        }
	        else if(arr[i] && !arr[i+1]){
	            int j = i+1;
	            while(j<n && !arr[j])
	                j += 1;
	            if(arr[i] == arr[j]){
	                arr[i]  *= 2;
	                arr[j] = 0;
	            }
	            while(j<n && !arr[j])
	                j += 1;
	            if(j<n)
	                swap(arr[i+1], arr[j]);
	        }
	        i += 1;
	        if(i>0 && arr[i] && arr[i] == arr[i-1])
	            i -= 1;
	    }
	    int idx = 0;
	    vector <int> res(n, 0);
	    for(int i=0; i<n; i++){
	        if(arr[i])
	            res[idx++] = arr[i];
	    }
	    return res;
	}
vector<int> valid(int arr[],int n)
	{
	   int i,j=0,prev=0,c=0;
	   for(i=0;i<n;i++){
	       if(arr[i]>0){
	           if(arr[i]==prev) {
	               arr[i] *= 2;
	               prev=arr[i];
	               swap(arr[i], arr[j]);
	               arr[i]=0;
	               c++;
	            }
	            else{
	               prev=arr[i];
	               j=i;
	            }
	       }
	    }
	    int idx = 0;
	    vector <int> res(n, 0);
	    for(int i=0; i<n; i++){
	        if(arr[i])
	            res[idx++] = arr[i];
	    }
	    return res;
	}
0
poojayadav1004184 months ago
void move(int a[],int s){   int i=0;   for(int j=0;j<s;j++){       if(a[j]!=0){           swap(a[i],a[j]);           i++;       }   }}vector<int> valid(int arr[],int n){        move(arr,n);    for(int i=0;i<n;i++){        if(arr[i]==arr[i+1] && arr[i+1]!=0){            arr[i+1]=0;            arr[i]=2*arr[i];        }    }    move(arr,n);    return arr;}
what is the problem in this?
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.