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

⭐ C++ Program to Print Decreasing Star Triangle Pattern

This C++ program prints a decreasing star triangle pattern using nested for loops. The number of stars decreases by one in every row.

πŸ“„ Source Code

#include<iostream>
using namespace std;

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

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

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

        cout<<endl;
    }
}

πŸ’» Expected Output

Enter the number:
5

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

πŸ“˜ Explanation

The program first takes a number from the user and stores it in the variable num. The outer loop starts from num and decreases the value of i until it reaches 1.

The inner loop runs from 1 to i. During each iteration, it prints *. Therefore, the first row contains num stars, and every following row contains one fewer star.

For example, when num = 5, the number of stars printed in each row is: 5, 4, 3, 2, 1.

🧠 Algorithm

1. Start the program.
2. Declare the variable num.
3. Read the value of num from the user.
4. Initialize the outer loop from num down to 1.
5. For each value of i, run the inner loop from 1 to i.
6. Print * in every iteration of the inner loop.
7. Move to the next line after completing each row.
8. Repeat until the outer loop reaches 1.
9. Stop the program.

⏱ Time Complexity

n + (n-1) + (n-2) + ... + 1

= n(n+1)/2

Time Complexity = O(n²)

πŸ’Ύ Space Complexity

The program uses only a few integer variables and does not require any additional data structure.

Space Complexity = O(1)

πŸ“ Search Description

C++ program to print a decreasing star triangle pattern using nested for loops. Learn how to print stars in decreasing order with a simple C++ pattern program suitable for beginners.

πŸ” Keywords

C++ star pattern, C++ star triangle, decreasing star pattern, decreasing triangle pattern, inverted star triangle, C++ pattern programs, star pattern program in C++, C++ nested loops, C++ for loop pattern, triangle pattern in C++, star printing program, beginner C++ programs, pattern printing in C++

🏷 Hashtags

#CppProgramming #Cpp #PatternPrograms #StarPattern #StarTriangle #DecreasingPattern #TrianglePattern #InvertedTriangle #NestedLoops #ForLoop #Coding #Programming #LearnCpp #CppPatterns #1printf

⭐ C++ Program to Print Decreasing Consecutive Number Triangle Pattern

This C++ program prints a decreasing triangle pattern using consecutive numbers starting from 0. The number of elements decreases in every row, while the value of k continues increasing throughout the pattern.

πŸ“„ Source Code

#include<iostream>
using namespace std;

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

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

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

        cout<<endl;
    }
}

πŸ’» Expected Output

Enter the number:
5

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

πŸ“˜ Explanation

The program first takes a number from the user and stores it in the variable num. The variable k is initialized to 0.

The outer loop starts from num and decreases i until it reaches 1. This controls the number of elements printed in each row.

The inner loop runs from 1 to i. During every iteration, the expression k++ prints the current value of k and then increases it by 1.

Because k is declared outside both loops, its value is not reset when a new row starts. Therefore, the numbers continue sequentially from one row to the next.

🧠 Algorithm

1. Start the program.
2. Declare num and initialize k = 0.
3. Read the value of num from the user.
4. Initialize the outer loop from num down to 1.
5. For each row, run the inner loop from 1 to i.
6. Print the current value of k and increment it using k++.
7. Move to the next line after completing each row.
8. Repeat until the outer loop reaches 1.
9. Stop the program.

⏱ Time Complexity

n + (n-1) + (n-2) + ... + 1

= n(n+1)/2

Time Complexity = O(n²)

πŸ’Ύ Space Complexity

The program uses only a few integer variables and does not require any additional data structure.

Space Complexity = O(1)

πŸ“ Search Description

C++ program to print a decreasing consecutive number triangle pattern starting from 0. Learn how to use nested for loops and a counter variable to print continuous numbers across multiple rows.

πŸ” Keywords

C++ number pattern, C++ pattern programs, consecutive number triangle, decreasing number triangle, continuous number pattern, consecutive number pattern in C++, number triangle pattern, C++ nested loops, C++ for loop pattern, counter variable in C++, C++ pattern printing, beginner C++ programs, triangle pattern program

