Skip to main content

Featured

Solve LeetCode 3 Longest Substring Without Repeating Characters in C++ using the sliding window

Longest Substring Without Repeating Characters in C++ LeetCode 3 · sliding window · last index array Table of Contents Problem Statement Approach Algorithm C++ Program Input Sample Output Output Explanation Dry Run Flow of Execution Time Complexity Space Complexity Applications Key Points Interview Questions Frequently Asked Questions Conclusion Problem Statement 3. Longest Substring Without Repeating Characters Given a string s , find the length of the longest substring that contains no repeating characters. A substring is a continuous part of the string, so "pwke" is a subsequence of "pwwkew" but not a substring. Approach We use a sliding window . The window is the part of the string between start and end , and we always keep it free of repeated characters. An array lastIndex of size 128 remembers the most recent position of every ASCII ...

C++ Triangle Pattern Programs 5: Star, Number, Alphabet & Binary Patterns with Output

C++ Program to Print Consecutive Number Pyramid Pattern

A counter starts at 0 and keeps increasing across rows, so the numbers never repeat.

Source Code
pattern.cpp
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int num,a=0;
  6. cout<<"Enter the number:"<<endl;
  7. cin>>num;
  8. for(int i=1;i<=num;i++)
  9. {
  10. // leading spaces
  11. for(int k=num;k>=i;k--)
  12. {
  13. cout<<" ";
  14. }
  15. // print i numbers, counter continues
  16. for(int j=1;j<=i;j++)
  17. {
  18. cout<<a++<<" ";
  19. }
  20. cout<<endl;
  21. }
  22. return 0;
  23. }
Output (Input: 5)
Enter the number:
5
     0
    1 2
   3 4 5
  6 7 8 9
 10 11 12 13 14
0
12
345
6789
1011121314
How It Works

The variable a starts at 0 and is declared outside both loops, so it is never reset. The expression a++ prints the current value and then adds 1, which is why the numbers keep counting from one row to the next.

The outer loop i controls the row number. The k loop prints num - i + 1 spaces, and the j loop runs i times, so row i holds i numbers.

Once the numbers reach two digits (10 and above), each entry is wider than one character, so the rows no longer line up neatly. For a tidy shape with larger inputs, print with setw() from <iomanip>.

Dry Run (num = 5)
ispacesa at startnumbers printeda at end
15001
2411 23
3333 4 56
4266 7 8 910
511010 11 12 13 1415
Algorithm
  1. Read num and set a = 0.
  2. Repeat for i from 1 to num.
  3. Print a space for k from num down to i.
  4. For j from 1 to i, print a and a space, then increase a by 1.
  5. Move to the next line.
Time and Space Complexity
O(n²)Each row prints num + 1 items, so the total is num × (num + 1).
O(1)Only the integers num, a, i, j and k are used.
Search Description, Keywords and Hashtags

Description: C++ program to print a consecutive number pyramid pattern starting from 0, using nested for loops and a counter variable.

Keywords: C++ consecutive number pyramid, continuous number pattern in C++, C++ pyramid pattern program, counter variable in C++, C++ nested loops, C++ for loop pattern, pattern printing in C++, beginner C++ programs

Hashtags: #CppProgramming #Cpp #PatternPrograms #NumberPattern #ConsecutiveNumbers #PyramidPattern #NestedLoops #ForLoop #Coding #LearnCpp #1printf

C++ Program to Print Star Pyramid Pattern

One star in row 1, two in row 2, and one more star in every row after that.

Source Code
pattern.cpp
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int num;
  6. cout<<"Enter the number:"<<endl;
  7. cin>>num;
  8. for(int i=1;i<=num;i++)
  9. {
  10. // leading spaces
  11. for(int k=num;k>=i;k--)
  12. {
  13. cout<<" ";
  14. }
  15. // print i stars
  16. for(int j=1;j<=i;j++)
  17. {
  18. cout<<"* ";
  19. }
  20. cout<<endl;
  21. }
  22. return 0;
  23. }
Output (Input: 5)
Enter the number:
5
     *
    * *
   * * *
  * * * *
 * * * * *
*
**
***
****
*****
How It Works

The outer loop i controls the row number. The k loop prints num - i + 1 spaces, so the rows shift left as they get longer.

The j loop runs i times and prints "* " each time, so row 1 has one star, row 2 has two, and so on. Then endl starts a new line.

Because a space is 1 character wide and each star with its trailing space is 2 characters, the shape leans slightly instead of forming a perfectly symmetric pyramid. To make it symmetric, print two spaces per indent step.

Dry Run (num = 5)
ispacesstarsrow printed
151*
242* *
333* * *
424* * * *
515* * * * *
Algorithm
  1. Read num from the user.
  2. Repeat for i from 1 to num.
  3. Print a space for k from num down to i.
  4. Print "* " for j from 1 to i.
  5. Move to the next line.
