Print Pattern | Practice | GeeksforGeeks

Print a sequence of numbers starting with N where A[0] = N, without using loop, in which  A[i+1] = A[i] - 5,  until A[i] > 0. After that A[i+1] = A[i] + 5  repeat it until A[i] = N.
Example 1:
Input: N = 16
Output: 16 11 6 1 -4 1 6 11 16
Explaination: The value decreses until it 
is greater than 0. After that it increases 
and stops when it becomes 16 again.
Example 2:
Input: N = 10
Output: 10 5 0 5 10
Explaination: It follows the same logic as 
per the above example.
Your Task:
You do not need to read input or print anything. Your task is to complete the function pattern() which takes N as input parameters and returns a list containing the pattern.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 104 
0
lumosmaxima000001 week ago
Can anybody please let me know, where is the problem in this code - 
 
public:
    
    vector<int> res;
    
    vector<int> check(int n, int M)
    {
        res.push_back(n);
        
        if(n == M)
            return res;
        if(n <= 0)
            n += 5;
        else if(n > 0)
            n -= 5;
            
        return check(n,M);
    }
    
    vector<int> pattern(int N){
        // code here
        res.push_back(N);
        
        N -= 5;
        
        return check(N,N+5);
    }
I am getting infinite loop error.
0
miniaditi021 week ago
why is this code not working
class Solution:   def pattern(self, N):            if N==0 or N<0:           print(N, end=",")           return       print(N, end=",")       pattern(N-5)                     print(N, end=",")
0
miniaditi02
This comment was deleted.
0
rohansarna0712011 week ago
class Solution{
public:
vector<int>v ;
    vector<int> pattern(int N){
        if(N <=0   ){
            if(v[v.size()-1] > 0 ){
            v.push_back(N); 
                
            }
            return v ; 
            
        }
        
        
        v.push_back(N); 
        pattern(N-5);
        v.push_back(N);
        return v ; 
        
    }
};
+1
badgujarsachin832 weeks ago
 vector<int> pattern(int N){
        // code here
        int n=N;
        vector<int> v;
        while(n>0){
            v.push_back(n);
            n-=5;
        }
        while(n!=N){
            v.push_back(n);
            n+=5;
        }
        v.push_back(n);
        return v;
    }
0
shuklarishabh8903 weeks ago
def pattern(self, N):       if N==0 or N<0:           print(N,end=', ')           return       print(N,end=", ")       pattern(N-5)
       print(N,end=", ")       can anyBody tell me why this code is not working.
0
subhajitghosh19973 weeks ago
// JAVA // ALL TEST CASES PASS
class Solution{   static List<Integer> pattern(int N){       // code here       List<Integer> n1 = new ArrayList<>();       List<Integer> n2 = new ArrayList<>();       n1 = generate1(N , n1);       int x = n1.size();       int p = n1.get(x-1);       if(p > 0){           n1.add(p-5);           n1 = generate2(N , p-5 , n1);       }else{            n1 = generate2(N , p , n1);       }           return n1;   }      public static List<Integer> generate1(int N , List<Integer> n1){       if(N < 0){           return n1;       }       n1.add(N);       generate1(N-5 , n1);       return n1;   }      public static List<Integer> generate2(int N , int N2 , List<Integer> n1){       if(N2 == N){           return n1;       }       n1.add(N2+5);       generate2(N,N2+5 , n1);       return n1;   }      }
0
sourabhgharad19064 weeks ago
JAVA EASIEST SOLUTION!!!
class Solution{   static List<Integer> helper(int N,ArrayList<Integer> list){       if(N<=0){           list.add(N);           return list;       }       list.add(N);       helper(N-5,list);       list.add(N);       return list;   }   static List<Integer> pattern(int N){       ArrayList<Integer> list = new ArrayList<>();       return helper(N,list);   }}
 
 
0
debasishtewary54 weeks ago
class Solution{  public static List<Integer> pat(int n,List<Integer> l){if(n<=0) { return l;}else { l.add(n); return pat(n-5,l);}}public static int pat0(int n){if(n<=0) {  return n;}else { return pat0(n-5);}}public static List<Integer> pat1(int k,int n,List<Integer> l){if(n==k){ l.add(n); return l;}else { l.add(n); return pat1(k,n+5,l);}}   static List<Integer> pattern(int N){       // code here       int n=N;       int k=n;       List<Integer> l=new ArrayList<Integer>();       Solution.pat(n,l);Solution.pat1(k,Solution.pat0(n),l);return l;   }      }
0
kashyapjhon4 weeks ago
C++ Without Recursion Code:
vector<int> pattern(int N){       // code here       vector<int> ans;       int n=N,x=N;       while(n>0){           ans.push_back(n);           n=n-5;       }       while(n!=N){           ans.push_back(n);           n=n+5;       }       ans.push_back(n);       return ans;   }
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.