🏷 Hashtags

#CppProgramming #Cpp #PatternPrograms #NumberPattern #ConsecutiveNumbers #NumberTriangle #DecreasingPattern #TrianglePattern #NestedLoops #ForLoop #Coding #Programming #LearnCpp #CppPatterns #1printf

⭐ C++ Program to Print 0 1 Decreasing Triangle Pattern

This C++ program prints a decreasing triangle pattern using 0 and 1. Each row contains the same value, and the value changes between 0 and 1 for every row. The pattern is generated using nested for loops and the modulus operator %.

πŸ“„ Source Code

#include<iostream>
using namespace std;

int main()
{
    int num;

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

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

        cout<<endl;
    }
}

πŸ’» Expected Output

Enter the number:
5

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

πŸ“˜ Explanation

The program first takes a number from the user and stores it in the variable num. The outer loop starts from num and decreases the value of i until it reaches 1.

The inner loop runs from 1 to i. In every iteration, the expression i % 2 is calculated and printed. Since the value of i remains the same throughout a row, the same 0 or 1 is printed for that entire row.

If i is odd, i % 2 gives 1. If i is even, i % 2 gives 0. Therefore, the rows alternate between 1 and 0.

🧠 Algorithm

1. Start the program.
2. Read the number from the user.
3. Initialize the outer loop from num down to 1.
4. For each value of i, calculate i % 2.
5. Run the inner loop from 1 to i.
6. Print i % 2 in every iteration of the inner loop.
7. Move to the next line after completing each row.
8. Stop the program.

⏱ Time Complexity

n + (n-1) + (n-2) + ... + 1

= n(n+1)/2

Time Complexity = O(n²)

πŸ’Ύ Space Complexity

The program uses only integer variables and does not require any additional data structure.

Space Complexity = O(1)

πŸ“ Search Description

C++ program to print a 0 1 decreasing triangle pattern using nested for loops and the modulus operator. Each row contains repeated 0 or 1 values, creating an alternating binary number pattern.

πŸ” Keywords

C++ 0 1 pattern, C++ binary pattern, 0 1 triangle pattern, decreasing 0 1 pattern, repeated 0 1 pattern, binary triangle pattern in C++, C++ pattern programs, C++ nested loops, C++ modulus operator, C++ for loop pattern, number pattern in C++, beginner C++ programs, pattern printing in C++

🏷 Hashtags

#CppProgramming #Cpp #PatternPrograms #01Pattern #BinaryPattern #NumberPattern #TrianglePattern #DecreasingPattern #NestedLoops #ForLoop #ModulusOperator #Coding #Programming #LearnCpp #CppPatterns #1printf

⭐ C++ Program to Print 0 1 Decreasing Triangle Pattern

This C++ program prints a decreasing triangle pattern containing alternating 0 and 1 values using nested for loops and the modulus operator %.

πŸ“„ Source Code

#include<iostream>
using namespace std;

int main()
{
    int num;

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

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

        cout<<endl;
    }
}

πŸ’» Expected Output

Enter the number:
5

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

πŸ“˜ Explanation

The program first takes a number from the user and stores it in the variable num. The outer loop starts from num and decreases the value of i until it reaches 1.

The inner loop starts from 1 and runs up to i. In every iteration, the expression j % 2 is calculated.

When j is odd, j % 2 gives 1. When j is even, j % 2 gives 0. Therefore, the output alternates between 1 and 0.

🧠 Algorithm

1. Start the program.
2. Read the number from the user.
3. Initialize the outer loop from num down to 1.
4. For each value of i, run the inner loop from 1 to i.
5. Calculate j % 2 and print the result.
6. Move to the next line after completing each row.
7. Stop the program.

⏱ Time Complexity

n + (n-1) + (n-2) + ... + 1

= n(n+1)/2

Time Complexity = O(n²)

πŸ’Ύ Space Complexity

The program uses only a few integer variables and does not use any additional data structure.

Space Complexity = O(1)

πŸ“ Search Description