Time and Space Complexity
O(n²)Each row prints num + 1 items, so the total is num × (num + 1).
O(1)Only the integers num, i, j and k are used.
Search Description, Keywords and Hashtags

Description: C++ program to print a star pyramid pattern using nested for loops, with one loop for leading spaces and one loop for stars.

Keywords: C++ star pyramid, star pyramid pattern in C++, C++ pyramid pattern program, star pattern in C++, C++ nested loops, C++ for loop pattern, pattern printing in C++, beginner C++ programs

Hashtags: #CppProgramming #Cpp #PatternPrograms #StarPattern #StarPyramid #PyramidPattern #NestedLoops #ForLoop #Coding #LearnCpp #1printf

C++ Program to Print Alternating Row 1 0 Pyramid Pattern

Each row repeats a single value, and the value flips between 1 and 0 from row to row.

Source Code
pattern.cpp
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int num;
  6. cout<<"Enter the number:"<<endl;
  7. cin>>num;
  8. for(int i=1;i<=num;i++)
  9. {
  10. // leading spaces
  11. for(int k=num;k>=i;k--)
  12. {
  13. cout<<" ";
  14. }
  15. // print i % 2, i times
  16. for(int j=1;j<=i;j++)
  17. {
  18. cout<<i%2<<" ";
  19. }
  20. cout<<endl;
  21. }
  22. return 0;
  23. }
Output (Input: 5)
Enter the number:
5
     1
    0 0
   1 1 1
  0 0 0 0
 1 1 1 1 1
1
00
111
0000
11111
How It Works

The outer loop i controls the row number. The k loop prints num - i + 1 spaces, so the rows shift left as they get longer.

The j loop runs i times but prints i % 2, not j % 2. The value of i stays the same during a row, so the whole row has one value: 1 when i is odd and 0 when i is even.

Because a space is 1 character wide and each value with its trailing space is 2 characters, the shape leans slightly instead of forming a perfectly symmetric pyramid.

Dry Run (num = 5)
ispacesi % 2timesrow printed
15111
24020 0
33131 1 1
42040 0 0 0
51151 1 1 1 1
Algorithm
  1. Read num from the user.
  2. Repeat for i from 1 to num.
  3. Print a space for k from num down to i.
  4. Print i % 2 and a space, i times.
  5. Move to the next line.
Time and Space Complexity
O(n²)Each row prints num + 1 items, so the total is num × (num + 1).
O(1)Only the integers num, i, j and k are used.
Search Description, Keywords and Hashtags

Description: C++ program to print an alternating row 1 0 pyramid pattern using nested for loops and the modulus operator on the row number.

Keywords: C++ 1 0 pyramid pattern, C++ binary pyramid, alternating row 1 0 pattern in C++, C++ modulus operator pattern, C++ nested loops, C++ for loop pattern, pattern printing in C++, beginner C++ programs

Hashtags: #CppProgramming #Cpp #PatternPrograms #BinaryPattern #01Pattern #PyramidPattern #NestedLoops #ModulusOperator #Coding #LearnCpp #1printf

C++ Program to Print Alternating 1 0 Pyramid Pattern

Every row starts with 1 and alternates 1 0 1 0, growing by one value per row.

Source Code
pattern.cpp
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int num;
  6. cout<<"Enter the number:"<<endl;
  7. cin>>num;
  8. for(int i=1;i<=num;i++)
  9. {
  10. // leading spaces
  11. for(int k=num;k>=i;k--)
  12. {
  13. cout<<" ";
  14. }
  15. // print 1 or 0 using j % 2
  16. for(int j=1;j<=i;j++)
  17. {
  18. cout<<j%2<<" ";
  19. }
  20. cout<<endl;
  21. }
  22. return 0;
  23. }
Output (Input: 5)
Enter the number:
5
     1
    1 0
   1 0 1
  1 0 1 0
 1 0 1 0 1
1
10
101
1010
10101
How It Works

The outer loop i controls the row number. The k loop prints num - i + 1 spaces, so the rows shift left as they get longer.

The j loop runs from 1 to i and prints j % 2. When j is odd, the remainder is 1; when j is even, it is 0. Since j restarts at 1 in every row, each row begins with 1 and alternates.

Because a space is 1 character wide and each value with its trailing space is 2 characters, the shape leans slightly instead of forming a perfectly symmetric pyramid.

Dry Run (num = 5)
ispacesj valuesj % 2 printed
1511
241 21 0
331 2 31 0 1
421 2 3 41 0 1 0
511 2 3 4 51 0 1 0 1
Algorithm
  1. Read num from the user.
  2. Repeat for i from 1 to num.
  3. Print a space for k from num down to i.
  4. For j from 1 to i, print j % 2 and a space.
  5. Move to the next line.
Time and Space Complexity
O(n²)Each row prints num + 1 items, so the total is num × (num + 1).
O(1)Only the integers num, i, j and k are used.
Search Description, Keywords and Hashtags

