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 3: Star, Number, Alphabet & Binary Patterns with Output

C++ Program to Print Right-Aligned Star Triangle Pattern

Source Code

#include<iostream>
using namespace std;

int main()
{
    int num;

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

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

        for(int j=1;j<=i;j++)
        {
            cout<<"* ";
        }

        cout<<endl;
    }
}

Expected Output

Enter the number:
5

          *
        * *
      * * *
    * * * *
  * * * * *

Explanation

This C++ program prints a right-aligned star triangle pattern using nested loops. The outer loop controls the number of rows, while the first inner loop prints spaces before the stars.

The first inner loop runs from num down to i and prints two spaces in each iteration. This creates the alignment of the triangle. The second inner loop prints * characters, and the number of stars increases by one in every row.

Algorithm

  1. Start the program.
  2. Read the number of rows from the user.
  3. Run an outer loop from i = 1 to num.
  4. Use the first inner loop to print spaces before the stars.
  5. Use the second inner loop to print stars from 1 to i.
  6. Move to the next line after printing each row.
  7. Stop the program.

Time Complexity

O(n²) — The program uses nested loops to print spaces and stars for each row.

Space Complexity

O(1) — The program uses only a fixed number of variables and does not require additional memory proportional to the input size.

Search Description

Learn how to write a C++ program to print a right-aligned star triangle pattern using nested loops. This beginner-friendly pattern program explains how spaces and stars are used to create a triangular shape.

Keywords

C++ star pattern program, right aligned star triangle, star triangle pattern in C++, C++ pattern programs, nested loops in C++, pyramid star pattern, right triangle pattern, star pattern using loops

Hashtags

#Cpp #CPlusPlus #CppProgramming #PatternPrograms #StarPattern #TrianglePattern #NestedLoops #ProgrammingForBeginners #Coding #DSA #1printf

C++ Program to Print Repeated Character Triangle Pattern

Source Code

#include<iostream>
using namespace std;

int main()
{
    int num;

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

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

        for(int j=1;j<=i;j++)
        {
            cout<<char(i+64)<<" ";
        }

        cout<<endl;
    }
}

Expected Output

Enter the number:
5

        A
      B B
    C C C
  D D D D
E E E E E

Explanation

This C++ program prints a repeated character triangle pattern using nested loops. The outer loop controls the rows, while the first inner loop prints spaces to create the right-aligned triangle shape.

The second inner loop prints the same character repeatedly in each row. The expression char(i + 64) converts the row number into its corresponding uppercase alphabet character. For example, 1 + 64 = 65, which represents A in ASCII, while 2 + 64 = 66, which represents B.

Algorithm

  1. Start the program.
  2. Read the number of rows from the user.
  3. Run an outer loop from i = 1 to num.
  4. Use a loop to print spaces before the characters.
  5. Use another loop from j = 1 to i.
  6. Convert i + 64 into an uppercase character using char().
  7. Print the same character repeatedly in the current row.
  8. Move to the next line after printing each row.
  9. Stop the program.

Time Complexity

O(n²) — The nested loops perform approximately n² operations to generate the pattern.

Space Complexity

O(1) — The program uses only a fixed number of variables and does not require additional memory based on the input size.

Search Description

Learn how to write a C++ program to print a repeated character triangle pattern using nested loops and ASCII values. This beginner-friendly example explains how to print the same uppercase alphabet character repeatedly in each row.

Keywords

C++ repeated character pattern, repeated alphabet triangle pattern, character triangle pattern in C++, C++ alphabet pattern, ASCII character pattern, uppercase character pattern, nested loops in C++, C++ pattern programs

Hashtags

#Cpp #CPlusPlus #CppProgramming #PatternPrograms #CharacterPattern #AlphabetPattern #ASCIIPattern #NestedLoops #ProgrammingForBeginners #Coding #DSA #1printf

C++ Program to Print Character Triangle Pattern

Source Code

#include<iostream>
using namespace std;

int main()
{
    int num;

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

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

        for(int j=1;j<=i;j++)
        {
            cout<<char(j+64)<<" ";
        }

        cout<<endl;
    }
}

Expected Output

Enter the number:
5

        A
      A B
    A B C
  A B C D
A B C D E

Explanation

This C++ program prints an uppercase character triangle pattern using nested loops. The outer loop controls the number of rows, while the first inner loop prints spaces to create the right-aligned triangle shape.

The second inner loop prints characters using char(j + 64). In ASCII, the value 65 represents A, 66 represents B, 67 represents C, and so on. Since j starts from 1, adding 64 converts it into the corresponding uppercase alphabet character.

Algorithm

  1. Start the program.
  2. Read the number of rows from the user.
  3. Run an outer loop from i = 1 to num.
  4. Use a loop to print spaces before the characters.
  5. Use another loop from j = 1 to i.
  6. Convert j + 64 into an uppercase character using char().
  7. Print the character followed by a space.
  8. Move to the next line after each row.
  9. Stop the program.

Time Complexity

O(n²) — The nested loops perform a total of approximately n² operations to generate the pattern.