C++ program to print a decreasing 0 1 number triangle pattern using nested for loops and the modulus operator. Learn how to create alternating 0 and 1 patterns in C++ with simple programming logic.

πŸ” Keywords

C++ 0 1 pattern, C++ 0 1 triangle, C++ binary pattern, 0 1 number pattern, decreasing 0 1 pattern, 0 1 triangle pattern in C++, C++ pattern programs, C++ nested loops, C++ modulus operator, C++ for loop pattern, binary number pattern, beginner C++ programs, pattern printing in C++

🏷 Hashtags

#CppProgramming #Cpp #PatternPrograms #NumberPattern #01Pattern #BinaryPattern #TrianglePattern #DecreasingPattern #NestedLoops #ForLoop #ModulusOperator #Coding #Programming #LearnCpp #1printf

⭐ C++ Program to Print Decreasing Repeated Number Triangle Pattern

This C++ program prints a decreasing number triangle pattern using nested for loops. Each row contains the same number, and the number of elements decreases by one in every row.

πŸ“„ Source Code

#include<iostream>
using namespace std;

int main()
{
    int num;

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

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

        cout<<endl;
    }
}

πŸ’» Expected Output

Enter the number:
5

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

πŸ“˜ Explanation

The program first takes a number from the user and stores it in the variable num. The outer loop starts from num and decreases the value of i until it reaches 1.

The inner loop runs from 1 to i. During every iteration, the value of i is printed. Therefore, every row contains the same number, while the number of elements decreases in each row.

🧠 Algorithm

1. Start the program.
2. Read the number from the user.
3. Initialize the outer loop from num down to 1.
4. For each value of i, run the inner loop from 1 to i.
5. Print the value of i in each iteration of the inner loop.
6. Move to the next line after completing each row.
7. Stop the program.

⏱ Time Complexity

The outer loop runs n times and the inner loop runs a decreasing number of times. Therefore, the total number of operations is approximately:

n + (n-1) + (n-2) + ... + 1

= n(n+1)/2

Time Complexity = O(n²)

πŸ’Ύ Space Complexity

The program uses only a few integer variables and does not require additional data structures.

Space Complexity = O(1)

πŸ“ Search Description

C++ program to print a decreasing repeated number triangle pattern using nested for loops. Learn how to create number patterns in C++ with simple loops and beginner-friendly programming logic.

πŸ” Keywords

C++ number pattern, C++ pattern programs, decreasing number pattern, repeated number triangle, decreasing repeated number triangle, number triangle pattern in C++, C++ nested loops, C++ for loop pattern, triangle pattern program, beginner C++ programs, C++ pattern printing, number pattern programs

🏷 Hashtags

#CppProgramming #Cpp #PatternPrograms #NumberPattern #NumberTriangle #DecreasingPattern #TrianglePattern #NestedLoops #ForLoop #Coding #Programming #LearnCpp #CppPatterns #1printf

⭐ C++ Program to Print Decreasing Number Triangle Pattern

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

int main()
{
    int num;

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

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

        cout << endl;
    }

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

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

πŸ“˜ Explanation

This C++ program prints a decreasing number triangle pattern using nested for loops. The outer loop starts from the entered number and decreases down to 1. Therefore, the number of elements in each row decreases by one.

The inner loop starts from 1 and runs up to the current value of i. It prints the numbers from 1 to i in each row. After completing each row, cout << endl moves the cursor to the next line.

🧠 Algorithm

  1. Start the program.
  2. Read the number from the user.
  3. Start the outer loop from i = num and decrease it down to 1.
  4. Start the inner loop from j = 1 up to i.
  5. Print the value of j followed by a space.
  6. Move to the next line after printing each row.
  7. Repeat until i becomes 1.
  8. Stop the program.

⏱ Time Complexity

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

πŸ’Ύ 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 print a decreasing number triangle pattern in C++ using nested loops. This beginner-friendly C++ program prints numbers from 1 to the current row value while decreasing the number of elements in each row.

πŸ” Keywords

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

🏷 Hashtags

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

Comments

Popular Posts

πŸŒ™