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 Pattern Program: Diagonal Number Pattern

C Pattern Program: Diagonal Number Pattern

๐Ÿ”ท C Pattern Program: Diagonal Number Pattern

#include <stdio.h>

int main()
{
    int num;
    printf("Enter the number:\n");
    scanf("%d", &num);
    
    for (int i = 0; i < num; i++)
    {
        for (int j = 0; j < num; j++)
        {
            if (j == i)
                printf("%d", i + 1);
            else if (j == num - 1 - i)
                printf("%d", num - i);
            else
                printf(" ");
        }
        printf("\n");
    }

    return 0;
}
  

๐Ÿ“˜ Explanation:

This C program prints a square pattern with two diagonal lines:

๐Ÿ”น On the **main diagonal** (from top-left to bottom-right), it prints i+1 (increasing number).
๐Ÿ”น On the **anti-diagonal** (from top-right to bottom-left), it prints num - i (decreasing number).
๐Ÿ”น All other positions are filled with spaces to maintain the structure.
๐Ÿ”น The result is a visually symmetric pattern based on the input size.

๐Ÿ” Sample Output:

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

๐Ÿท️ Keywords:

C pattern printing, diagonal number pattern, matrix pattern in C, double diagonal number pattern, C loop-based programs

Comments

๐ŸŒ™