Space Complexity

O(1) — The program uses only a fixed number of variables and does not require additional memory based on the input size.

Search Description

Learn how to write a C++ program to print a character triangle pattern using nested loops and ASCII values. This beginner-friendly example explains how to convert numeric values into uppercase alphabet characters using char(j + 64).

Keywords

C++ character pattern program, character triangle pattern in C++, alphabet triangle pattern, C++ alphabet pattern, ASCII value pattern, nested loops in C++, uppercase character pattern, C++ pattern programs

Hashtags

#Cpp #CPlusPlus #CppProgramming #PatternPrograms #CharacterPattern #AlphabetPattern #ASCIIPattern #NestedLoops #ProgrammingForBeginners #Coding #DSA #1printf

C++ Program to Print Repeated Binary Number Triangle Pattern

Source Code

#include<iostream>
using namespace std;

int main()
{
    int num;

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

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

        for(int j=1;j<=i;j++)
        {
            cout<<i%2<<" ";
        }

        cout<<endl;
    }
}

Expected Output

Enter the number:
5

        1
      0 0
    1 1 1
  0 0 0 0
1 1 1 1 1

Explanation

This C++ program prints a repeated binary number triangle pattern. The program first reads the number of rows from the user. The outer loop controls the rows, while the first inner loop adds spaces to create the right-aligned triangle shape.

The second inner loop prints the value of i % 2. When i is odd, i % 2 produces 1. When i is even, it produces 0. Therefore, every row contains the same binary value.

Algorithm

  1. Start the program.
  2. Read the number of rows from the user.
  3. Run an outer loop from i = 1 to num.
  4. Use a loop to print spaces before the numbers.
  5. Use another loop to print i % 2 repeatedly in each row.
  6. Move to the next line after printing each row.
  7. Stop the program.

Time Complexity

O(n²) — The program uses nested loops to print the spaces and binary values for each row.

Space Complexity

O(1) — The program uses only a few integer variables and does not require additional memory proportional to the input size.

Search Description

Learn how to write a C++ program to print a repeated binary number triangle pattern using nested loops and the modulus operator. This beginner-friendly example explains how to generate 1 and 0 patterns based on row numbers.

Keywords

C++ binary pattern program, repeated binary triangle pattern, binary number triangle in C++, C++ pattern programs, nested loops in C++, i%2 pattern, 1 0 pattern in C++, number pattern programs

Hashtags

#Cpp #CPlusPlus #CppProgramming #PatternPrograms #BinaryPattern #NumberPattern #NestedLoops #ProgrammingForBeginners #Coding #DSA #1printf

⭐ C++ Program to Print Binary Triangle Pattern

πŸ“„ Source Code:
#include<iostream>
using namespace std;

int main()
{
    int num;

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

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

        for(int j=1; j<=i; j++)
        {
            cout << j % 2 << " ";
        }

        cout << endl;
    }

    return 0;
}
  
πŸ’» Expected Output (Input: 5):
Enter the number:
5

        1
      1 0
    1 0 1
  1 0 1 0
1 0 1 0 1
  

πŸ“˜ Explanation

This C++ program prints a binary triangle pattern using nested for loops. The outer loop controls the number of rows, starting from 1 and increasing up to the entered number. The first inner loop prints leading spaces to properly align the pattern, while the second inner loop prints alternating 1 and 0 values using the expression j % 2. Since the remainder alternates between 1 and 0 for consecutive values of j, the program creates a binary triangle pattern.

🧠 Algorithm

  1. Read the number from the user.
  2. Start the outer loop from 1 up to the entered number.
  3. Print leading spaces for proper alignment.
  4. Print alternating 1 and 0 values using j % 2.
  5. Move to the next line after each row.
  6. Repeat until the complete binary triangle is displayed.

⏱ Time Complexity

O(n²)

πŸ’Ύ Space Complexity

O(1)

πŸ“ Search Description

Learn how to print a binary triangle pattern in C++ using nested loops. This beginner-friendly C++ program uses the modulo operator to generate alternating 1 and 0 values, making it useful for learning binary patterns, nested loops, and pattern printing in C++.

πŸ” Keywords

binary triangle pattern in C++, C++ program for binary pattern, 0 1 pattern in C++, binary pattern using nested loops, j % 2 pattern in C++, triangle pattern in C++, C++ pattern program, pattern printing in C++, nested loops in C++, C++ programming examples.

🏷 Hashtags

#CppProgramming #PatternPrograms #BinaryPattern #NumberPattern #TrianglePattern #Coding #Programming #NestedLoops #Cpp #1printf

⭐ C++ Program to Print Repeated Number Triangle Pattern

πŸ“„ Source Code:
#include<iostream>
using namespace std;

int main()
{
    int num;

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

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

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

        cout << endl;
    }

    return 0;
}
  
πŸ’» Expected Output (Input: 5):
Enter the number:
5

        1
      2 2
    3 3 3
  4 4 4 4
5 5 5 5 5
  

πŸ“˜ Explanation