Description: C++ program to print an alternating 1 0 pyramid pattern using nested for loops and the modulus operator.

Keywords: C++ 1 0 pyramid pattern, C++ binary pyramid, alternating 1 0 pattern in C++, C++ modulus operator pattern, C++ nested loops, C++ for loop pattern, pattern printing in C++, beginner C++ programs

Hashtags: #CppProgramming #Cpp #PatternPrograms #BinaryPattern #01Pattern #PyramidPattern #NestedLoops #ModulusOperator #Coding #LearnCpp #1printf

C++ Program to Print Repeated Number Pyramid Pattern

Row 1 prints 1 once, row 2 prints 2 twice, row 3 prints 3 three times, and so on.

Source Code
pattern.cpp
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int num;
  6. cout<<"Enter the number:"<<endl;
  7. cin>>num;
  8. for(int i=1;i<=num;i++)
  9. {
  10. // leading spaces
  11. for(int k=num;k>=i;k--)
  12. {
  13. cout<<" ";
  14. }
  15. // print i, i times
  16. for(int j=1;j<=i;j++)
  17. {
  18. cout<<i<<" ";
  19. }
  20. cout<<endl;
  21. }
  22. return 0;
  23. }
Output (Input: 5)
Enter the number:
5
     1
    2 2
   3 3 3
  4 4 4 4
 5 5 5 5 5
1
22
333
4444
55555
How It Works

The outer loop i controls the row number. The k loop prints num - i + 1 spaces, so the rows shift left as they get longer.

The j loop only decides how many times to print. It runs i times, but it prints the value of i (not j), so every number in a row is the same. That is the only difference from the 1 2 3 pyramid.

Because a space is 1 character wide and each number with its trailing space is 2 characters, the shape leans slightly instead of forming a perfectly symmetric pyramid.

Dry Run (num = 5)
ispacesvaluetimes
1511
2422
3333
4244
5155
Algorithm
  1. Read num from the user.
  2. Repeat for i from 1 to num.
  3. Print a space for k from num down to i.
  4. Print the value of i and a space, i times.
  5. Move to the next line.
Time and Space Complexity
O(n²)Each row prints num + 1 items, so the total is num × (num + 1).
O(1)Only the integers num, i, j and k are used.
Search Description, Keywords and Hashtags

Description: C++ program to print a repeated number pyramid pattern using nested for loops, where each row prints its own row number.

Keywords: C++ repeated number pyramid, number pyramid pattern in C++, C++ pyramid pattern program, same number in each row, C++ nested loops, C++ for loop pattern, pattern printing in C++, beginner C++ programs

Hashtags: #CppProgramming #Cpp #PatternPrograms #NumberPattern #PyramidPattern #NestedLoops #ForLoop #Coding #LearnCpp #1printf

C++ Program to Print Increasing Number Pyramid Pattern

Nested for loops · one loop for spaces, one loop for numbers

Source Code

pyramid.cpp
#include<iostream>
using namespace std;

int main()
{
    int num;

    cout<<"Enter the number:"<<endl;
    cin>>num;

    for(int i=1;i<=num;i++)
    {
        // leading spaces
        for(int k=num;k>=i;k--)
        {
            cout<<" ";
        }

        // numbers 1 to i
        for(int j=1;j<=i;j++)
        {
            cout<<j<<" ";
        }

        cout<<endl;
    }

    return 0;
}

Output

terminal
Enter the number:
5
     1
    1 2
   1 2 3
  1 2 3 4
 1 2 3 4 5

How It Works

The outer loop i controls the row. For every row, the k loop prints num - i + 1 spaces, and the j loop prints the numbers 1 to i. Then endl starts a new line.

Because each space is one character but each number takes two (digit plus space), the pyramid leans slightly: the left edge moves one step per row and the right edge moves two.

Dry Run (num = 5)

ispacesnumbers printed
151
241 2
331 2 3
421 2 3 4
511 2 3 4 5

Algorithm

  1. Read num from the user.
  2. Repeat for i from 1 to num:
  3. Print a space for k from num down to i.
  4. Print j and a space for j from 1 to i.
  5. Move to the next line.

Complexity

O(n²)Time: each row prints num + 1 items, so the total is num × (num + 1).
O(1)Space: only the integers num, i, j and k are used.

Search Description

C++ program to print an increasing number pyramid pattern using nested for loops. Learn how a space loop and a number loop work together to build pyramid patterns.

Keywords

C++ number pyramid, increasing number pyramid pattern, C++ pyramid pattern program, number pattern in C++, C++ nested loops, C++ for loop pattern, pattern printing in C++, beginner C++ programs

Hashtags

#CppProgramming #Cpp #PatternPrograms #NumberPattern #PyramidPattern #NestedLoops #ForLoop #Coding #LearnCpp #1printf

Comments

Popular Posts

🌙