Number Pattern | Practice | GeeksforGeeks

For a given number N. Print the pattern in the following form: 1 121 12321 1234321 for N = 4.
Example 1:
Input:
N = 3
Output:
1 121 12321
Explanation:
For N = 3 we will print the 3 strings 
according to the pattern.
Example 2:
Input:
N = 6
Output:
1 121 12321 1234321 123454321 12345654321
Explanation:
For N = 6 we will print the 6 strings 
according to the pattern.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function numberPattern() which takes an integer N as input parameter and returns the list of strings to be printed.
Expected Time Complexity: O(N2)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 200
0
ambadkarajinkya2164 days ago
class Solution{   ArrayList<String> numberPattern(int N){       ArrayList<String> list = new ArrayList<> ();       for(int i=1;i<=N;i++) {           String str ="";           for(int j=1;j<=i;j++) {               str+=(Integer.toString(j));           }           for(int k=i-1;k>0;k--) {               str+=(Integer.toString(k));           }           list.add(str);       }       return list;   }}
0
sushantwaybhase1232 months ago
class Solution{public:   vector<string> numberPattern(int N)   {   vector<string>v;   for(int i=1;i<=N;i++)   {   string a;       for(int j=1;j<=i;j++)       {         a+=to_string(j);                }       for(int k=i-1;k>=1;k--)       {            a+=to_string(k);                  }   v.push_back(a);   }   return v;   }};
 
0
navneetkrug3 months ago
 for(int i=1;i<=n;i++)   {       for(int j=1;j<=((2*i-1)/2 + 1);j++)       {           cout << j;           res=j;       }       for(--res ;res>=1;res--)       {           cout << res;       }       cout <<" ";   } 
0
pankajkumarravi6 months ago
 ArrayList<String> numberPattern(int N){       // code here       ArrayList<String> arrayList=new ArrayList<>();       String L="1",R="1";       arrayList.add("1");       for (int i=2;i<=N;i++){           arrayList.add(L+i+R);           L = L+i;           R =i+R;       }       return arrayList;   }
-1
jiten9 months ago
jiten
solution in c++ :-
string int_to_string(int x){        string ans;        while(x){            ans.push_back(char(x%10)+'0');            x/=10;        }        reverse(ans.begin(), ans.end());        return ans;    }
    vector<string> numberPattern(int N)    {        vector<string> res;        for(int i = 1 ; i <= N ; i++){
            string temp;
            for(int j = 1 ; j <= i ; j++){                temp+=int_to_string(j);            }
            for(int j = i-1 ; j >= 1 ; j--){                temp+=int_to_string(j);            }
            res.push_back(temp);        }        return res;    }string int_to_string(int x){        string ans;        while(x){            ans.push_back(char(x%10)+'0');            x/=10;        }        reverse(ans.begin(), ans.end());        return ans;    }
    vector<string> numberPattern(int N)    {        vector<string> res;        for(int i = 1 ; i <= N ; i++){
            string temp;
            for(int j = 1 ; j <= i ; j++){                temp+=int_to_string(j);            }
            for(int j = i-1 ; j >= 1 ; j--){                temp+=int_to_string(j);            }
            res.push_back(temp);        }        return res;    }
0
Yash Kadulkar1 year ago
Yash Kadulkar
Can someone suggest improvisation on this?
def numberPattern(self, N):# code here    disp = []for i in range(1,N+1):    new = []    for j in range(1,i+1):        new.append(str(j))    disp.append(''.join(new + new[-2::-1]))    return disp
0
Shreyansh Kumar Singh1 year ago
Shreyansh Kumar Singh
https://uploads.disquscdn.c...
0
Sagar Pawar1 year ago
Sagar Pawar
class Solution{    ArrayList<string> numberPattern(int N){        // code here        ArrayList<string> arr = new ArrayList<string>();        int digi=1,k,l=0;        String tempS;        for(int i=1; i<=N; i++){            k=i;            tempS="";            for(int j=1; j<=digi; j++){                if(j>=i) tempS+=String.valueOf(k--);                else tempS+=String.valueOf(j);            }            arr.add(l,tempS);            l++;            digi+=2;        }        return arr;    }}
0
Ankur Parihar1 year ago
Ankur Parihar
wtf if wrong! why don't you guys atleast run against correct answer to check whether you're posting it correctly!My solution is working everywhere except here! Yet you expect us to buy your courses!
0
Tulsi Dey2 years ago
Tulsi Dey
Solution in java:Execution time: 0.47 https://uploads.disquscdn.c...
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.