This C++ program prints a repeated number triangle pattern using nested for loops. The outer loop controls the number of rows, starting from 1 and increasing up to the entered number. The first inner loop prints leading spaces to properly align the pattern, while the second inner loop prints the current row number repeatedly. As the row number and the number of elements increase in each row, a repeated number triangle pattern is formed.

🧠 Algorithm

  1. Read the number from the user.
  2. Start the outer loop from 1 up to the entered number.
  3. Print leading spaces for proper alignment.
  4. Print the current row number repeatedly.
  5. Move to the next line after each row.
  6. Repeat until the complete repeated number triangle is displayed.

⏱ Time Complexity

O(n²)

πŸ’Ύ Space Complexity

O(1)

πŸ“ Search Description

Learn how to print a repeated number triangle pattern in C++ using nested loops. This beginner-friendly C++ program prints the current row number repeatedly while increasing the number of elements in each row, making it useful for learning loops and pattern printing in C++.

πŸ” Keywords

repeated number triangle pattern in C++, C++ program for repeated number pattern, number pattern using nested loops, repeated number pattern in C++, triangle pattern in C++, C++ pattern program, pattern printing in C++, nested loops in C++, C++ programming examples.

🏷 Hashtags

#CppProgramming #PatternPrograms #NumberPattern #RepeatedNumberPattern #TrianglePattern #Coding #Programming #NestedLoops #Cpp #1printf

⭐ C++ Program to Print Number Triangle Pattern

πŸ“„ Source Code:
#include<iostream>
using namespace std;

int main()
{
    int num;

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

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

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

        cout << endl;
    }

    return 0;
}
  
πŸ’» Expected Output (Input: 5):
Enter the number:
5

        1
      1 2
    1 2 3
  1 2 3 4
1 2 3 4 5
  

πŸ“˜ Explanation

This C++ program prints a number triangle pattern using nested for loops. The outer loop controls the number of rows, starting from 1 and increasing up to the entered number. The first inner loop prints spaces to properly align the pattern, while the second inner loop prints numbers from 1 up to the current row number. As the number of elements increases in every row, a number triangle pattern is formed.

🧠 Algorithm

  1. Read the number from the user.
  2. Start the outer loop from 1 up to the entered number.
  3. Print leading spaces for proper alignment.
  4. Print numbers from 1 to the current row value.
  5. Move to the next line after each row.
  6. Repeat until the complete number triangle is displayed.

⏱ Time Complexity

O(n²)

πŸ’Ύ Space Complexity

O(1)

πŸ“ Search Description

Learn how to print a number triangle pattern in C++ using nested loops. This beginner-friendly C++ program prints numbers from 1 to the current row value with proper spacing and alignment, making it useful for learning loops and pattern programs in C++.

πŸ” Keywords

number triangle pattern in C++, C++ program for number pattern, number pattern using nested loops, triangle pattern in C++, C++ pattern program, pattern printing in C++, nested loops in C++, C++ programming examples.

🏷 Hashtags

#CppProgramming #PatternPrograms #NumberPattern #TrianglePattern #Coding #Programming #NestedLoops #Cpp #1printf

C++ Program to Print Right-Aligned Incremental Number Triangle Pattern

Source Code

#include<iostream>
using namespace std;

int main()
{
    int num,a=0;

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

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

        for(int j=1;j<=i;j++)
        {
            cout<<a++<<" ";
        }

        cout<<endl;
    }
}

Expected Output

Enter the number:
5

          0
        1 2
      3 4 5
    6 7 8 9
  10 11 12 13 14

Explanation

This C++ program prints a right-aligned incremental number triangle pattern using nested loops. The outer loop controls the rows, while the first inner loop prints spaces to create the right-aligned triangle shape.

The variable a is initialized to 0. Inside the second inner loop, a++ prints the current value of a and then increments it by 1. Therefore, the numbers continue sequentially across all rows instead of restarting from 0 in each row.

Algorithm

  1. Start the program.
  2. Initialize the variable a to 0.
  3. Read the number of rows from the user.
  4. Run an outer loop from i = 1 to num.
  5. Use the first inner loop to print spaces before the numbers.
  6. Use the second inner loop to print the value of a.
  7. Increment a after every number using a++.
  8. Move to the next line after printing each row.
  9. Stop the program.

Time Complexity

O(n²) — The nested loops perform approximately n² operations to generate the spaces and numbers.

Space Complexity

O(1) — The program uses only a fixed number of variables and does not require additional memory proportional to the input size.

Search Description

Learn how to write a C++ program to print a right-aligned incremental number triangle pattern using nested loops. This beginner-friendly example demonstrates how to use a counter variable with the post-increment operator to print continuous numbers across multiple rows.

Keywords

C++ number pattern program, incremental number triangle, continuous number pattern in C++, right aligned number triangle, C++ pattern programs, nested loops in C++, number triangle using loops, sequential number pattern

Hashtags

#Cpp #CPlusPlus #CppProgramming #PatternPrograms #NumberPattern #NumberTriangle #NestedLoops #ProgrammingForBeginners #Coding #DSA #1printf

Comments

Popular Posts

πŸŒ™